使用poi导出excel文件

一、获取数据

    private List<PmsrT2> getPmsrT2List(HttpServletRequest request) {
        try {
             String pmsProjectId = request.getParameter("pmsProjectId");   //项目主键 
             String orgCodes = request.getParameter("orgCodes");           //省份集合
             String conserveType = request.getParameter("conserveType");   //养护类型
             String roadCode = request.getParameter("roadCode");           //路线编码
             if(!StringUtils.isEmpty(pmsProjectId)){
                    String tableName = pmsProjectId;
                    String hql = "select * from "+tableName+" where 1=1 ";
                    List<String> params = new ArrayList<String>();
                    if(!StringUtils.isEmpty(orgCodes)){
                        String [] orgCodesArr = orgCodes.split(",");
                        String zwf = com.platform.common.utils.StringUtils.repeat("?", ", ", orgCodesArr.length);
                        for(int i = 0;i < orgCodesArr.length;i++) {
                            params.add(orgCodesArr[i]);
                        }
                        hql += " and prov_code in ( "+zwf+" ) ";
                    }
                    if(!StringUtils.isEmpty(conserveType)){
                        hql += " and fatype = ? ";
                        params.add(conserveType);
                    }
                    if(!StringUtils.isEmpty(roadCode)){
                        hql += " and road_code like ? ";
                        params.add('%'+roadCode+'%');
                    }
                    return super.getDao().findBySql(hql, params.toArray(), PmsrT2.class);
                }
                request.getSession().setAttribute("process", "findOk");//设置查询进度
                return null;
        } catch (Exception e) {
            return null;
        }
    }

二、设置表头样式

/**
     * @Description 设置文本cell的样式
     * @createDate 2018年8月31日 下午3:32:31
     */
    private CellStyle getCellStyle(SXSSFWorkbook wb) {
        CellStyle cellStyle = wb.createCellStyle();// 单元格样式
        Font fontstyle = wb.createFont();
        fontstyle.setBold(false);// 不加粗
        fontstyle.setFontName("宋体");// 宋体
        fontstyle.setFontHeightInPoints((short) 10);// 字体大小
        cellStyle.setFont(fontstyle);// 把字体格式添加到单元格中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);// 内容居中,添加边框
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        return cellStyle;
    }

    /**
     * @Description 添加excel标题
     * @createDate 2018年8月31日 下午3:30:42
     */
    private Sheet addTitle(Sheet sheet, CellStyle headStyle) {
        String[] head = new String[] {"省份","养护年度","区域", "地区", "路线类型", "路线类别","路线编码", "路线名称", "起点桩号","终点桩号","里程","路面宽度","AADT","养护前PQI","养护后PQI","养护前PCI","养护后PCI",
                "养护前RQI","养护后RQI","养护前RDI","养护后RDI","养护前SRI","养护后SRI","养护前PSSI","养护后PSSI","行车方向","技术等级","路面类型","养护方案分类","养护方案编码","养护方案名称","养护方案费用(万元)"};
        Row row = sheet.createRow(0);
        for (int i = 0; i < head.length; i++) {
            sheet.setColumnWidth(i, 20*256);   
            Cell cell = row.createCell(i);
            cell.setCellValue(head[i]);
            cell.setCellStyle(headStyle);
            row.setHeight((short)720);
        }
        return sheet;
    }

三、给工作簿添加查询结果值

private void setExcelValue(Sheet sheet, CellStyle cellStyle, List<PmsrT2> pmsrT2List) throws Exception {
        for (int i = 0; i < pmsrT2List.size(); i++) {
            Row row = sheet.createRow(i + 1);//第一行            
            row.createCell(0).setCellValue(pmsrT2List.get(i).getProvName());
            row.createCell(1).setCellValue(String.valueOf(pmsrT2List.get(i).getYhYear()));
            row.createCell(2).setCellValue(convert.convertAreaName(pmsrT2List.get(i).getTheArea()));
            row.createCell(3).setCellValue(convert.convertPartName(pmsrT2List.get(i).getThePart()));
            row.createCell(4).setCellValue(convert.convertLxlxName(pmsrT2List.get(i).getRoadCategory()));
            
            row.createCell(5).setCellValue(convert.convertinitGllbName(pmsrT2List.get(i).getRoadType()));                    
            row.createCell(6).setCellValue(pmsrT2List.get(i).getRoadCode());
            row.createCell(7).setCellValue(pmsrT2List.get(i).getRoadName());
            row.createCell(8).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getStartStake()));
            row.createCell(9).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getEndStake()));
            row.createCell(10).setCellValue(String.valueOf(pmsrT2List.get(i).getRoadLength()));
            
            row.createCell(11).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getRoadWith()));                    
            row.createCell(12).setCellValue(pmsrT2List.get(i).getCarAadt());
            row.createCell(13).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getPqi()));
            row.createCell(14).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getPqi2()));            
            row.createCell(15).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getPci()));
            row.createCell(16).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getPci2()));
            
            row.createCell(17).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getRqi()));                    
            row.createCell(18).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getRqi2()));
            row.createCell(19).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getRdi()));
            row.createCell(20).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getRdi2()));            
            row.createCell(21).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getSri()));
            row.createCell(22).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getSri2()));
            
            row.createCell(23).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getPssi()));                    
            row.createCell(24).setCellValue(ImportNumberStringUtils.numTwoFormat(pmsrT2List.get(i).getPssi2()));
            row.createCell(25).setCellValue(pmsrT2List.get(i).getRoadDirectName());
            row.createCell(26).setCellValue(pmsrT2List.get(i).getRoadGradeName());                
            row.createCell(27).setCellValue(pmsrT2List.get(i).getPaveTypeName());
            row.createCell(28).setCellValue(String.valueOf(pmsrT2List.get(i).getFatype()));
            row.createCell(29).setCellValue(String.valueOf(pmsrT2List.get(i).getFacode()));
            
            row.createCell(30).setCellValue(String.valueOf(pmsrT2List.get(i).getFaname()));                    
            row.createCell(31).setCellValue(String.valueOf(pmsrT2List.get(i).getPrices().divide(new BigDecimal(10000))));
            for (int j = 0; j <= 31; j++) {// 给每个单元格设置格式
                row.getCell(j).setCellStyle(cellStyle);
            }
            if (i % 500 == 0) {
                ((SXSSFSheet) sheet).flushRows();// 每500行刷新所有行
            }
        }
    }

四、输出结果流

    try {
                String fileName = "需求分析.xlsx";
                OutputStream out = response.getOutputStream();
                response.setCharacterEncoding("UTF-8");
                response.reset();
                response.addHeader("Content-Disposition",
                        " attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1"));
                response.setContentType("application/octet-stream");
                wb.write(out);
                out.close();
                wb.dispose();// 处理在磁盘上支持此工作簿的临时文件
            } catch (Exception e) {
                e.printStackTrace();
            }
            request.getSession().setAttribute("process", "finish");//设置进度 

猜你喜欢

转载自blog.csdn.net/ZHANGLIZENG/article/details/82260608