利用POi3.8導出excel產生大量xml臨時文件怎麽辦?
阿新 • • 發佈:2018-02-13
put lln exce del div 清除 img style eve
在實際項目中,經常會用到POI3.8來導出excel。而導出excel的時候,會因為殘留大量以.xml結尾的文件而導致服務器存儲空間急劇增長,最後導致系統掛了。為此,該怎麽辦呢?
.xml後綴殘留文件示例
通過大量的翻閱資料,目前有兩種解決方式:
方式1:手動清除臨時文件
POI3.8並沒有提供方法來清除臨時文件,為此,這裏可以自己手動進行清除:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency>
package com.jack.shx.MySpringBoot; import java.io.File; import java.lang.reflect.Field; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.streaming.SheetDataWriter;public class SXSSFTempFilesDelete { /*** * Returns a private attribute of a class * @param containingClass The class that contains the private attribute to retrieve * @param fieldToGet the name of the attribute to get * @return The private attribute * @throws NoSuchFieldException *@throws IllegalAccessException */ public static Object getPrivateAttribute( Object containingClass, String fieldToGet) throws NoSuchFieldException, IllegalAccessException { // get the field of the containingClass instance Field declaredField = containingClass.getClass().getDeclaredField(fieldToGet); declaredField.setAccessible(true); // access it Object get = declaredField.get(containingClass); // return it! return get; } /*** * Deletes all temporary files of the SXSSFWorkbook instance * @param workbook * @throws NoSuchFieldException * @throws IllegalAccessException */ public static void deleteSXSSFTempFiles(SXSSFWorkbook workbook) throws NoSuchFieldException, IllegalAccessException { int numberOfSheets = workbook.getNumberOfSheets(); // iterate through all sheets (each sheet as a temp file) for (int i = 0; i <= numberOfSheets; i++) { Sheet sheetAt = workbook.getSheetAt(i); // delete only if the sheet is written by stream if (sheetAt instanceof SXSSFSheet) { SheetDataWriter sdw = (SheetDataWriter) getPrivateAttribute(sheetAt, "_writer"); File f = (File) getPrivateAttribute(sdw, "_fd"); try { f.delete(); } catch (Exception ex) { // could not delete the file } } } } }
或者
方式2:將POI3.8替換成更高的版本,並利用dispose方法清除臨時文件
目前,更高版本的POI已經提供了清除臨時文件的方法,具體可以參考官網:http://poi.apache.org/apidocs/org/apache/poi/xssf/streaming/SXSSFWorkbook.html
此時,我們在使用的時候就可以方便的清除臨時文件,避免空間爆滿了:
/** * 大數據量導出 * @throws IOException */ @Test public void text2() throws IOException { XSSFWorkbook xssfWorkbook = new XSSFWorkbook(Thread.currentThread().getContextClassLoader().getResourceAsStream("bigdata.xlsx")); SXSSFWorkbook wb = new SXSSFWorkbook(xssfWorkbook, 1000); //內存中保留 1000 條數據,以免內存溢出,其余寫入 硬盤 專門處理大數據 Sheet sh = wb.getSheetAt(0); for(int rownum = 1; rownum < 75537; 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); } } // // Rows with rownum < 900 are flushed and not accessible // for(int rownum = 0; rownum < 900; rownum++){ // Assert.assertNull(sh.getRow(rownum)); // } // // // ther last 100 rows are still in memory // for(int rownum = 900; rownum < 1000; rownum++){ // Assert.assertNotNull(sh.getRow(rownum)); // } FileOutputStream out = new FileOutputStream("D:\\sxssf.xlsx"); wb.write(out); out.close(); // dispose of temporary files backing this workbook on disk wb.dispose();
請註意 就這個一句話 wb.dispose();
利用POi3.8導出excel產生大量xml臨時文件怎麽辦?