您的位置:首页 > 大数据

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);

        }

        

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: