导出excel

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;



//controller

HSSFWorkbook wb =evaluateService.getEvaluationExportData(type,orgId,startTime,endTime);

exportService.exportExcel(wb,response, CommonConstants.EVALUATION_EXPORT_FILE_NAME);

//exportservice


@Override

public void exportExcel(HSSFWorkbook wb, HttpServletResponse response, String fileName) {

if(fileName ==null && fileName ==""){

throw new CommonException(ErrorCodeEnum.LACK_FILE_NAME);

    }

try{

OutputStream output=response.getOutputStream();

        response.reset();

        response.setHeader("Content-disposition", "attachment; filename=" +new String(fileName.getBytes("UTF-8"),"iso-8859-1"));

        response.setContentType("application/msexcel");

        wb.write(output);

        output.close();

    }catch (IOException e){

throw new CommonException(ErrorCodeEnum.EXCEL_EXPORT_FAILED);

    }

}

public HSSFWorkbookgetEvaluationExportData(String type, String orgId, Date startTime, Date endTime) {

HSSFWorkbook wb =new HSSFWorkbook();

    List evaluationExportVOS =evaluateMapper.getEvaluationExportData(type,orgId,RequestContextUtil.getUserId(),startTime,endTime,CommonConstants.DICT_TYPE_POST_LABEL);

    if(evaluationExportVOS.size() ==0){

HSSFSheet sheet = wb.createSheet("0");

        HSSFRow row = sheet.createRow(0);

        row.createCell(0).setCellValue("无数据");

        return wb;

    }

List templetId =new ArrayList<>();

    for(EvaluationExportVO evaluationExportVO : evaluationExportVOS){

templetId.add(evaluationExportVO.getTempletId());

    }

templetId = CommonFunction.removeDuplicate(templetId);

    HSSFSheet sheet[] =new HSSFSheet[templetId.size()+1];

    for (int i =0; i < templetId.size(); i++) {

sheet[i] = wb.createSheet("评价模块" + i);

        sheet[i].setDefaultColumnWidth(30);

        List evaluationExportVOS1 =new ArrayList<>();

        for(EvaluationExportVO evaluationExportVO : evaluationExportVOS){

if(evaluationExportVO.getTempletId().equals(templetId.get(i))){

evaluationExportVOS1.add(evaluationExportVO);

            }

}

HSSFRow row[] =new HSSFRow[evaluationExportVOS1.size() +1];

        int rowNum =1;

        EvaluationTempleteVO evaluationTempleteVO =scoreTempleteMapper.selectTempletById(templetId.get(i));

        row[0] = sheet[i].createRow(0);

        row[0].createCell(0).setCellValue("任务名称");

        row[0].createCell(1).setCellValue("是否评价");

        row[0].createCell(2).setCellValue("评价人");

        row[0].createCell(3).setCellValue("任务负责人");

        row[0].createCell(4).setCellValue("任务完成时间");

        row[0].createCell(5).setCellValue("评价时间");

        row[0].createCell(6).setCellValue("评价标签");

        row[0].createCell(7).setCellValue("总分");

        row[0].createCell(8).setCellValue("评语");

        row[0].createCell(9).setCellValue(CommonFunction.subString(evaluationTempleteVO.getaName(), CommonConstants.EVALUATION_NAME_SEPARATOR).equals(CommonConstants.NON_EVALUATION_SUB_ITEM) ?null : CommonFunction.subString(evaluationTempleteVO.getaName(), CommonConstants.EVALUATION_NAME_SEPARATOR));

        row[0].createCell(10).setCellValue(CommonFunction.subString(evaluationTempleteVO.getbName(), CommonConstants.EVALUATION_NAME_SEPARATOR).equals(CommonConstants.NON_EVALUATION_SUB_ITEM) ?null : CommonFunction.subString(evaluationTempleteVO.getbName(), CommonConstants.EVALUATION_NAME_SEPARATOR));

        row[0].createCell(11).setCellValue(CommonFunction.subString(evaluationTempleteVO.getcName(), CommonConstants.EVALUATION_NAME_SEPARATOR).equals(CommonConstants.NON_EVALUATION_SUB_ITEM) ?null : CommonFunction.subString(evaluationTempleteVO.getcName(), CommonConstants.EVALUATION_NAME_SEPARATOR));

        row[0].createCell(12).setCellValue(CommonFunction.subString(evaluationTempleteVO.getdName(), CommonConstants.EVALUATION_NAME_SEPARATOR).equals(CommonConstants.NON_EVALUATION_SUB_ITEM) ?null : CommonFunction.subString(evaluationTempleteVO.getdName(), CommonConstants.EVALUATION_NAME_SEPARATOR));

        row[0].createCell(13).setCellValue(CommonFunction.subString(evaluationTempleteVO.geteName(), CommonConstants.EVALUATION_NAME_SEPARATOR).equals(CommonConstants.NON_EVALUATION_SUB_ITEM) ?null : CommonFunction.subString(evaluationTempleteVO.geteName(), CommonConstants.EVALUATION_NAME_SEPARATOR));

        row[0].createCell(14).setCellValue(CommonFunction.subString(evaluationTempleteVO.getfName(), CommonConstants.EVALUATION_NAME_SEPARATOR).equals(CommonConstants.NON_EVALUATION_SUB_ITEM) ?null : CommonFunction.subString(evaluationTempleteVO.getfName(), CommonConstants.EVALUATION_NAME_SEPARATOR));

        row[0].createCell(15).setCellValue(CommonFunction.subString(evaluationTempleteVO.getgName(), CommonConstants.EVALUATION_NAME_SEPARATOR).equals(CommonConstants.NON_EVALUATION_SUB_ITEM) ?null : CommonFunction.subString(evaluationTempleteVO.getgName(), CommonConstants.EVALUATION_NAME_SEPARATOR));

        row[0].createCell(16).setCellValue(CommonFunction.subString(evaluationTempleteVO.gethName(), CommonConstants.EVALUATION_NAME_SEPARATOR).equals(CommonConstants.NON_EVALUATION_SUB_ITEM) ?null : CommonFunction.subString(evaluationTempleteVO.gethName(), CommonConstants.EVALUATION_NAME_SEPARATOR));

        row[0].createCell(17).setCellValue(CommonFunction.subString(evaluationTempleteVO.getiName(), CommonConstants.EVALUATION_NAME_SEPARATOR).equals(CommonConstants.NON_EVALUATION_SUB_ITEM) ?null : CommonFunction.subString(evaluationTempleteVO.getiName(), CommonConstants.EVALUATION_NAME_SEPARATOR));

        row[0].createCell(18).setCellValue(CommonFunction.subString(evaluationTempleteVO.getjName(), CommonConstants.EVALUATION_NAME_SEPARATOR).equals(CommonConstants.NON_EVALUATION_SUB_ITEM) ?null : CommonFunction.subString(evaluationTempleteVO.getjName(), CommonConstants.EVALUATION_NAME_SEPARATOR));

        for(EvaluationExportVO evaluationExportVO : evaluationExportVOS1){

row[rowNum] = sheet[i].createRow(rowNum);

                row[rowNum].createCell(0).setCellValue(evaluationExportVO.getTaskName());

                row[rowNum].createCell(1).setCellValue("已评价");

                row[rowNum].createCell(2).setCellValue(evaluationExportVO.getEvaluatePersonName());

                row[rowNum].createCell(3).setCellValue(evaluationExportVO.getTaskResponsiblePersonName());

                row[rowNum].createCell(4).setCellValue(CommonFunction.getStringDate(evaluationExportVO.getFinishedTime()));

                row[rowNum].createCell(5).setCellValue(CommonFunction.getStringDate(evaluationExportVO.getEvaluateTime()));

                row[rowNum].createCell(6).setCellValue(evaluationExportVO.getPostLabelName());

                row[rowNum].createCell(7).setCellValue(evaluationExportVO.getTotalScore());

                row[rowNum].createCell(8).setCellValue(evaluationExportVO.getEvaluationInf());

                row[rowNum].createCell(9).setCellValue(String.valueOf(evaluationExportVO.getaScore()).equals(CommonConstants.NON_EVALUATION_SCORE)?null : String.valueOf(evaluationExportVO.getaScore()));

                row[rowNum].createCell(10).setCellValue(String.valueOf(evaluationExportVO.getbScore()).equals(CommonConstants.NON_EVALUATION_SCORE)?null : String.valueOf(evaluationExportVO.getbScore()));

                row[rowNum].createCell(11).setCellValue(String.valueOf(evaluationExportVO.getcScore()).equals(CommonConstants.NON_EVALUATION_SCORE)?null : String.valueOf(evaluationExportVO.getcScore()));

                row[rowNum].createCell(12).setCellValue(String.valueOf(evaluationExportVO.getdScore()).equals(CommonConstants.NON_EVALUATION_SCORE)?null : String.valueOf(evaluationExportVO.getdScore()));

                row[rowNum].createCell(13).setCellValue(String.valueOf(evaluationExportVO.geteScore()).equals(CommonConstants.NON_EVALUATION_SCORE)?null : String.valueOf(evaluationExportVO.geteScore()));

                row[rowNum].createCell(14).setCellValue(String.valueOf(evaluationExportVO.getfScore()).equals(CommonConstants.NON_EVALUATION_SCORE)?null : String.valueOf(evaluationExportVO.getfScore()));

                row[rowNum].createCell(15).setCellValue(String.valueOf(evaluationExportVO.getgScore()).equals(CommonConstants.NON_EVALUATION_SCORE)?null : String.valueOf(evaluationExportVO.getgScore()));

                row[rowNum].createCell(16).setCellValue(String.valueOf(evaluationExportVO.gethScore()).equals(CommonConstants.NON_EVALUATION_SCORE)?null : String.valueOf(evaluationExportVO.gethScore()));

                row[rowNum].createCell(17).setCellValue(String.valueOf(evaluationExportVO.getiScore()).equals(CommonConstants.NON_EVALUATION_SCORE)?null : String.valueOf(evaluationExportVO.getiScore()));

                row[rowNum].createCell(18).setCellValue(String.valueOf(evaluationExportVO.getjScore()).equals(CommonConstants.NON_EVALUATION_SCORE)?null : String.valueOf(evaluationExportVO.getjScore()));

                rowNum++;

        }

}

sheet[templetId.size()] = wb.createSheet("未完成评价任务");

    sheet[templetId.size()].setDefaultColumnWidth(30);

    List evaluationExportVOS2 =evaluateMapper.getNotEvaluationExportData(type,orgId,RequestContextUtil.getUserId(),startTime,endTime);

    HSSFRow row[] =new HSSFRow[evaluationExportVOS2.size() +1];

    row[0] = sheet[templetId.size()].createRow(0);

    row[0].createCell(0).setCellValue("任务名称");

    row[0].createCell(1).setCellValue("是否评价");

    row[0].createCell(2).setCellValue("评价人");

    row[0].createCell(3).setCellValue("任务完成时间");

    row[0].createCell(4).setCellValue("任务负责人");

    row[0].createCell(5).setCellValue("评价标签");

    int rowNum =1;

    for(EvaluationExportVO evaluationExportVO: evaluationExportVOS2){

if(evaluationExportVO.getIsEvaluate() ==0){

row[rowNum] =sheet[templetId.size()].createRow(1);

            row[rowNum].createCell(0).setCellValue(evaluationExportVO.getTaskName());

            row[rowNum].createCell(1).setCellValue("尚未评价");

            row[rowNum].createCell(2).setCellValue(evaluationExportVO.getEvaluatePersonName());

            row[rowNum].createCell(3).setCellValue(CommonFunction.getStringDate(evaluationExportVO.getFinishedTime()));

            row[rowNum].createCell(4).setCellValue(evaluationExportVO.getTaskResponsiblePersonName());

            row[rowNum].createCell(5).setCellValue(evaluationExportVO.getPostLabelName());

            rowNum++;

        }

}

return wb;

}

你可能感兴趣的:(导出excel)