指点成金-最美分享吧

登录

使用EasyPOI导出复杂的Word表格

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了使用EasyPOI导出复杂的Word表格相关的知识,希望对你有一定的参考价值。

在我们日常开发中常常有需求,需要以Word格式导出各种合同信息,收费凭证等问题。格式较为复杂(如下图)。我们该怎么解决呢?

下面通过五步带大家学习如何使用EasyPOI导出复杂的Word表格!


文章目录

  • 一.EasyPoi简介
  • 二.准备工作
  • 三.上传模板
  • 四.代码编写
  • 五.测试


一.EasyPoi简介

easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板语言(熟悉的表达式语法),完成以前复杂的写法。

  • GitHub地址:https://github.com/liurenjin/easypoi

  • 教程地址:http://easypoi.mydoc.io/#text_186905

二.准备工作

  1. 新建SpringBoot项目并添加maven依赖
       <dependency>            <groupId>cn.afterturngroupId>            <artifactId>easypoi-baseartifactId>            <version>4.1.0version>        dependency>        <dependency>            <groupId>cn.afterturngroupId>            <artifactId>easypoi-webartifactId>            <version>4.1.0version>        dependency>        <dependency>            <groupId>cn.afterturngroupId>            <artifactId>easypoi-annotationartifactId>            <version>4.1.0version>        dependency>
  1. 了解导出EasyPOI常用的一些指令和其作用

注意:整体风格和el表达式类似,采用的写法是代表表达式,然后根据表达式里面的数据取值。

指令作用
test ? obj:obj2三目运算
n:表示 这个cell是数值类型 n:
le:代表长度le:() 在if/else 运用le:() > 8 ? obj1 : obj2
fd:格式化时间 fd:(obj;yyyy-MM-dd)
fn:格式化数字 fn:(obj;###.00)
fe:遍历数据,创建row
!fe:遍历数据不创建row
$fe:下移插入,把当前行,下面的行全部下移.size()行,然后插入
#fe:横向遍历
v_fe:横向遍历值
!if:删除当前列 !if:(test)
"’单引号表示常量值 "’ 比如’1’ 那么输出的就是 1
&NULL&空格
]]换行符 多行遍历导出
sum:统计数据
  1. 根据EasyPOI常用指令填写word模板(这里Word版本最好在07版本以上,模板后缀为.docx

三.上传模板

将上面修改好的Word模板上传至项目的Resources目录下

四.代码编写

编写Word文档的下载方法

    @GetMapping(value = "/download")    public void test(HttpServletResponse response) throws Exception        //获取模板文档        File rootFile =new File(ResourceUtils.getURL("classpath:").getPath());        File templateFile= new File(rootFile,"/word/test.docx");        //填充数据        List<Map> resultList =new ArrayList<>();        Map<String,Object> resultListParams =new HashMap<>();        Map<String,Object> resultListParams2 =new HashMap<>();        resultListParams.put("id","1");        resultListParams2.put("id","2");        resultListParams.put("name","测试1");        resultListParams2.put("name","测试2");        resultListParams.put("zh","111");        resultListParams2.put("zh","222");        resultListParams.put("xx","xx1");        resultListParams2.put("xx","xx2");        resultListParams.put("je","je1");        resultListParams2.put("je","je2");        resultListParams.put("hh","hh1");        resultListParams2.put("hh","hh2");        resultList.add(resultListParams);        resultList.add(resultListParams2);        //准备数据        Map<String,Object> params =new HashMap<>();        params.put("zhandian","测试站点");        params.put("kpsj", "2022-4-1");        params.put("kprs", "5");        params.put("zje", "100");        params.put("je", "50");        params.put("je2", "22");        params.put("xuexiaomingchenghebianhao", "测试学校-55556555");        params.put("resultList",resultList);        XWPFDocument word = WordExportUtil.exportWord07(templateFile.getPath(),params);        String filename = "1+X幼儿照护职业技能等级证书.docx";        response.setHeader("content-disposition","attachment;filename="+new String(filename.getBytes(),"ISO8859-1"));        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");        word.write(response.getOutputStream());    

五.测试

项目启动成功后我们访问本地接口进行下载!发现所有的值都完成了替换!

以上是关于使用EasyPOI导出复杂的Word表格的主要内容,如果未能解决你的问题,请参考以下文章