poi导出excel2007版本

/**
     * 导出excel2007版本
     * 
     * @param titles
     *            表头集合
     * @param sheetNames
     *            sheet名称
     * @param datas
     *            数据集合
     * @param fileName
     *            文件名字
     * @param response
     *            输出流
     * @return String
     */
    public static String exportExcel07(List<List<String>> titles, List<String> sheetNames, List<List<List<String>>> datas,
            String fileName, HttpServletResponse response) {
        
        SXSSFWorkbook wb = new SXSSFWorkbook();
        CellStyle style = POIUtils.getCellStyleFont(wb, null);
        DataFormat format = wb.createDataFormat();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style.setDataFormat(format.getFormat("@"));
        for (int i = 0; i < titles.size(); i++) {
            List<String> title = titles.get(i);

            Sheet sheet = wb.createSheet(sheetNames.get(i));

            Row row = sheet.createRow((int) 0);

            for (int j = 0; j < title.size(); j++) {
                Cell cell = row.createCell(j);
                cell.setCellStyle(style);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(title.get(j));
            }
            int size = datas.size();
            if(size>0){
                List<List<String>> data = datas.get(i);
                int k = 1;
                for (int j = 0; j < data.size(); j++) {
                    row = sheet.getRow(k + j);
                    if (null == row) {
                        row = sheet.createRow(k + j);
                    }
                    
                    List<String> data_ = data.get(j);
                    
                    for (int l = 0; l < data_.size(); l++) {
                        Cell cell = row.createCell(l);
                        
                        cell.setCellStyle(style);
                        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                        cell.setCellValue(data_.get(l));
                    }
                }
            }
        }

        try {
            //response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.reset();
        //    response.setContentType("application/vnd.ms-excel;charset=ISO8859-1");
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso8859-1"));
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            try {
                OutputStream os = response.getOutputStream();
                wb.write(os);
                os.flush();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

posted @ 2019-10-16 15:28  十月围城小童鞋  阅读(2276)  评论(0编辑  收藏  举报