读取模板xlsm并使用SXSSF(POI)将大量数据写入现有工作表

我能够使用XSSF读取现有的xlsm,并使用SXSSF将数据写入工作表。 最后将其作为另一个具有OutputStream的xlsm输出。
SXSSF被提及在文档中编写xlsx
是否正确的方法来读取和写入大量的数据xlsm或文件将被损坏,如果这个解决scheme?
这是工作的示例代码,

public static void main(String[] args) throws Throwable { OPCPackage pkg = OPCPackage.open(new File("sample.xlsm")); XSSFWorkbook wb_template; wb_template = new XSSFWorkbook( pkg ); System.out.println("package loaded"); SXSSFWorkbook wb = new SXSSFWorkbook(wb_template); wb.setCompressTempFiles(true); SXSSFSheet sh = (SXSSFSheet) wb.createSheet(); sh.setRandomAccessWindowSize(100);// keep 100 rows in memory, exceeding rows will be flushed to disk for(int rownum = 4; rownum < 5000; rownum++){ Row row = sh.createRow(rownum); for(int cellnum = 0; cellnum < 10; cellnum++){ Cell cell = row.createCell(cellnum); String address = new CellReference(cell).formatAsString(); cell.setCellValue(address); } } FileOutputStream out = new FileOutputStream(new File("C:\\ouput\\new_file.xlsm")); wb.write(out); out.close(); } 

我没有看到你当前的代码有太多错误,但是在apache网站上,它说SXSSF在你的计算机上留下临时文件,而且你必须调用如下的处理方式:

 Note that SXSSF allocates temporary files that you must always clean up explicitly, by calling the dispose method. SXSSFWorkbook wb = new SXSSFWorkbook(100); // dispose of temporary files backing this workbook on disk wb.dispose(); 

我会build议执行处理,因为它似乎是防止剩余的信息被存储。 在是否正确的方法,我会build议拿一个testing文件,并给它几个去,这个代码执行将如何将会受到你的系统(CPU功率,内存等)和文件的大小处理。

祝你好运!