Java导出Excel(项目实战Demo)

Java导出Excel(项目实战Demo)

Controller 层

/**
     * 考勤机刷卡明细导出Excel
     *
     * @throws Exception
     */
//    @RequiresPermissions("report:ExportToExcel")
    @RequestMapping(value = "/AttendMachineCardDetailsExportToExcel", method = RequestMethod.POST)
    public void attendMachineCardDetailsExportToExcel(@RequestParam(value = "workTimeDateStart", required = false) String workTimeDateStart,
                                                     @RequestParam(value = "workTimeDateEnd", required = false) String workTimeDateEnd,
                                                     @RequestParam(value = "userName", required = false) String userName,
                                                     @RequestParam(value = "userType", required = false) String userType,
                                                    ) throws Exception {

        AttendMachineCard attendMachineCard = new AttendMachineCard();
        attendMachineCard.setWorkTimeDateStart(workTimeDateStart);
        attendMachineCard.setWorkTimeDateEnd(workTimeDateEnd);
        attendMachineCard.setUserName(userName);
        attendMachineCard.setUserType(userType);
        List<AttendMachineCard> list = attendMachineCardServiceImpl.getAttendMachineCardList(attendMachineCard);//用于查询,返回结果
        attendMachineCardServiceImpl.exportAttendMachineCardDetails(list, request, response);//导出Excle
    }

ServiceImpl

@Service
public class AttendMachineCardServiceImpl implements AttendMachineCardService{
    @Autowired
    private AttendMachineCardMapper attendMachineCardMapper;
    @Override
    public List<AttendMachineCard> getAttendMachineCardList(AttendMachineCard attendMachineCard) {
        List<AttendMachineCard> attendMachineCardList = null;
        try {
            attendMachineCardList = attendMachineCardMapper.getAttendMachineCardList(attendMachineCard);
        } catch (Exception e) {
            throw new OutsideSystemServiceException(ResultEnum.SEARCH_ATTEND_MACHINE_CARD_ERROR,e);
        }
    return attendMachineCardList;
    }

    @Override
    public void exportAttendMachineCardDetails(List<AttendMachineCard> attendMachineCard, HttpServletRequest request, HttpServletResponse response) throws IOException {
        // set content attributes for the response
        try {
            this.logexportLogDetails(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=人员考勤信息统计表.xlsx");
        // 创建工作薄
        try (XSSFWorkbook workbook = new XSSFWorkbook();) {
            // 创建工作表
            XSSFSheet sheet = workbook.createSheet("人员考勤信息统计表");
            createExcelContentDetails(attendMachineCard, sheet);
            workbook.write(response.getOutputStream());
        }
        response.flushBuffer();
    }
    
 
    private void createExcelContentDetails(List<AttendMachineCard> attendMachineCards,XSSFSheet sheet){
        XSSFRow titleRow = sheet.createRow(0);
        for (int col = 0; col < 7; col++) {
            // 向工作表中添加数据
            titleRow.createCell(col);
        }
        titleRow.getCell(0).setCellValue("姓名");
        titleRow.getCell(1).setCellValue("人员类别");
        titleRow.getCell(2).setCellValue("刷卡日期");
        titleRow.getCell(3).setCellValue("刷卡时间");
        titleRow.getCell(4).setCellValue("签到类型");
        titleRow.getCell(5).setCellValue("签到方式");
        titleRow.getCell(6).setCellValue("设备编号");

        sheet.setColumnWidth(0, 30 * 256);
        sheet.setColumnWidth(1, 30 * 256);
        sheet.setColumnWidth(2, 30 * 256);
        sheet.setColumnWidth(3, 30 * 256);
        sheet.setColumnWidth(4, 30 * 256);
        sheet.setColumnWidth(5, 30 * 256);
        sheet.setColumnWidth(6, 30 * 256);

        // 数据行
        for (int row = 1; row < attendMachineCards.size() + 1; row++) {
            XSSFRow rows = sheet.createRow(row);
            for (int col = 0; col < 7; col++) {
                rows.createCell(col);
            }
            AttendMachineCard attendMachineCard = attendMachineCards.get(row - 1);
            rows.getCell(0).setCellValue(attendMachineCard.getUserName());
            rows.getCell(1).setCellValue(attendMachineCard.getUserType());
            rows.getCell(2).setCellValue(attendMachineCard.getPunckDate());
            rows.getCell(3).setCellValue(attendMachineCard.getPunckTime());
            rows.getCell(4).setCellValue(attendMachineCard.getSignType());
            rows.getCell(5).setCellValue(attendMachineCard.getWayOfSigningIn());
            rows.getCell(6).setCellValue(attendMachineCard.getNumber());
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值