POI生成excel文件

 EntranceGuardUploadEventModel eventModel = new EntranceGuardUploadEventModel();
        List<EntranceGuardUploadEvent> dataList = entranceGuardUploadEventService.queryAll(eventModel);
        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = workbook.createSheet(I18NUtil.getMessage(request, "employee.iodata.exportreport"));
        // 第三步,在sheet中添加表头第0行
        HSSFRow row = sheet.createRow(0);
        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
        row.createCell(0).setCellValue(I18NUtil.getMessage(request, "employee.label.employee.id"));
        row.createCell(1).setCellValue(I18NUtil.getMessage(request, "employee.label.employee.name"));
        row.createCell(2).setCellValue(I18NUtil.getMessage(request, "table.coloum.depart.name"));
        row.createCell(3).setCellValue(I18NUtil.getMessage(request, "employee.label.cardno"));
        row.createCell(4).setCellValue(I18NUtil.getMessage(request, "queryCondition.cardtype.describe"));
        row.createCell(5).setCellValue(I18NUtil.getMessage(request, "queryCondition.doorarea.name.select"));
        row.createCell(6).setCellValue(I18NUtil.getMessage(request, "queryCondition.door.name.select"));
        row.createCell(7).setCellValue(I18NUtil.getMessage(request, "queryCondition.doorstatus.describe"));
        row.createCell(8).setCellValue(I18NUtil.getMessage(request, "report.iotime.statustime"));
        //设置表头居中
        row.getCell(0).setCellStyle(style);
        row.getCell(1).setCellStyle(style);
        row.getCell(2).setCellStyle(style);
        row.getCell(3).setCellStyle(style);
        row.getCell(4).setCellStyle(style);
        row.getCell(5).setCellStyle(style);
        row.getCell(6).setCellStyle(style);
        row.getCell(7).setCellStyle(style);
        row.getCell(8).setCellStyle(style);

        //设置列宽
        sheet.setColumnWidth(0, 2000);
        sheet.setColumnWidth(1, 2500);
        sheet.setColumnWidth(2, 5000);
        sheet.setColumnWidth(3, 6000);
        sheet.setColumnWidth(4, 3000);
        sheet.setColumnWidth(5, 5000);
        sheet.setColumnWidth(6, 5000);
        sheet.setColumnWidth(7, 3000);
        sheet.setColumnWidth(8, 5000);
        //格式化日期 pattern
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
        // 第五步,写入实体数据
        for (int i = 0; i < dataList.size(); i++) {
            row = sheet.createRow(i + 1);
            // 第六步,创建单元格,并设置值
            row.createCell(0).setCellValue(dataList.get(i).getEmployeeId());
            row.createCell(1).setCellValue(dataList.get(i).getEmployeeName());
            row.createCell(2).setCellValue(dataList.get(i).getName());//部门
            row.createCell(3).setCellValue(dataList.get(i).getSerial());
            //卡类型
            String key = dataList.get(i).getCardType() == null ? "" : dataList.get(i).getCardType().toString();
            String cardDescribe;
            if (key != "") {
                cardDescribe = constantTableCache.getConstantValue(ConstantTableCache.ConstantTableName.CARD_TYPE, Integer.parseInt(key));
            } else cardDescribe = "";
            row.createCell(4).setCellValue(cardDescribe);
            row.createCell(5).setCellValue(dataList.get(i).getAreaName());
            row.createCell(6).setCellValue(dataList.get(i).getDoorName());
            String doorStatusKey = dataList.get(i).getDoorStatus() == null ? "" : dataList.get(i).getDoorStatus().toString();
            String doorStatusDescribe;
            if (doorStatusKey != "") {
                doorStatusDescribe = constantTableCache.getConstantValue(ConstantTableCache.ConstantTableName.DOOR_STATUS_TYPE, Integer.parseInt(doorStatusKey));
            } else doorStatusDescribe = "";
            row.createCell(7).setCellValue(doorStatusDescribe);
            row.createCell(8).setCellValue(format.format(dataList.get(i).getEventDate()));
        }
        // 第七步,将文件存到指定位置
        try {
            String savePath = "E:/xxx.xls";
            FileOutputStream fout = new FileOutputStream(savePath);
            workbook.write(fout);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值