栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

easypoi按模板导出文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

easypoi按模板导出文件

springboot使用easypoi通过使用模板导出文件 第一步:导入poi以及easypoi的maven坐标

首先,easypoi是依赖于Apache的poi实现的,因此需要先引入poi


	org.apache.poi
	poi
	4.1.2



	org.apache.poi
	poi-ooxml
	4.1.2

其次引入easypoi
maven项目引入:

        
            cn.afterturn
            easypoi-base
            3.1.0
        
        
            cn.afterturn
            easypoi-web
            3.1.0
        
        
            cn.afterturn
            easypoi-annotation
            3.1.0
        

springboot项目引入:


    cn.afterturn
    easypoi-spring-boot-starter
    4.4.0

第二步:编写后台代码

我后台使用的是springboot

方式一:此种方式比较简单,直接写入模板的地址,使用WordExportUtil就可以直接拿到带有数据的模板文档。

注意:但是有个坑,就是一旦更换模板,项目需要重新启动才能刷新模板。意思就是你第一次使用模板通过此程序时,它会对模板进行缓存,等你下次再通过时会使用缓存中的模板来进行输出,因此在模板不改变的情况下使用此种方式还算是比较简单一些。

    @GetMapping("/exportByEasypoi")
    public void exportByEasypoi() throws Exception {
        ArrayList users = new ArrayList();
        User user = new User();
        user.setId("1");
        user.setName("小张");
        user.setAge("18");
        user.setTel("138383838383");
        users.add(user);

        users.add(new User("2","小王","22","384324324"));
        users.add(new User("3","小3","23","3843243224"));
        users.add(new User("4","小4","24","11122224"));
        users.add(new User("5","小5","25","14654324324"));

        File fileOut = new File("C:\Users\Administrator\Desktop\test\target.docx");
        OutputStream outputStream = new FileOutputStream(fileOut);

        Map data = new HashMap();
        data.put("data",users);

        XWPFdocument document = WordExportUtil.exportWord07("C:\Users\Administrator\Desktop\test\template.docx", data);
        document.write(outputStream);
        outputStream.close();
    }
方拾二:这种方式就是自定义加载模板的方式,让你的模板可以随时改变,并且每次导出倒是按照你地址的最新形态来给你导出
@GetMapping("/exportByEasypoi")
    public void exportByEasypoi() throws Exception {
        ArrayList users = new ArrayList();
        User user = new User();
        user.setId("1");
        user.setName("小张");
        user.setAge("18");
        user.setTel("138383838383");
        users.add(user);

        users.add(new User("2","小王","22","384324324"));
        users.add(new User("3","小3","23","3843243224"));
        users.add(new User("4","小4","24","11122224"));
        users.add(new User("5","小5","25","14654324324"));

        File fileIn = new File("C:\Users\Administrator\Desktop\test\template.docx");
        File fileOut = new File("C:\Users\Administrator\Desktop\test\target.docx");
        OutputStream outputStream = new FileOutputStream(fileOut);

        Map data = new HashMap();
        data.put("data",users);
        InputStream inputStream = new FileInputStream(fileIn);
        MyXWPFdocument sourcedocument = new MyXWPFdocument(inputStream);
        WordExportUtil.exportWord07(sourcedocument, data);
        sourcedocument.write(outputStream);
        
        outputStream.close();
    }

导出图片到模板中

ImageEntity imageEntity = new ImageEntity();
imageEntity.setUrl(imageUrl);
imageEntity.setHeight(384);
imageEntity.setWidth(676);
imageEntity.setType(ImageEntity.URL);
previewContractObj.setImage(imageEntity);
data.put("previewContractObj", previewContractObj);

图片的长宽必须设置,这里设置为多少,导出时显示的大小就是多少
模板直接引用:

第三步:编辑模板


注意:

模板这里有个小坑,就是当传入的值为数组时,需要遍历输出必须要放到表格里easypoi才能完整的遍历,对于表格以外的位置支持的并不是很好。模板遍历时,解析的数组子对象默认为“t”。直接用“t”来引用就可以了。也可以在后面自定义

以下就是模板常用的指令:
空格分割
三目运算 {{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& 空格
&INDEX& 表示循环中的序号,自动添加
]] 换行符 多行遍历导出
sum: 统计数据
cal: 基础的±X% 计算
dict: 字典
i18n: 国际化

第四步 运行访问

由于这里只需要后端处理,因此我就不写前端了,直接用postman访问就可以了。把文件写到本地文件夹进行预览,如图所示:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763551.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号