poi excel大数据导出-SXSSFWorkbook
2017-06-29 14:40
309 查看
POI之前的版本不支持大数据量处理,如果数据过多则经常报OOM错误,有时候调整JVM大小效果也不是太好。3.8版本的POI新出来了SXSSFWorkbook,可以支持大数据量的操作,只是SXSSFWorkbook只支持.xlsx格式,不支持.xls格式。
3.8版本的POI对excel的导出操作,一般只使用HSSFWorkbook以及SXSSFWorkbook,HSSFWorkbook用来处理较少的数据量,SXSSFWorkbook用来处理大数据量以及超大数据量的导出。
(1)SXSSFWorkbook的使用例子如下:
response.setContentType("applicationnd.ms-excel");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String filename = ServerInitServlet.sdf1.format(new Date()) + ".xlsx";
filename = new String(filename.getBytes("GBK"), "iso8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
String[] keyArray = { "产品名称", "品牌信息", "产品规格", "产品型号", "产品编号", "产品主条码", "单位", "安全库存", "安全库存上线", "安全库存下线",
"类别", "包装", "存储条件", "通俗名称", "产品系列", "材质", "注册证号", "成本价" };
SXSSFWorkbook wb = new SXSSFWorkbook(10000);
Sheet sh = wb.createSheet();
List<Produce> produces = null;
produces = adminDAO.allProductInfo(orderNum);
Row rowTitle = sh.createRow(0);
for (int n = 0; n < keyArray.length; n++) {
String content = keyArray
;
CellUtil.createCell(rowTitle, n, content);
}
for (int i = 0; i < produces.size(); i++) {
Produce produce = produces.get(i);
String[] contetnArr = produce.getApplyFeeItemStringArr();
Row row = sh.createRow(i+1);
for (int n = 0; n < contetnArr.length; n++) {
String content = contetnArr
;
CellUtil.createCell(row, n, content);
}
}
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
try {
bufferedOutPut.flush();
wb.write(bufferedOutPut);
bufferedOutPut.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
(2) HSSFSheet
public void createRow(String[] listName,String[] listAlign,Integer[]listColSpanLength,Short[]listFontHeight,String[] listBlod,int rowIndex) {
// align:left,center,right
if (!(listName!=null&&listName.length>0)) {
return;
}
HSSFRow row = sheet.createRow(rowIndex);
int startCol=0;
int endCol=-1;
for (int i = 0; i < listName.length; i++) {
startCol=endCol+1;
endCol=startCol+listColSpanLength[i]-1;
// 设置第一行
HSSFCell cell = row.createCell(startCol);
row.setHeightInPoints(40);
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.ENCODING_UTF_16);// 中文处理
cell.setCellValue(new HSSFRichTextString(listName[i]));
// 指定合并区域
sheet.addMergedRegion(new CellRangeAddress(rowIndex,rowIndex, startCol, endCol));
// 定义单元格格式,添加单元格表样式,并添加到工作簿
HSSFCellStyle cellStyle = wb.createCellStyle();
// 设置单元格水平对齐类型
if (listAlign[i].equals("left")) {
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左对齐
}
if (listAlign[i].equals("center")) {
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中对齐
}
if (listAlign[i].equals("right")) {
cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//右对齐
}
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中对齐
cellStyle.setWrapText(true);// 自动换行
// 设置单元格字体
HSSFFont font = wb.createFont();
if (listBlod[i].equals("normal")) {
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
}else {
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
}
font.setFontName("宋体");
font.setFontHeightInPoints(listFontHeight[i]);
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
}
}
3.8版本的POI对excel的导出操作,一般只使用HSSFWorkbook以及SXSSFWorkbook,HSSFWorkbook用来处理较少的数据量,SXSSFWorkbook用来处理大数据量以及超大数据量的导出。
(1)SXSSFWorkbook的使用例子如下:
response.setContentType("applicationnd.ms-excel");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String filename = ServerInitServlet.sdf1.format(new Date()) + ".xlsx";
filename = new String(filename.getBytes("GBK"), "iso8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
String[] keyArray = { "产品名称", "品牌信息", "产品规格", "产品型号", "产品编号", "产品主条码", "单位", "安全库存", "安全库存上线", "安全库存下线",
"类别", "包装", "存储条件", "通俗名称", "产品系列", "材质", "注册证号", "成本价" };
SXSSFWorkbook wb = new SXSSFWorkbook(10000);
Sheet sh = wb.createSheet();
List<Produce> produces = null;
produces = adminDAO.allProductInfo(orderNum);
Row rowTitle = sh.createRow(0);
for (int n = 0; n < keyArray.length; n++) {
String content = keyArray
;
CellUtil.createCell(rowTitle, n, content);
}
for (int i = 0; i < produces.size(); i++) {
Produce produce = produces.get(i);
String[] contetnArr = produce.getApplyFeeItemStringArr();
Row row = sh.createRow(i+1);
for (int n = 0; n < contetnArr.length; n++) {
String content = contetnArr
;
CellUtil.createCell(row, n, content);
}
}
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
try {
bufferedOutPut.flush();
wb.write(bufferedOutPut);
bufferedOutPut.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
(2) HSSFSheet
public void createRow(String[] listName,String[] listAlign,Integer[]listColSpanLength,Short[]listFontHeight,String[] listBlod,int rowIndex) {
// align:left,center,right
if (!(listName!=null&&listName.length>0)) {
return;
}
HSSFRow row = sheet.createRow(rowIndex);
int startCol=0;
int endCol=-1;
for (int i = 0; i < listName.length; i++) {
startCol=endCol+1;
endCol=startCol+listColSpanLength[i]-1;
// 设置第一行
HSSFCell cell = row.createCell(startCol);
row.setHeightInPoints(40);
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.ENCODING_UTF_16);// 中文处理
cell.setCellValue(new HSSFRichTextString(listName[i]));
// 指定合并区域
sheet.addMergedRegion(new CellRangeAddress(rowIndex,rowIndex, startCol, endCol));
// 定义单元格格式,添加单元格表样式,并添加到工作簿
HSSFCellStyle cellStyle = wb.createCellStyle();
// 设置单元格水平对齐类型
if (listAlign[i].equals("left")) {
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左对齐
}
if (listAlign[i].equals("center")) {
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中对齐
}
if (listAlign[i].equals("right")) {
cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//右对齐
}
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中对齐
cellStyle.setWrapText(true);// 自动换行
// 设置单元格字体
HSSFFont font = wb.createFont();
if (listBlod[i].equals("normal")) {
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
}else {
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
}
font.setFontName("宋体");
font.setFontHeightInPoints(listFontHeight[i]);
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
}
}
相关文章推荐
- 关于SXSSFWorkbook导出大批量数据的问题
- Java Excel SXSSFWorkbook大量数据导出
- 利用poi-SXSSFWorkbook导出数据
- 大数据导出POI之SXSSFWorkbook
- 关于SXSSFWorkbook导出大批量数据的问题
- SXSSFWorkbook 导出大批量数据和图片到excel
- 导出数据 org.apache.poi.hssf.usermodel.HSSFWorkbook
- POI EXCEL 数据导出
- POI 海量数据/大数据文件生成SXSSFWorkbook使用简介
- XSSFWorkbook、HSSFWorkbook导出EXCEL数据和图片
- 1.POI导出大量数据方法介绍-SXSSF
- 使用SXSSFWorkbook导出大量excel表格
- java 导出数据到excel 之 WritableWorkbook
- 将数据库中的数据导出为excel文件 HSSFWorkBooK用法
- JAVA POI HSSFWorkbook 导出Excel表格超大数据量解决方案
- HSSFWorkBook导出excel2003表数据超过65535条时报错怎么办
- HSSFWorkBooK用法【数据导出为execl】
- 3.POI SXSSF导出大量数据实例
- java 导出数据到excel 之 WritableWorkbook
- 利用SXSSFWorkbook导出excel