java.lang.OutOfMemoryError : Java heap space at org. apache.xmlbeans.impl.store.Saver$TextSaver.resize

标签 java excel apache-poi out-of-memory

我试图将我的数据导出到 Excel 工作表中,它包含近 70000 行。 我收到以下错误:

Exception in thread "http-bio-8765-exec-1" java.lang.OutOfMemoryError: Java heap space at org.apache.xmlbeans.impl.store.Saver$TextSaver.resize(Saver.java:1700) at org.apache.xmlbeans.impl.store.Saver$TextSaver.preEmit(Saver.java:1303) at org.apache.xmlbeans.impl.store.Saver$TextSaver.emit(Saver.java:1190)

下面是用于编写Excel的代码:

public String getXLSWriter(ResultSet rs, String file_name)
    {
        String filepath="";
        try {
            //String filename=file_name.replace('/','_');
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet(file_name);

            XSSFRow rowhead = sheet.createRow((int) 0);
             filepath=prop.getProperty("file_store_path")+file_name+timeStamp+".xlsx";

            FileOutputStream fileOut = new FileOutputStream(filepath);
            rowhead.createCell((int) 0).setCellValue("ID");
            rowhead.createCell((int) 1).setCellValue("firstName");
            rowhead.createCell((int) 2).setCellValue("LastName");

            int i = 1;
            while (rs.next()){
                XSSFRow row = sheet.createRow((int) i);

                row.createCell((int) 0).setCellValue(rs.getString("id")); 
                row.createCell((int) 1).setCellValue(rs.getString("first_name") );
                row.createCell((int) 2).setCellValue(rs.getString("last_name"));


                i++;
            }

            workbook.write(fileOut);
            fileOut.close();
            System.out.println("XLS done!!"); 
            } catch (SQLException e1) {
                e1.printStackTrace();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

下面是我用于下载 Excel 的代码:

public void getDownloaded(String filepath,HttpServletResponse response)
    {
        try {
            File file=new File(filepath);
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition","attachment;filename="+file.getName());
            FileInputStream in=new FileInputStream(file);
            ServletOutputStream out=response.getOutputStream();
            byte[] outputByte = new byte[4096];
            //copy binary content to output stream
            int length;
            while((length=in.read(outputByte, 0, 4096)) != -1){
                out.write(outputByte, 0, length);
            }

            in.close();
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        }

最佳答案

启动java时不要一次读取所有数据或使用-Xmx选项来增加最大内存。 检查内存泄漏并将此数据导入数据库以节省本地内存。

关于java.lang.OutOfMemoryError : Java heap space at org. apache.xmlbeans.impl.store.Saver$TextSaver.resize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48880092/

相关文章:

java - saveAsTextFile() 将最终的 RDD 写入单个文本文件 - Apache Spark

VBA 错误 : Ambiguous Name Detected (Excel 2010)

c# - 如何使用 VB.net 或 C# 从 Excel 动态删除行并保存

excel - VBA 返回包含特定字符串的单元格的索引

java - Apache POI 获取字体规范

java - 如何使用 setter 方法在 hibernate 中的列中显式设置 NULL 值?

java - Apache poi 文件损坏且无法写入现有工作簿

java - 如何从sqlite数据库android获取一个值?

java - 在 JSP 页面内调试?

java - java代码设置日期和时间的问题