利用POi3.8导出excel产生大量xml临时文件怎么办?

本文介绍了解决使用Apache POI导出Excel时产生的大量XML临时文件问题的两种方法:一是通过反射手动删除临时文件;二是升级POI版本并使用dispose方法释放资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在实际项目中,经常会用到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();

原文地址:https://www.cnblogs.com/jack-Star/p/8445675.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值