java 导出数据到EXCEL

在工作中我们经常遇到写数据到EXCEL中,今天发型代码中有很多导出EXCEL的代码,但是每人都开了一个自己的导出EXCEL的方法,流程都一样:
先是从DB读取数据到list中,然后创建workbook -> 创建worksheet -> 创建EXCEL的header -> 创建row -> 填充数据到row -> 写数据到response中

正好这次需求也是导出用户信息到EXCEL中,顺便一起把这些重复的工作一起重构了,也方便以后再新增导出其他信息到EXCEL时不用写那些重复的逻辑。
重构代码的思路:

1:把EXCEL的 header 修改成动态设置

创建一个EXCEL的注解,提供出数据每个属性在EXCEL中的顺序和中文名称

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExcelCellTitle {

    //默认值
    String value() default "";

    //excel中的序号
    int order() default 0;

    //表头
    String title() default "";
}

对于每个需要写入EXCEL中的数据模型都自己添加该注解并设置好order和title

2:把EXCEL 中写入Row 的代码抽象到接口里,交给具体的数据对象来实现

public interface ImportExcelDTO {

    /**
     * 添加数据到EXCEL中的row中
     * @Author xiaoyue.chen
     * @param row
     */
    void addOneRowOfDataToExcel(Row row);
}

定义具体的业务数据模型

public class EmployeePermsDTO implements ImportExcelDTO {

    @ExcelCellTitle(order = 1, title = "工号")
    private String userId;

    @ExcelCellTitle(order = 2, title = "姓名")
    private String userCn;

    @ExcelCellTitle(order = 3, title = "部门编号")
    private String userOrgId;

    @ExcelCellTitle(order = 4, title = "部门名称")
    private String orgname;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserCn() {
        return userCn;
    }

    public void setUserCn(String userCn) {
        this.userCn = userCn;
    }

    public String getUserOrgId() {
        return userOrgId;
    }

    public void setUserOrgId(String userOrgId) {
        this.userOrgId = userOrgId;
    }

    public String getOrgname() {
        return orgname;
    }

    public void setOrgname(String orgname) {
        this.orgname = orgname;
    }

    /**
     * 添加数据到EXCEL中的row中
     * @Author xiaoyue.chen
     * @param row
     */
    @Override
    public void addOneRowOfDataToExcel(Row row) {
        WorkSheetBuilder.createCell(row, 0).setCellValue(this.getUserId());
        WorkSheetBuilder.createCell(row, 1).setCellValue(this.getUserCn());
        WorkSheetBuilder.createCell(row, 2).setCellValue(this.getUserOrgId());
        WorkSheetBuilder.createCell(row, 3).setCellValue(this.getOrgname());
    }
}

通过这样的设计方式就可以把具体的数据填充交给具体的数据对象处理,对于工具类就可以使用接口来填充数据到row中,降低代码的耦合度

3:在创建EXCEL的header时需要设置EXCEL的header格式,字体格式,再抽一个EXCEL的格式工具类

public class WorkSheetBuilder {

    private static void setCellBorder(CellStyle cellStyle) {
        //边框颜色和宽度设置
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 下边框
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 左边框
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 右边框
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边框
    }

    private static void setCellBackground(CellStyle cellStyle) {
        //设置背景颜色
        cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    }

    private static void setCellFont(CellStyle cellStyle, Workbook writeWorkbook) {
        //粗体字设置
        Font font = writeWorkbook.createFont();
        font.setBoldweight((short)3);//.setBold(true);
        cellStyle.setFont(font);
    }

    public static Row createRow(Sheet sheet, int rowNum) {
        return sheet.createRow(rowNum);
    }

    public static Cell createCell(Row row, int colNum) {
        Cell cell = row.createCell(colNum);
        return cell;
    }

    public static Cell createCell(Row row, int colNum, CellStyle cellStyle) {
        Cell cell = row.createCell(colNum);
        cell.setCellStyle(cellStyle);
        return cell;
    }

    public static CellStyle buildHeadCellStyle(Workbook writeWorkbook) {
        CellStyle style = writeWorkbook.createCellStyle();
        //对齐方式设置
        style.setAlignment(CellStyle.ALIGN_CENTER);
        setCellBorder(style);
        setCellBackground(style);
        setCellFont(style, writeWorkbook);
        return style;
    }
}

4:实现具体的EXCEL工具类

完成对数据的header解析,导入,并写入response。同时对外提供统一的导入接口。

public class ExcelWriterBuilder {

    /**
     * work book
     */
    private Workbook writeWorkbook;

    /**
     * work sheet
     */
    private Sheet writeSheet;

    /**
     * excel file
     */
    private File file;

    /**
     * excel 文件名称
     */
    private String fileName;

