欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java导出excel接口(附效果图)

程序员文章站 2022-06-02 11:22:32
...

java导出Excel接口

内网审批记录结果表导出excel
@RequestMapping("/excel")
@RestController
@Api(tags = "导出Excel @ 季瓜", description = "ExcelController")
public class ExcelController {

    private static final Logger loggger = LoggerFactory.getLogger(ExcelController.class);

    @Resource
    private OperationService operationService;

    @PostMapping("/excelDownload")
    @ApiOperation("导出excel")
    public void excelDownload(HttpServletResponse response, @RequestBody SelectUser selectUser) {

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("审批记录导出");
		
		//获取需要导出的数据
        List<ReasonDTO> list = operationService.getAllOperation(selectUser.getTimeStart(),selectUser.getTimeEnd());

        int insertRows = 0;

        for (int i = 0 ; i < list.size() ; i++){
            Row titileRow = sheet.createRow(insertRows);
            titileRow.createCell(0).setCellValue("申请人:");
            titileRow.createCell(1).setCellValue(list.get(i).getName());
            titileRow.createCell(2).setCellValue("审批人:");
            titileRow.createCell(3).setCellValue(list.get(i).getApprovalName());
            titileRow.createCell(4).setCellValue("审批时间:");
            titileRow.createCell(5).setCellValue(list.get(i).getApprovalTime());
            CellRangeAddress rangeAddress = new CellRangeAddress(insertRows,insertRows,5,6);
            sheet.addMergedRegion(rangeAddress);
            titileRow.createCell(8).setCellValue("审批结果:");
            titileRow.createCell(9).setCellValue(list.get(i).getApprovalResult());
            insertRows++;

            Row titileRow1 = sheet.createRow(insertRows);
            titileRow1.createCell(0).setCellValue("文件名:");
            CellRangeAddress rangeAddress1 = new CellRangeAddress(insertRows,insertRows,1,2);
            sheet.addMergedRegion(rangeAddress1);
            titileRow1.createCell(1).setCellValue(list.get(i).getFileName());
            titileRow1.createCell(3).setCellValue("文件大小:");
            CellRangeAddress rangeAddress2 = new CellRangeAddress(insertRows,insertRows,4,6);
            sheet.addMergedRegion(rangeAddress2);
            titileRow1.createCell(4).setCellValue(list.get(i).getFileSize());
            insertRows++;

            Row titileRow2 = sheet.createRow(insertRows);
            titileRow2.createCell(0).setCellValue("申请原因:");
            CellRangeAddress rangeAddress3 = new CellRangeAddress(insertRows,insertRows,1,9);
            sheet.addMergedRegion(rangeAddress3);
            titileRow2.createCell(1).setCellValue(list.get(i).getReason());
            insertRows++;

            CellRangeAddress rangeAddress4 = new CellRangeAddress(insertRows,insertRows,0,9);
            sheet.addMergedRegion(rangeAddress4);

            insertRows++;
        }


        OutputStream outputStream = null;

        try {
            String fileName = "审批记录";
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls","utf-8"));
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            workbook.close();
            loggger.info("导出文件:" + DateUtil.dataToString(new Date()));
        } catch (Exception e) {
            loggger.error("导出文件错误:" + e.getMessage());
        }
    }
效果图

java导出excel接口(附效果图)