我有一个可以写入 Excel 文件的程序。 它使用 Apache POI 编写 excel 2007 文件(我有超过 256 列,所以我必须使用它)。该程序有效。我已经在非常小的文件上对其进行了测试,但如果我使用更多行,则会耗尽内存。
这是堆栈跟踪:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.io.ByteArrayOutputStream.write(Unknown Source)
at org.apache.poi.openxml4j.opc.internal.MemoryPackagePartOutputStream.write(MemoryPackagePartOutputStream.java:88)
at org.apache.xmlbeans.impl.store.Cursor._save(Cursor.java:590)
at org.apache.xmlbeans.impl.store.Cursor.save(Cursor.java:2544)
at org.apache.xmlbeans.impl.values.XmlObjectBase.save(XmlObjectBase.java:212)
at org.apache.poi.xssf.usermodel.XSSFSheet.write(XSSFSheet.java:2480)
at org.apache.poi.xssf.usermodel.XSSFSheet.commit(XSSFSheet.java:2439)
at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:196)
at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:200)
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:204)
at model.Conversione.traduzioneFile(Conversione.java:219)
at model.Main.scriviFile(Main.java:75)
at model.Main.main(Main.java:51)
错误发生在我写“workbook.write(fileOut)”的行(根据堆栈跟踪),其中 fileOut 是 FileOutputStream。这意味着显然有足够的内存供所有 java 对象存储 excel 文件,但由于某种原因,当它写入硬盘时,它必须占用更多的内存。
只是告诉你,我尝试将 java 堆大小增加到 1 gig(通过添加 -Xms128m -Xmx1024m),但这似乎仍然不起作用。
救命啊! O.o
<小时/>代码示例:
..
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//I'M USING A DATABASE
import DAO.EventoDAO;
import DAO.ParametroDAO;
public class Conversion {
public static void traduzioneFile(File read, File write){
FileOutputStream fos=null;
try {
fos = new FileOutputStream(write);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (fos!=null) {
try{
Workbook wb = new XSSFWorkbook() ;
Sheet sheet = wb.createSheet();
//I'm reading from a table in a .txt file , converting values, and putting them in a table..
FileInputStream fis;
try {
fis = new FileInputStream(fileLettura);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
String line=br.readLine();
//here there are some variables
while(line!=null) {
Row row = null;
row=sheet.createRow((short)row_number);
//arrayLinea contains all the words of the line
while (column_number<arrayLinea.length){
value=arrayLinea[column_number];
//if value is ok i translate it and put it in a cell
row.createCell((short)contatoreColonne).setCellValue(value);
contatoreColonne++
}
//next line
linea=br.readLine();
row_line++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception ex){
ex.printStackTrace();
}
wb.write(fos);
fos.flush();
fos.close();
}catch (FileNotFoundException e){
}catch (IOException e){
}catch (Exception e){
}
}
}
我希望它是可读的..但是我正在扫描每一行,每列翻译值列,将它们放入单元格中...那部分没问题..我用systems.out.println测试了它^^ 但是在最后一行说“翻译完成,开始写作”之后,出现错误..
最佳答案
使用 POI 写入 .xlsx 文件会占用大量内存。 1 场演出可能还不够。
最近,Apache POI 引入了一个新的 API ( SXSSF ),它是用于写入 .xlsx 文件的流式实现。我自己还没有使用过,但也许你可以研究一下。
关于java - 尝试使用 Apache POI 写入 excel 文件导致 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8417315/