java导出excel表格

		// 查询所有数据装在list里
        List<DeathRecordView> all = deathRecordViewDAO.findAll();
        if (all.isEmpty()) {
            throw new ParamException("没有数据可以导出");
        }
        // 定义excel文件名
        String fileName = "死亡记录列表";
        Workbook workbook = ExcelUtils.createWorkBook(fileName);
        try {
            OutputStream os = this.getOutputStream(response, fileName);
            Sheet sheet = workbook.getSheetAt(0);
            //默认行宽列高
            sheet.setDefaultColumnWidth(11);
            sheet.setDefaultRowHeightInPoints(15);
            //定义表头
            String[] header = new String[]{
                    "防疫任务编号", "养殖户名称", "户主姓名", "联系电话", "户主身份证", "一卡通号码", "防疫人员", "注射时间", "死亡数量", "记录时间"
            };
            //表头对应的对象字段
            List<String> headerField = Lists.newArrayList(
                    "taskNum", "breedName", "userName",
                    "telphone", "cardNum", "icCard", "epUserName", "requiredTime", "deathCount", "recordTime"
            );
            CellStyle headerStyle = this.getHeaderStyle(workbook);
            Row headerRow = sheet.createRow(0);
            headerRow.setHeightInPoints(26);
            //设置表头
            for (int i = 0; i < header.length; i++) {
                Cell headerCell = headerRow.createCell(i);
                headerCell.setCellStyle(headerStyle);
                headerCell.setCellValue(header[i]);
            }
            try {
                CellStyle dataStyle = this.getDataStyle(workbook);
                for (int i = 0; i < all.size(); i++) {
                    Row dataRow = sheet.createRow(i + 1);
                    DeathRecordView deathRecordView = all.get(i);
                    for (int j = 0; j < headerField.size(); j++) {
                        Cell dataCell = dataRow.createCell(j);
                        dataCell.setCellStyle(dataStyle);
                        Field field = deathRecordView.getClass().getDeclaredField(headerField.get(j));
                        field.setAccessible(true);
                        dataCell.setCellValue(field.get(deathRecordView) == null ? "" : field.get(deathRecordView).toString());
                        if ("requiredTime".equals(headerField.get(j))) {
                            // 时间进行转换
                            LocalDate o = (LocalDate) field.get(deathRecordView);
                            if (o != null) {
                                dataCell.setCellValue(field.get(deathRecordView) == null ? "" : o.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                            }
                        }
                        if ("recordTime".equals(headerField.get(j))) {
                            // 时间进行转换
                            LocalDateTime o = (LocalDateTime) field.get(deathRecordView);
                            if (o != null) {
                                dataCell.setCellValue(field.get(deathRecordView) == null ? "" : o.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                            }
                        }
                    }
                }
            } catch (NoSuchFieldException | IllegalAccessException e) {
                throw new WarnException("导出失败,读取字段时出错!");
            }
            workbook.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new WarnException("导出任务列表失败:" + e.getMessage());
        }
public static Workbook createWorkBook(String sheetName) {
        // 创建excel工作簿
        Workbook wb = new HSSFWorkbook();
        // 创建第一个sheet(页),并命名
        wb.createSheet(sheetName);
        return wb;
    }
## 得到outputStream对象
private OutputStream getOutputStream(HttpServletResponse response, String fileName) {
        OutputStream os = null;
        try {
            response.setHeader("content-Type", "application/vnd.ms-excel");
            os = new BufferedOutputStream(response.getOutputStream());
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return os;
    }

设置表头样式

private CellStyle getHeaderStyle(Workbook workbook) {
        Font font = this.getHeaderFont(workbook);
        //表头单元格样式
        CellStyle style = workbook.createCellStyle();
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setWrapText(true);
        style.setFont(font);
        return style;
    }

设置数据区样式

 private CellStyle getDataStyle(Workbook workbook) {
        Font font = this.getDataFont(workbook);
        //表头单元格样式
        CellStyle style = workbook.createCellStyle();
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setAlignment(HorizontalAlignment.LEFT);
        style.setFont(font);
        return style;
    }

java导出excel完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>