编程语言
首页 > 编程语言> > 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,
                                                     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());
        }
    }
}

标签:rows,Java,titleRow,Demo,Excel,attendMachineCard,getCell,sheet,setCellValue
来源: https://www.cnblogs.com/RickQi/p/java-dao-chuexcel-xiang-mu-shi-zhandemo.html