导出行列数量不确定的表格

一、报表导出时候部门和年份都是不确定的,也就是说行列数不确定,所以我之前写的工具类不太适合这种表格的导出,下面我写了一个比较灵活的表格导出方法,具体到每一个单元格,比较灵活,有不足之处还望批评指正。

在这里插入图片描述

二、JAVA代码

@Override
    public void entryYearsDistribution3(String exportType,String end, HttpServletResponse response) throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("入职年限分布表");
        CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框  
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框  
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框  
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框  
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中

        DepartmentInfo departmentInfo = new DepartmentInfo();
        List<DepartmentInfo> departmentInfos = departmentInfoDAO.selectDepartmentList(departmentInfo);
        //左上角第一个单元格
        HSSFRow row = sheet.createRow(0);
        Short s = 540;
        row.setHeight(s);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("行标签");
        cell.setCellStyle(cellStyle);
        //写入左侧第一列的部门名称
        for (int i = 0; i < departmentInfos.size(); i++) {
            row = sheet.createRow(i+1);
            row.setHeight(s);
            cell = row.createCell(0);
            cell.setCellValue(departmentInfos.get(i).getDepartmentName());
            cell.setCellStyle(cellStyle);
        }
        //创建最后一行
        HSSFRow lastRow = sheet.createRow(departmentInfos.size());
        cell = row.createCell(0);
        cell.setCellValue("合计");
        cell.setCellStyle(cellStyle);
        //查询出年份的集合,可以知道有多少列
        List<String> yearList = calaulateYear(end);
        int departmentCount=0;
        for (int i = 1; i <yearList.size()+1; i++) {
            //i代表某一列,给此列的表头设置年份
            row = sheet.getRow(0);
            cell = row.createCell(i);
            cell.setCellValue(yearList.get(i-1));
            cell.setCellStyle(cellStyle);
            //查询出每个列要有多少单元格
            List<DepartmentInfo> departmentInfoByMonth = null;
            String year = yearList.get(i - 1);
            String realYear = year.substring(0, 4);
            String lastYear = end.substring(0, 4);
            if(exportType.equals("1")) {     //包含离职员工
                if(realYear.equals(2013)){   //2013年的要单独拿出来查
                    departmentInfoByMonth = departmentInfoDAO.countNumberByYearBefore("2013");
                }if(realYear.equals(lastYear)){
                    departmentInfoByMonth = departmentInfoDAO.countNumberByLastYear(end);
                } else {
                    departmentInfoByMonth = departmentInfoDAO.countNumberByYear(realYear);
                }
            }else {                         //不包含离职员工
                if(realYear.equals(2013)) {
                    departmentInfoByMonth = departmentInfoDAO.countNumberByYearBefore2("2013");
                }if(realYear.equals(lastYear)){
                    departmentInfoByMonth = departmentInfoDAO.countNumberByLastYear2(end);
                }else {
                    departmentInfoByMonth = departmentInfoDAO.countNumberByYear2(realYear);
                }
            }
            departmentCount = departmentInfoByMonth.size();
            //创建整列,外层i的值是某一列,里面j的值是每一行
            BigDecimal total = new BigDecimal(0);
            for (int j = 0; j < departmentInfoByMonth.size(); j++) {
                row = sheet.getRow(j + 1);       //获取表头下第一行
                if ( row == null) {                       //如果行不存在,创建行
                    row = sheet.createRow(j+1);
                }
                else {
                    row = sheet.getRow(j+1);    //否则,获取此行
                }
                cell = row.createCell(i);                 //在此行创建除去表左侧表头的第一列的单元格
                String numbers = departmentInfoByMonth.get(j).getNumbers();
                cell.setCellValue(numbers);               //给此单元格设值
                cell.setCellStyle(cellStyle);
                total = total.add(new BigDecimal(numbers)); //将此单元格的值加到此列的总值变量total中
            }
            //将合计放入此列的最后一个单元格中
            cell = lastRow.createCell(i);
            cell.setCellValue(total.toString());
            cell.setCellStyle(cellStyle);
        }
        //设置第一行最后一个单元格为“合计”
        row = sheet.getRow(0);
        cell = row.createCell(yearList.size()+1);
        cell.setCellValue("合计");
        cell.setCellStyle(cellStyle);
        //写入最后一列所有单元格的合计
        for (int j = 1; j <= departmentCount+1; j++) {
            BigDecimal total = new BigDecimal(0);
            for (int i = 1; i < yearList.size()+1; i++) {
                row = sheet.getRow(j);
                total = total.add(new BigDecimal(row.getCell(i).getStringCellValue()));
            }
            cell = sheet.getRow(j).createCell(yearList.size()+1);
            cell.setCellValue(total.toString());
            cell.setCellStyle(cellStyle);
        }
        
        OutputStream output = response.getOutputStream();
        response.reset();
        //导出文件名设置时间拼接
        String fileName = URLEncoder.encode("入职年限分布表");
        response.reset();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Disposition",
                "attachment; filename=\"" + fileName + ".xls\"");
        wb.write(output);
        output.close();
    }
发布了55 篇原创文章 · 获赞 4 · 访问量 3152

猜你喜欢

转载自blog.csdn.net/qq_41347385/article/details/103453451
今日推荐