我一直在尝试使用循环将 Excel 值附加到某些特定的 xml 节点中。
我现在使用的代码:
public ExcelReaderAndWriter(String inputFileName,String outputFileName) throws IOException, InvalidFormatException {
// Creating a Workbook from an Excel file (.xls or .xlsx)
try (Workbook workbook = WorkbookFactory.create(new File(inputFileName))) {
// Getting the Sheet at index zero
Sheet sheet = workbook.getSheet("XSL_RULES");
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
//obtain a rowIterator and columnIterator and iterate over them
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator <Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// Now let's iterate over the columns of the current row
Iterator <Cell> cellIterator = row.cellIterator();
if (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = dataFormatter.formatCellValue(cell);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
try {
docBuilder = docFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
所以这段代码“以某种方式”工作,但我在递归获取每个元素时遇到问题。当我执行它时,它会将文件创建到某个位置(通常是文件的末尾和最后一个单元格值。
我想我在 While 循环中也犯了一个错误,因为它在一次执行后永远不会停止,多次循环在最后停止。
这里的错误在哪里,我怎样才能以更好、更干净的方式改进它?谢谢!
编辑:我找到了解决方案并编辑了代码。这是工作。
问题是我的 for 循环,我找不到如何计算行号并根据该循环获取数字。这是关于整张纸和行号。毕竟没有那么难。希望对其他人有所帮助。
素胚勾勒不出你
相关分类