我有一个编写Excel文件的程序。它使用Apache POI来编写Excel 2007文件(我有超过256列,所以必须使用它)。程序可以运行。我已经在非常小的文件上测试过了,但如果我使用更多的行,就会用完内存。
以下是堆栈跟踪:
根据堆栈跟踪,错误发生在我写“workbook.write(fileOut)”的那一行,其中fileOut是FileOutputStream。这意味着显然有足够的内存来存储Excel文件的所有Java对象,但由于它在写入硬盘时必须获取更多内存,所以出现问题。
只是告诉你,我已经尝试增加Java堆大小,甚至达到1G(通过添加-Xms128m -Xmx1024m),但仍然无济于事。
救命啊!O.o
代码示例:
以下是堆栈跟踪:
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。这意味着显然有足够的内存来存储Excel文件的所有Java对象,但由于它在写入硬盘时必须获取更多内存,所以出现问题。
只是告诉你,我已经尝试增加Java堆大小,甚至达到1G(通过添加-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 测试过了 ^^ 但是在最后一行写着“翻译完成,开始写入”的时候,出现了错误...