读取模板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功率,内存等)和文件的大小处理。
祝你好运!
- 使用apache poi 3.7多次写入xlsx文档时出现exception
- 如何让用户从Java / Struts的Web应用程序中将数据库中的数据下载到Excel表单文件中?
- Java apache poi,在excel.equals中的date在java中的常规stringdate
- 如何比较Excel中的两个或两个以上的值与值中的李和报告通过或失败在同一个Excel中使用Apache poi selenium webdriver Java
- Apache POI HSSF XLS读取错误
- 尝试从具有Apache POI的Excel工作簿中获取公式时出错
- Excel工作表的单元格types显示为实际上是文本types的数字
- 使用apache poi读取excel – Hashmap,ArrayList
- 导入Apache POI单元库