Excel导出,特定模板,二维数组导出

效果:

@RequestMapping(value = "/exportExcelForSJSB", method = { RequestMethod.GET, RequestMethod.POST })
    public void exportExcelForSJSB(
            @RequestParam(value = "startMonth", required = false) String startMonth,
            @RequestParam(value = "endMonth", required = false) String endMonth,
            HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        EntityWrapper<Organ> entityWrapper = new EntityWrapper<Organ>();
        entityWrapper.eq("ORGANPID", "130000000000");
        entityWrapper.orderBy("ORGANORDER");
        List<Organ> organList = organServiceImpl.selectList(entityWrapper);
        List<String> cityCodeList = new ArrayList<String>();
        for(Organ o:organList){
            String cityCode = o.getOrganID();
            cityCodeList.add(cityCode);
        }
        String[] typeNames = new String[]{"LY_SLQZJB", "LY_XSQDPXC" ,"LY_SDZFJGYS","LY_QTZNBMYS","LY_QTZNBMYS",//重复了一个目的是让数组对齐,与导出的表格格式一直
                "BM_DWHZFJG","BM_XZBM","BM_CLWBZ","BM_QTBM","BM_QTBM",//重复了一个目的是让数组对齐
                "JB_SCJ","JB_XKJ","JB_CLWBZCY","JB_QT","JB_QT"};//重复了一个目的是让数组对齐
        int[][] total = new int[cityCodeList.size()+1][15];//创建14行15列数组
        for(int i=0;i<cityCodeList.size();i++){
            for(int j=0;j<typeNames.length;j++){
                int sum = sjsbMpxsServiceImpl.summaryReportByCityAndType(startMonth, endMonth, cityCodeList.get(i), typeNames[j]);
                total[i][j] = sum;
            }
        }
        //遍历二维数组
        for(int m=0;m<total.length;m++){//控制行数
            for(int n=0;n<total[m].length;n++){//一行中有多少个元素(即多少列)
                total[m][4] = total[m][0]+total[m][1]+total[m][2]+total[m][3];//来源-小计
                total[m][9] = total[m][8]+total[m][5]+total[m][6]+total[m][7];//部门-小计
                total[m][14] = total[m][12]+total[m][13]+total[m][10]+total[m][11];//级别-小计    
            }
        }
        //计算合计,先遍历列,再遍历行
        for(int j=0;j<15;j++){
            for(int i=0;i<cityCodeList.size();i++){
                total[cityCodeList.size()][j] += total[i][j];
            }
        }    
            export(request, response, total, "摸排接收涉黑涉恶腐败和“保护伞”线索情况汇总表", "countReport", 0, 4, 1, 15);
    }
        
    private static void export(HttpServletRequest request,HttpServletResponse response,
            int[][] result, String fileName,String templateName, int numSheet, int startRow, int xStart, int xEnd){
        response.reset();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String dateStr = sdf.format(new Date());
        fileName += "-" + dateStr;
        try {
            fileName = new String(fileName.getBytes(),"ISO-8859-1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 指定下载的文件名
        response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".xls" + "\"");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
//        String basePrintPath = (String) request.getSession().getAttribute("basePrintPath");
        String basePrintPath = request.getRealPath("") + "/print/";
        String template = basePrintPath + templateName + ".xls";        
        @SuppressWarnings("unchecked")
        HSSFWorkbook exportExcel = generateExcelForSJSB(result, template, numSheet, startRow, xStart, xEnd);
        
        try {
            exportExcel.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static HSSFWorkbook generateExcelForSJSB(int[][] dataArray, String template,// 模板路径
            int numSheet,// 第几个工作簿
            int startRow,// 开始行号,即记录变量的行号。起始为0
            int xStart, int xEnd) {
            InputStream is = null;
            HSSFWorkbook excel = null;
            try {
                is = new FileInputStream(template);
                excel = new HSSFWorkbook(is);
                HSSFSheet xssfSheet = excel.getSheetAt(numSheet);
                HSSFRow row = xssfSheet.getRow(startRow);               
                for(int m=0;m<dataArray.length;m++){
                    HSSFRow tempRow = xssfSheet.getRow(startRow + m);
                        for(int n=0;n<dataArray[m].length;n++){                       
                            if (row.getCell(n+1) != null) {
                                HSSFCell cell = row.getCell(n+1);
                                HSSFCell c = tempRow.createCell((short) (n+1));
                                HSSFCellStyle cellStyle= cell.getCellStyle();   
                                cellStyle.setWrapText(true);    
                                c.setCellStyle(cellStyle);
                                c.setCellValue(dataArray[m][n]);
                            }
                        }
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return excel;
        }
   

猜你喜欢

转载自blog.csdn.net/qq543539043/article/details/80234941
今日推荐