java - Java 中的 Excel 生成问题

标签 java excel hibernate spring-mvc apache-poi

我正在尝试使用 Apache POI 生成 Excel 文件。我的需求是生成 120k 行和 7 列的数据。我可以轻松生成包含 25,000 行的 excel 文件,但问题是我无法生成包含 100k 行的 excel 文件。代码的运行时执行速度变慢。

为了生成 excel 文件,系统必须通过 for 循环(100k 行和 9 列)迭代 900k 次。
最初在 excel 对象中添加 24 行大约需要 1 秒,然后逐渐降低执行速度。
添加 70k 行后,添加一行需要 24..30 秒。为什么会这样?

List<String[]> keyList = keyService.findAllKeyByBatchCode(batchCode);

int x = 1;
if(keyList != null && keyList.size() != 0) {
    Date activated_Date = null;

    for (int i = 0; i<keyList.size(); i++) {
        Object[] keyUser = keyList.get(i);

        data.put(x, new Object[] {
            String.valueOf(x), 
            keyForDetails.getLicenceType().getName(),
            batchCode,
            keyForDetails.getKeyType(),
            keyUser[0], 
            "", 
            DateUtility.convertUtilDatetoString(keyForDetails.getDate()), 
            String.valueOf(keyForDetails.getPrice()),
            keyUser[1]
        });

        x++;
    }
}

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Key sheet");
Set<Integer> keyset = data.keySet();
int rownum = 0;
System.out.println("-------------printin------");

try {
    for (Integer key : keyset) {
        Row row = sheet.createRow(rownum++);
        Object [] objArr = data.get(key);
        int cellnum = 0;

        for (Object obj : objArr) {
            System.out.println("-------------print cell------");
            Cell cell = row.createCell(cellnum++);

            if(obj instanceof Date) 
                cell.setCellValue((Date)obj);
            else if(obj instanceof Boolean)
                cell.setCellValue((Boolean)obj);
            else if(obj instanceof String)
                cell.setCellValue((String)obj);
            else if(obj instanceof Double)
                cell.setCellValue((Double)obj);
        }   
    }

    if(rownum != 1) {
        System.out.println("-------------file writein------");
        FileOutputStream out =  new FileOutputStream(new File("C:/Users/Public/Documents/"+batchCode+" page"+n+".xlsx"));
        workbook.write(out);
        out.close();
        System.out.println("Excel written successfully..");
    }
} catch (Exception e) {
    e.printStackTrace();
}

最佳答案

Apache-poi 有一个 streaming api并且可以处理大数据。从 apache POI 站点的文档中可以看出,从版本 3.8.beta3 开始,有一个新的 api 可用,称为 SXSSF。它的占用空间小得多,应该用于生成非常大的 excel 文件。

你可以查看这个link

您还可以引用以下基本代码:

public class ReadWriteExcelFile {  

    public static void main(String[] args) throws IOException {  
        String excelFileName = "C:\\temp\\Test.xlsx";//name of excel file  

        String sheetName = "Sheet1";//name of sheet  

        SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
        Sheet sheet = wb.createSheet(sheetName);  

//iterating r number of rows  
        for (int r = 0; r < 55555; r++) {  
            Row row = sheet.createRow(r);  

//iterating c number of columns  
            for (int c = 0; c < 5; c++) {  
                Cell cell = row.createCell(c);  
                cell.setCellValue("Cell " + r + " " + c);  
            }  
            if ( r % 1000 == 0) {
                System.out.println(r);
            }
        }  

        FileOutputStream out = new FileOutputStream(excelFileName);
        wb.write(out);
        out.close();
    }  
}

关于java - Java 中的 Excel 生成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39367726/

相关文章:

java - 如何编写具有属性和值的 JAXB 对象

excel - 在 WCF 数据服务中指定空 IQueryable 中的列?

java - 是否有一些完全配置的 Spring Wicket Hibernate 原型(prototype),包括注释?

java - SpringFramework 和 Hibernate 应用程序 HTTP 状态 404 Java

java - 剪刀石头布游戏(菜单法)

java - 缓存基本 XWPFDocument 模板并重用它们来生成文档

excel - 如何仅使用公式计算excel中由分隔符分隔的字符串中的值?

excel - 绝对引用 - 在用 R1C1 表示法编写的等式中插入 '$' 的等价物

java - 错误 org.hibernate.exception.SQLGrammarException : could not extract ResultSet

java - AudioRecord 对象未在项目中初始化