File file = new File("D:\\testExcelInput.xlsx");
FileInputStream fIP = new FileInputStream(file);
//get the existing sheet from the workbook
XSSFWorkbook wb = new XSSFWorkbook(fIP);
XSSFSheet sheet = wb.getSheetAt(0);
//get the count of already existing rows
int i=sheet.getPhysicalNumberOfRows();
Row row=sheet.createRow(++i);
//input date in the required format
Cell cellDate = row.createCell(0);
cellDate.setCellValue(dateFormat.format(date));
//input item ID
Cell cellItem = row.createCell(1);
//cellItem.setCellType(CellType.NUMERIC);
cellItem.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
cellItem.setCellValue(ItemID);
//input name
Cell cellName = row.createCell(2);
cellName.setCellValue(ItemName);
//create a number cell and insert quantity
Cell cellQuant = row.createCell(3);
cellQuant.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
cellQuant.setCellValue(quant);
try ( //save the changes by writing the data in the workbook
FileOutputStream fileOut = new FileOutputStream("D:\\testExcelInput.xlsx")) {
wb.write(fileOut);
//wb.close();
这是我的代码。我找不到错误背后的原因。当我使用hssf和.xls文件时它工作正常,但我需要使用.xlsx文件,这就是我使用xssf的原因。
答案 0 :(得分:0)
尝试将此库添加到依赖项中:
xmlbeans-2.6.0.jar
使用.xlsx
文件时需要它。
如果你正在使用maven,你可以这样依赖:
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>