我的word文档包含多个表格。 我能够使用Apache POI读取表数据,但我无法将该数据写入excel工作簿。
阅读word文档的代码:
List<XWPFTable> table = doc.getTables();
System.out
.println("==========No Of Tables in Document============================================="
+ table.size());
for (XWPFTable xwpfTable : table) {
System.out.println("================table -" + tableIdx
+ "===Data==");
rowIdx = 1;
List<XWPFTableRow> row = xwpfTable.getRows();
ArrayList<String> rawData = new ArrayList<String>();
for (XWPFTableRow xwpfTableRow : row) {
colIdx = 1;
List<XWPFTableCell> cell = xwpfTableRow.getTableCells();
for (XWPFTableCell xwpfTableCell : cell) {
if (xwpfTableCell != null) {
aryNumbers[rowIdx][colIdx] = xwpfTableCell.getText()
.toString();
}
colIdx++;
}
System.out.println("");
rowIdx++;
}
tableIdx++;
}
}
为了将数据写入excel,我使用的是以下代码:
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("StructureValidation");
int rowCount = 0;
for (Object[] aBook : aryNumbers) {
Row row = sheet.createRow(++rowCount);
int columnCount = 0;
for (Object field : aBook) {
Cell cell = row.createCell(++columnCount);
if (field instanceof String) {
cell.setCellValue((String) field);
} else if (field instanceof Integer) {
cell.setCellValue((Integer) field);
}
}
}
try (FileOutputStream outputStream = new FileOutputStream("JavaBooks.xlsx")) {
workbook.write(outputStream);
}
从word文档中读取数据后,我想将它写在excel工作簿上。 但我无法这样做,请帮助。