Java导出Excel(项目实战Demo)

2020-01-02 08:40:13来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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,
                                                     HttpServletRequest request,
                                                     HttpServletResponse response) 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;
    @Autowired
    private SysLogMapper sysLogMapper;
    @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());
        }
    }
}


原文链接:https://www.cnblogs.com/RickQi/p/java-dao-chuexcel-xiang-mu-shi-zhandemo.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:记一次线上问题 → 事务去哪了

下一篇:成长计划设计方案&#183;补充