File f = new File(sFilePath);
HSSFSheet sheet[] = null;
HSSFWorkbook wb = null;
if(!f.exists())
f.createNewFile();
FileOutputStream fileOut = new FileOutputStream(sFilePath, false);
if(wb == null)
{
wb = new HSSFWorkbook();
sheet = new HSSFSheet[1];
sheet[0] = wb.createSheet();
wb.setSheetName(0, "存货管理档案表", HSSFWorkbook.ENCODING_UTF_16);
}
/********************************************************************/
HSSFCellStyle cs = wb.createCellStyle();//创建一个style
HSSFFont font = wb.createFont();//创建一个字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //粗体
cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
cs.setFont(font);
/**添加列标题**********************************************************/
HSSFRow row = sheet[0].getRow(0);
if(row == null)
row = sheet[0].createRow(0);
for(short i = 0; i < tableColCaption.length; i++)
{
HSSFCell cell = row.getCell(i);
if(cell == null)
cell = row.createCell(i);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cs);
cell.setCellType(1);
cell.setCellValue(tableColCaption[i]);
}
/*********************************************************************/
/*****写入表体信息**********************************************************/
for(int i=0;i<count;i++){
String[] rowInfo = (String[])tableData.elementAt(i);
HSSFRow arow = sheet[0].getRow((int)(i+1));
if(arow == null)
arow = sheet[0].createRow((short)(i+1));
for(short j = 0; j < rowInfo.length; j++)
{
HSSFCell cell = arow.getCell(j);
if(cell == null)
cell = arow.createCell(j);
String strCellValue = rowInfo[j];
if(strCellValue == null){
cell.setCellType(1);
cell.setCellValue("");
}
else{
cell.setCellType(1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(strCellValue);
}
}
}
/*********************************************************************/
wb.write(fileOut);
fileOut.close();
if (m_iCurPanel == BillMode.List) {
showHintMessage("文件导出已完成!");
this.onRefresh();
}
} catch (Exception e) {
e.printStackTrace();
showHintMessage("导出出错");
showWarningMessage("导出出错:"+ e.getMessage() + ",文件路径:" + sFilePath);
}
超过3000就报错了, wb.write(fileOut);报错OutofMemory怎么解决!
HSSFSheet sheet[] = null;
HSSFWorkbook wb = null;
if(!f.exists())
f.createNewFile();
FileOutputStream fileOut = new FileOutputStream(sFilePath, false);
if(wb == null)
{
wb = new HSSFWorkbook();
sheet = new HSSFSheet[1];
sheet[0] = wb.createSheet();
wb.setSheetName(0, "存货管理档案表", HSSFWorkbook.ENCODING_UTF_16);
}
/********************************************************************/
HSSFCellStyle cs = wb.createCellStyle();//创建一个style
HSSFFont font = wb.createFont();//创建一个字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //粗体
cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
cs.setFont(font);
/**添加列标题**********************************************************/
HSSFRow row = sheet[0].getRow(0);
if(row == null)
row = sheet[0].createRow(0);
for(short i = 0; i < tableColCaption.length; i++)
{
HSSFCell cell = row.getCell(i);
if(cell == null)
cell = row.createCell(i);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cs);
cell.setCellType(1);
cell.setCellValue(tableColCaption[i]);
}
/*********************************************************************/
/*****写入表体信息**********************************************************/
for(int i=0;i<count;i++){
String[] rowInfo = (String[])tableData.elementAt(i);
HSSFRow arow = sheet[0].getRow((int)(i+1));
if(arow == null)
arow = sheet[0].createRow((short)(i+1));
for(short j = 0; j < rowInfo.length; j++)
{
HSSFCell cell = arow.getCell(j);
if(cell == null)
cell = arow.createCell(j);
String strCellValue = rowInfo[j];
if(strCellValue == null){
cell.setCellType(1);
cell.setCellValue("");
}
else{
cell.setCellType(1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(strCellValue);
}
}
}
/*********************************************************************/
wb.write(fileOut);
fileOut.close();
if (m_iCurPanel == BillMode.List) {
showHintMessage("文件导出已完成!");
this.onRefresh();
}
} catch (Exception e) {
e.printStackTrace();
showHintMessage("导出出错");
showWarningMessage("导出出错:"+ e.getMessage() + ",文件路径:" + sFilePath);
}
超过3000就报错了, wb.write(fileOut);报错OutofMemory怎么解决!
解决办法
a:改成3k就write一次(最好是2k或者2k5)
b:把你的虚拟机内存改大,改成512再试试
String[] rowInfo = (String[])tableData.elementAt(i);
HSSFRow arow = sheet[0].getRow((int)(i+1));
if(arow == null)
arow = sheet[0].createRow((short)(i+1));
for(short j = 0; j < rowInfo.length; j++)
{
HSSFCell cell = arow.getCell(j);
if(cell == null)
cell = arow.createCell(j);
String strCellValue = rowInfo[j];
if(strCellValue == null){
cell.setCellType(1);
cell.setCellValue("");
}
else{
cell.setCellType(1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(strCellValue);
}
}
if(i==2000)
{
wb.write(fileOut);
}
}
/*********************************************************************/
if(count<2000)
{
wb.write(fileOut);
} fileOut.close();
这么写对吗?
感觉可能你不是在write的时候出的问题
可能是在创建的时候报错
你把内存改大试试?
-Xms512m
-Xmx1024m