需求:需要把500列,170000行导进excel
package vo.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestHSSF {
public static final int COLUMN = 500;
public static final int ROW = 17000;
public static void main(String[] args) throws IOException {
/*title数据准备,1000列*/
String[] titlestr=new String[COLUMN];
for(int k = 0; k < COLUMN; k++){
titlestr[k]="Title_"+k;
}
/*主体数据准备,17000行,1000列*/
List list=new ArrayList();
for(int i = 0; i < ROW; i++){
String[] str=new String[COLUMN];
for(int j = 0; j < COLUMN; j++){
str[j]="L"+i+j;
}
list.add(str);
}
outputExcel(list,titlestr);
}
public static void outputExcel(List list,String[] titlestr) throws IOException{
// 创建Excel
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet0");
ServletOutputStream out = null;
/*生成title*/
HSSFRow row = sheet.createRow(0);
HSSFCell cell;
for (int i = 0; i < titlestr.length; i++) {
cell = row.createCell(i);
cell.setCellValue(titlestr[i]);
}
/*生成数据*/
for (int i = 0; i < list.size(); i++) {
String[] str=list.get(i);
row = sheet.createRow(i+1);
for(int j = 0; j < str.length; j++){
cell = row.createCell(j);
cell.setCellValue(str[j]);
}
}
FileOutputStream fout = new FileOutputStream("F://myexcel.xls");
wb.write(fout);
fout.close();
System.out.println("==========ok");
}
}
结果:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid column index (256). Allowable column range for BIFF8 is (0..255) or ('A'..'IV')
at org.apache.poi.hssf.usermodel.HSSFCell.checkBounds(HSSFCell.java:939)
at org.apache.poi.hssf.usermodel.HSSFCell.
at org.apache.poi.hssf.usermodel.HSSFRow.createCell(HSSFRow.java:148)
at org.apache.poi.hssf.usermodel.HSSFRow.createCell(HSSFRow.java:126)
at vo.test.TestHSSF.outputExcel(TestHSSF.java:52)
at vo.test.TestHSSF.main(TestHSSF.java:37)
看来HSSFWorkbook写入的列不能超过256列。
package vo.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TestXSSF {
public static final int COLUMN = 500;
public static final int ROW = 17000;
public static void main(String[] args) throws Exception {
/*title数据准备,1000列*/
String[] titlestr=new String[COLUMN];
for(int k = 0; k < COLUMN; k++){
titlestr[k]="Title_"+k;
}
/*主体数据准备,17000行,1000列*/
List list=new ArrayList();
for(int i = 0; i < ROW; i++){
String[] str=new String[COLUMN];
for(int j = 0; j < COLUMN; j++){
str[j]="L"+i+j;
}
list.add(str);
}
/*一行行的添加*/
for(int k = 0; k < list.size(); k++){
String[] str=list.get(k);
outputExcel(titlestr,str,k);
}
}
public static void outputExcel(String[] tempTitle,String[] columnsInfo,int index) throws Exception{
/*初始*/
if(index==0){
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("sheet0");
XSSFRow firstRow = sheet.createRow(0);
XSSFCell cells[] = new XSSFCell[tempTitle.length];
//循环设置表头信息
for (int i=0;i
结果:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.io.ByteArrayOutputStream.grow(Unknown Source)
at java.io.ByteArrayOutputStream.ensureCapacity(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:180)
at org.apache.poi.xssf.usermodel.XSSFSheet.write(XSSFSheet.java:2695)
at org.apache.poi.xssf.usermodel.XSSFSheet.commit(XSSFSheet.java:2655)
at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:313)
at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:317)
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:173)
at vo.test.TestXSSF.outputExcel(TestXSSF.java:98)
at vo.test.TestXSSF.main(TestXSSF.java:48)
通过调节JVM的运行内存,问题还是出现
package vo.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
public class TestSXSSF {
public static final int COLUMN = 500;
public static final int ROW = 17000;
public static void main(String[] args) throws Exception {
/*title数据准备,1000列*/
String[] titlestr=new String[COLUMN];
for(int k = 0; k < COLUMN; k++){
titlestr[k]="Title_"+k;
}
/*主体数据准备,17000行,1000列*/
List list=new ArrayList();
for(int i = 0; i < ROW; i++){
String[] str=new String[COLUMN];
for(int j = 0; j < COLUMN; j++){
str[j]="L"+i+j;
}
list.add(str);
}
outputExcel(titlestr,list);
}
public static void outputExcel(String[] titlestr,List list) throws IOException{
/*在内存中保留100行,超过的行将刷新到磁盘*/
SXSSFWorkbook wb = new SXSSFWorkbook(100);
Sheet sh = wb.createSheet("sheet01");
//标题
Row row = sh.createRow(0);
for(int i = 0; i < titlestr.length; i++){
Cell cell = row.createCell(i);
cell.setCellValue(titlestr[i]);
}
for(int i = 0; i < list.size(); i++){
row = sh.createRow(i+1);
String[] str= list.get(i);
for(int j = 0; j < str.length; j++){
Cell cell = row.createCell(j);
cell.setCellValue(str[j]);
}
}
FileOutputStream out = new FileOutputStream("F://myexcel.xls");
wb.write(out);
out.close();
wb.dispose();
System.out.println("==========ok");
}
}
成功导出