Apache POI 在编辑后保留预定义的宏
Apache POI to keep predefined macros after editing
我有 XLSX
和 XLSM
文件,其中包含一些预定义的宏。我尝试使用 Apache POI
来编辑这些文件,但宏会自动删除。当我在修改后尝试打开文件时收到通知 window,文件已被删除,因为文件已损坏:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error066080_01.xml</logFileName>
<summary>Error in file (C:\_privat\completeReport.xlsm)</summary>
<removedRecords>
<removedRecord>/xl/worksheets/sheet1.xml</removedRecord>
<removedRecord>/xl/calcChain.xml</removedRecord>
</removedRecords>
</recoveryLog>
我使用以下代码来处理文件,Vars.XLS
包含 XLSX/XLSM
文件的路径。
File file = new File(Vars.XLS);
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook workbook = XSSFWorkbookFactory.createWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.createRow(recordcount+4);
Cell cell;
cell = row.createCell(0);
cell.setCellValue(recordcount);
inputStream.close();
FileOutputStream fileOut=new FileOutputStream(Vars.XLS);
workbook.write(fileOut);
fileOut.close();
您正在尝试更新 excel sheet。在更新的情况下,您不应创建行或单元格,而必须读取单元格。我在下面提供了简短的代码片段,你可以试试。
XSSFRow row = sheet.getRow(some number);
Cell cell = row.getCell(some number);
cell.setCellValue(recordcount);
我有 XLSX
和 XLSM
文件,其中包含一些预定义的宏。我尝试使用 Apache POI
来编辑这些文件,但宏会自动删除。当我在修改后尝试打开文件时收到通知 window,文件已被删除,因为文件已损坏:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error066080_01.xml</logFileName>
<summary>Error in file (C:\_privat\completeReport.xlsm)</summary>
<removedRecords>
<removedRecord>/xl/worksheets/sheet1.xml</removedRecord>
<removedRecord>/xl/calcChain.xml</removedRecord>
</removedRecords>
</recoveryLog>
我使用以下代码来处理文件,Vars.XLS
包含 XLSX/XLSM
文件的路径。
File file = new File(Vars.XLS);
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook workbook = XSSFWorkbookFactory.createWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.createRow(recordcount+4);
Cell cell;
cell = row.createCell(0);
cell.setCellValue(recordcount);
inputStream.close();
FileOutputStream fileOut=new FileOutputStream(Vars.XLS);
workbook.write(fileOut);
fileOut.close();
您正在尝试更新 excel sheet。在更新的情况下,您不应创建行或单元格,而必须读取单元格。我在下面提供了简短的代码片段,你可以试试。
XSSFRow row = sheet.getRow(some number);
Cell cell = row.getCell(some number);
cell.setCellValue(recordcount);