gpt4 book ai didi

java - 如何使用 Java 从 Excel (XLSX) 构建 XML

转载 作者:行者123 更新时间:2023-12-01 17:35:44 24 4
gpt4 key购买 nike

我正在使用 netbeans 7.0 中的 Java 读取 Excel (XSLX) 文件。我也能够读取 Excel 工作表内容并将其打印输出。

现在我必须将 Excel 数据转换为 XML 文件。标签将是列标题,每一行都会进入相应的标签。

这是 xslx 文件中的输入工作表。 ID、变量、描述和注释是列标题。

ID          Variable     Desc     Notes
B0001 VSI_C
B0001 1 VSI_C_R
B0001 2 VSI_C_P
B0002 VSI_C_L
B0003 VSI_C_H
B0004 VSI_C_O

现在,我正在将此数据转换为 XML 文件。我期望的输出是,

<?xml version="1.0" encoding="UTF-8"?>
<Bin_code>
<DCT>
<ID>B0001</ID>
<Variable/>
<Desc>VSI_C</Desc>
<Notes/>
</DCT>
<DCT>
<ID>B0001</ID>
<Variable/>
<Desc>VSI_C_R</Desc>
<Notes/>
</DCT>
............
...............
</Bin_code>

我已经尝试过了。我知道我必须使用“sheet”对象。但我不确定如何使用它。

import java.io.File;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel. Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class XSLXReader {
public static void main(String[] args)
{
DataInputStream in = null;
BufferedReader br = null;
FileWriter fostream;
FileWriter fostreamBatch;
BufferedWriter out = null;
BufferedWriter outBatch = null;
String strOutputPath = "D:\\Proj\\Current_\\";
String strFilePrefix = "Master 5.2-B";
String strLine;

try {
InputStream inputStream = new FileInputStream(new File("D:\\Proj\\Current_\\Master A-B.xlsx"));
Workbook wb = WorkbookFactory.create(inputStream);
Sheet sheet = wb.getSheet("Bin-code");

in = new DataInputStream(inputStream);
br = new BufferedReader(new InputStreamReader(in));

fostream = new FileWriter(strOutputPath+"\\"+strFilePrefix+".xml");
out = new BufferedWriter(fostream);

out.write("<Bin-code>");

while ((strLine = br.readLine()) != null)
{
out.write("<DCT>");
out.write("<ID>" + strLine.substring(1, strLine.length()) + "</ID>");

out.write("</DCT>");

}
out.write("</Bin-code>");
} catch (Exception e) {
e.printStackTrace();
}
}

}

请帮助我将 xslx 中的输入数据构造为 xml 中的输出数据,如上所示。

谢谢拉姆

最佳答案

您不应该使用InputStream直接读取文件。打开工作簿并访问工作表后,您只需迭代工作表中的所有行,然后迭代该行中的单元格。示例位于 Apache POI 站点 http://poi.apache.org/spreadsheet/quick-guide.html#Iterator 。然后,您可以像现在一样手动打印 XML,或者使用众多 XML 库之一和 DTD 来为您完成此操作。

这是完整的源代码:

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import java.text.*;

public class XSLXReader {
static DecimalFormat df = new DecimalFormat("#####0");

public static void main(String[] args) {
FileWriter fostream;
PrintWriter out = null;
String strOutputPath = "D:\\Proj\\Current_\\";
String strFilePrefix = "Master 5.2-B";

try {
InputStream inputStream = new FileInputStream(new File("D:\\Proj\\Current_\\Master A-B.xlsx"));
Workbook wb = WorkbookFactory.create(inputStream);
Sheet sheet = wb.getSheet("Bin-code");

fostream = new FileWriter(strOutputPath + "\\" + strFilePrefix+ ".xml");
out = new PrintWriter(new BufferedWriter(fostream));

out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<Bin-code>");

boolean firstRow = true;
for (Row row : sheet) {
if (firstRow == true) {
firstRow = false;
continue;
}
out.println("\t<DCT>");
out.println(formatElement("\t\t", "ID", formatCell(row.getCell(0))));
out.println(formatElement("\t\t", "Variable", formatCell(row.getCell(1))));
out.println(formatElement("\t\t", "Desc", formatCell(row.getCell(2))));
out.println(formatElement("\t\t", "Notes", formatCell(row.getCell(3))));
out.println("\t</DCT>");
}
out.write("</Bin-code>");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private static String formatCell(Cell cell)
{
if (cell == null) {
return "";
}
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
return "";
case Cell.CELL_TYPE_BOOLEAN:
return Boolean.toString(cell.getBooleanCellValue());
case Cell.CELL_TYPE_ERROR:
return "*error*";
case Cell.CELL_TYPE_NUMERIC:
return XSLXReader.df.format(cell.getNumericCellValue());
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
default:
return "<unknown value>";
}
}

private static String formatElement(String prefix, String tag, String value) {
StringBuilder sb = new StringBuilder(prefix);
sb.append("<");
sb.append(tag);
if (value != null && value.length() > 0) {
sb.append(">");
sb.append(value);
sb.append("</");
sb.append(tag);
sb.append(">");
} else {
sb.append("/>");
}
return sb.toString();
}
}

这会产生输出:

<?xml version="1.0" encoding="UTF-8"?>
<Bin-code>
<DCT>
<ID>B0001</ID>
<Variable/>
<Desc>VSI_C</Desc>
<Notes/>
</DCT>
<DCT>
<ID>B0001</ID>
<Variable>1</Variable>
<Desc>VSI_C_R</Desc>
<Notes/>
</DCT>
<DCT>
<ID>B0001</ID>
<Variable>2</Variable>
<Desc>VSI_C_P</Desc>
<Notes/>
</DCT>
<DCT>
<ID>B0002</ID>
<Variable/>
<Desc>VSI_C_L</Desc>
<Notes/>
</DCT>
<DCT>
<ID>B0003</ID>
<Variable/>
<Desc>VSI_C_H</Desc>
<Notes/>
</DCT>
<DCT>
<ID>B0004</ID>
<Variable/>
<Desc>VSI_C_O</Desc>
<Notes/>
</DCT>
</Bin-code>

关于java - 如何使用 Java 从 Excel (XLSX) 构建 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6572156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com