    /**
     * 创建excel title
     * @Author xiaoyue.chen
     * @param tClass
     */
    private void createExcelHeader(Class tClass) {
        CellStyle headerStyle = WorkSheetBuilder.buildHeadCellStyle(this.writeWorkbook);
        List<String> titles = getCellHeader(tClass);
        Row row = WorkSheetBuilder.createRow(this.writeSheet, 0);
        for (int i=0; i < titles.size(); i++) {
            WorkSheetBuilder.createCell(row, i, headerStyle).setCellValue(titles.get(i));
        }
    }

    /**
     * 填充数据到Excel表格
     * @Author xiaoyue.chen
     * @param writeTable 数据
     */
    private void addContent(List<? extends ImportExcelDTO> writeTable) {
        int rowIndex = 1;
        for (ImportExcelDTO dto : writeTable) {
            Row row = WorkSheetBuilder.createRow(this.writeSheet, rowIndex);
            dto.addOneRowOfDataToExcel(row);
            rowIndex++;
        }
    }

    /**
     * 获取excel titile
     * @Author xiaoyue.chen
     * @param tClass
     * @return
     */
    private List<String> getCellHeader(Class<T> tClass) {
        List<String> cellTitle = Arrays.stream(tClass.getDeclaredFields())
                .sorted(Comparator.comparing(field -> field.getAnnotation(ExcelCellTitle.class).order()))
                .map(field -> field.getAnnotation(ExcelCellTitle.class).title())
                .collect(Collectors.toList());
        return cellTitle;
    }

    /**
     * 浏览器下载excel
     * @Author xiaoyue.chen
     * @param wb
     * @param response
     */
    private void buildExcelDocument(Workbook wb, HttpServletResponse response) throws IOException {
        OutputStream outStream = null;//response.getOutputStream();

        try {
            //response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.reset();
            response.setContentType("application/binary;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(this.fileName, "utf-8"));
            response.flushBuffer();
            outStream = response.getOutputStream();
            wb.write(outStream);
            outStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (!ObjectUtils.isEmpty(outStream)) {
                outStream.close();
            }
        }
    }

    /**
     * 创建excel文件到本地目录
     * @throws IOException
     */
    private void createExcelToLocal() throws IOException {
        FileOutputStream fileOut = null;

        try {
            fileOut = new FileOutputStream(this.file);
            this.writeWorkbook.write(fileOut);
            fileOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (!ObjectUtils.isEmpty(fileOut)) {
                fileOut.close();
            }
        }
    }

    /**
     * 构造函数, 创建work boot
     * @Author xiaoyue.chen
     */
    public ExcelWriterBuilder() {
        this.writeWorkbook = new XSSFWorkbook();
    }

    /**
     * 构造函数
     * @Author xiaoyue.chen
     * @param fileName 文件名称
     */
    public ExcelWriterBuilder(String fileName) {
        this();
        this.fileName = fileName;
    }

    /**
     * 创建excel sheet 文件
     * @Author xiaoyue.chen
     * @param sheetName sheet name
     * @return
     */
    public ExcelWriterBuilder buildSheet(String sheetName) {
        this.writeSheet = this.writeWorkbook.createSheet(sheetName);
        return this;
    }

    /**
     * 将数据导入到excel
     * @Author xiaoyue.chen
     * @param writeTable
     * @param tClass
     */
    public void importDataToExcel(List<? extends ImportExcelDTO> writeTable, Class tClass) {
        this.createExcelHeader(tClass);
        this.addContent(writeTable);
    }

    /**
     * 生成文档供浏览器下载
     * @Author xiaoyue.chen
     */
    public void buildDownLoadExcel(HttpServletResponse response) {
        try {
            this.buildExcelDocument(this.writeWorkbook, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5: 外部调用

  private void writeEmployeeInfo2Excel(List<EmployeeDTO> employee, HttpServletResponse response) {
    ExcelWriterBuilder builder = new ExcelWriterBuilder("userInfo.xlsx");
    builder.buildSheet("userinfo").importDataToExcel(employee, EmployeePermsDTO.class);
    builder.buildDownLoadExcel(response);
  }


  private void writePerm2Excel(List<PermBasicDTO> perms, HttpServletResponse response) {
    ExcelWriterBuilder builder = new ExcelWriterBuilder("perm.xlsx");
    builder.buildSheet("permInfo").importDataToExcel(perms, PermBasicDTO.class);
    builder.buildDownLoadExcel(response);
  }

如果以后再有新的数据对象需要导入EXCEL中就只需要完成数据的获取和写入规则,然后创建一个ExcelWriterBuilder ,依次调用buidShett和importDataToExcel方法就可以完成数据导入。
减少了EXCEL操作的重复代码。

6:controller接口定义

 @ApiOperation("导出员工信息")
    @GetMapping(value = "/user/export")
    public void exportEmployeePerms( HttpServletResponse response) {
        logger.info("导出员工信息");
        service.exportEmployeeInfo(response);
    }

【注】 需要在maven中引入下面的依赖

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8</version>
        </dependency>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,490评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,581评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,830评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,957评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,974评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,754评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,464评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,847评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,995评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,137评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,819评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,482评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,023评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,149评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,409评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,086评论 2 355

推荐阅读更多精彩内容