0

I have XLSX and XLSM files which contains some predefined macros. I try to use Apache POI to edit these files, but macros are removed automatically. I get a notification window when I try to open file after modification, that files are removed, because file is broken:

<?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>

I use following code to handle fle, Vars.XLS contains path to a XLSX/XLSM file.

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();
plaidshirt
  • 5,189
  • 19
  • 91
  • 181
  • 1
    A `*.xlsx` file cannot contain macros, only `*.xlsm` can. And the recovery log shown has nothing to do with macro removing. A worksheet and the calculation chain was removed. And your code shown does simple things which thousands of other `apache poi` users also are doing without problems. So your problems are not related to the code but probably to the used file where `Vars.XLS` points to. So no help possible without having that file. – Axel Richter Jun 06 '19 at 04:01

1 Answers1

1

You are trying to update an excel sheet. In case of updation, you should not create row or cell, rather you have to read the cell. I provide below the brief code snippet, you can try.

XSSFRow row = sheet.getRow(some number);
Cell cell = row.getCell(some number);
cell.setCellValue(recordcount);
Sambit
  • 7,625
  • 7
  • 34
  • 65
  • I don't get error message, but file size is smaller after modification than original one, but I added a few more lines. – plaidshirt Jun 06 '19 at 10:44