JAVA导出Word文档工具EasyWord
介绍
基于Apache poi封装,在上层做了模型转换的封装,让使用者更加简单方便
只支持docx的导出,不支持doc
下面废话少说 让我们以最快的方式学会用java导出word文档
组件依赖
依赖 | 版本 | 备注 |
---|---|---|
lombok | 1.18.10 | |
poi | 5.1.0 | |
poi-ooxml | 5.1.0 | |
poi-scratchpad | 5.1.0 |
使用教程
1.引入依赖
<dependency><groupId>com.sushengren</groupId><artifactId>easyword</artifactId><version>1.1.3</version>
</dependency>
2.制作word模板
3.编写测试类
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DemoData {@WordProperty("标题")private String title;@WordProperty("生成时间")private String generationTime;@WordProperty("学生数")private Integer numberOfStudents;@WordProperty("监考老师数")private Integer numberOfInvigilators;@WordProperty("评卷老师数")private Integer numberOfTeachers;@WordProperty("年度")private String year;@WordProperty("学期")private String semester;@WordProperty("考试时间")private String examinationTime;@WordProperty("科目")private String subject;@WordProperty(value = "Logo", converter = PictureConverter.class)private InputStream logo;@WordProperty("班级列表")private List<ClassInfo> classList;@Getter@Setter@Builder@NoArgsConstructor@AllArgsConstructorpublic static class ClassInfo {@WordProperty("班级")private String className;@WordProperty("人数")private Integer numberOfPeople;@WordProperty("平均分")private Double theAverageScore;@WordProperty("年级")private String grade;@WordProperty("排名")private Integer ranking;@WordProperty("班主任")private String classTeacher;@WordProperty("学生列表")private List<StudentInfo> studentList;}@Getter@Setter@Builder@NoArgsConstructor@AllArgsConstructorpublic static class StudentInfo {@WordProperty("姓名")private String name;@WordProperty("学号")private String studentID;@WordProperty("总分")private Double totalScore;@WordProperty("平均分")private Double theAverageScore;@WordProperty("排名")private Integer ranking;@WordProperty("年级排名")private Integer gradeRanking;@WordProperty("备注")private String remark;}}public static void main(String[] args) throws IOException {List<StudentInfo> studentList = new ArrayList<>();studentList.add(new StudentInfo("小明", "No00001", 280.0, 93.3, 1, 1, ""));studentList.add(new StudentInfo("小红", "No00002", 260.0, 86.6, 2, 2, ""));studentList.add(new StudentInfo("小花", "No00003", 270.0, 90.0, 3, 120, ""));studentList.add(new StudentInfo("小莉", "No00004", 250.0, 83.3, 4, 210, ""));studentList.add(new StudentInfo("托尼", "No00005", 241.0, 80.3, 5, 600, ""));List<ClassInfo> classList = new ArrayList<>();classList.add(new ClassInfo("一年级一班", 50, 270.5, "一年级", 1, "温娟", studentList));classList.add(new ClassInfo("一年级二班", 60, 260.5, "一年级", 2, "张三", studentList));classList.add(new ClassInfo("一年级三班", 35, 280.5, "一年级", 3, "李四", studentList));classList.add(new ClassInfo("一年级四班", 56, 290.5, "一年级", 4, "王五", studentList));DemoData data = DemoData.builder().title("2022年度期末考试成绩报告").generationTime("2022-01-01").numberOfStudents(1510).numberOfInvigilators(157).numberOfTeachers(157).year("二零二二").semester("第二学期").examinationTime("2022-10-01 至 2022-10-02").subject("语文、数学、英语").classList(classList).logo(new FileInputStream("C:\\Users\\mangfu\\Pictures\\logo.png")).build();File file = new File("C:\\Users\\mangfu\\Desktop\\期末成绩报告模板.docx");FileOutputStream out = new FileOutputStream("C:\\Users\\mangfu\\Desktop\\期末成绩报告模板-1.docx");EasyWord.of(file).doWrite(data).toOutputStream(out);}
4.模板输出word文件
接口方式
/*** 导出word文件*/@ApiOperation("学习成绩报表")@GetMapping("/download")public void download(HttpServletResponse response, String param1, String param2) throws IOException {if(Strings.isNullOrEmpty(param1) || Strings.isNullOrEmpty(param2)){return;}try {//设置浏览器导出excl格式文件//response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.sheet");//设置浏览器导出word格式文件response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");//设置字符集response.setCharacterEncoding("utf-8");// 这里URLEncoder.encode可以防止中文乱码 注意这里Chrome仍然会乱码String fileName = URLEncoder.encode(tableName, "UTF-8").replaceAll("\\+", "%20");//设置导出excl文件格式//response.setHeader("Content-Disposition", "attachment;utf-8;filename=" + fileName + ".xlsx");//设置导出word文件格式response.setHeader("Content-Disposition", "attachment;utf-8;filename=" + fileName + ".docx");response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");//todo获取表信息//todo获取表字段信息TableInfo data = TableInfo.builder().title("2022年度期末考试成绩报告").generationTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())).numberOfStudents(1510).numberOfInvigilators(157).numberOfTeachers(157).year("二零二二").semester("第二学期").examinationTime("2022-10-01 至 2022-10-02").subject("语文、数学、英语").classList(classList).logo(new FileInputStream("C:\\Users\\mangfu\\Pictures\\logo.png")).build();//获取resources下目中录模板文件//File file = new ClassPathResource("xxx/xxx.docx").getFile();//以流的形式 获取resources下目中录模板文件InputStream file = new ClassPathResource("xxx/xxx.docx").getInputStream();EasyWord.of(file).doWrite(data).toOutputStream(response.getOutputStream());} catch (Exception e) {// 重置responseresponse.reset();response.setContentType("application/json");response.setCharacterEncoding("utf-8");Map<String, String> map = MapUtils.newHashMap();map.put("status", "failure");map.put("message", "下载文件失败" + e.getMessage());response.getWriter().println(JSON.toJSONString(map));}}
总结
java导出word文档 的使用方式还有很多 以最简单的方式学习 希望能够帮助到各位!
JAVA导出Word文档工具EasyWord相关推荐
- 【Java用法】使用Java导出word文档的解决方案(适用于从服务器上下载到本地电脑)
本文目录 一.Controller 二.Service 接口类 三.ServiceImpl 实现类 四.Content-Type 类型与MIME Type类型对照表 最近在做一个word导出功能,需求 ...
- 【Java用法】使用Java导出word文档的解决方案(适用于Windows电脑)
目录 实现方式一.通过原生的POI 实现方式二.通过Hutool工具包 步骤1.添加pom依赖 步骤2.编写几行代码 步骤3.启动项目,大功告成 实现的效果 最近在做一个word导出功能,需求非常简单 ...
- 【day22】java导出word文档(包含导出图片)
1.[Java]使用freemarker模板技术导出word main方法测试 package com.havenliu.document;import java.io.UnsupportedEnco ...
- java导出word文档组件_java导出word文档(转)
导出word文档有两种方式:第一种是使用POI,第二种是使用itext组件,下面逐渐介绍: 1.使用POI的方式:本方式目前没法改变字体样式,只能输出纯文本. /** * 试卷导出word文档 * @ ...
- Java导出word文档
需求 在开发中,经常会碰到需要导出word文档的时候,比如一些审批表.财务报表等等,接下来介绍一种基于java的简单高效的转换方法 环境 依赖 Apache的POI.FreeMarker和XDocre ...
- Java导出Word文档的几种方法
前言 在日常的开发工作中,我们时常会遇到导出Word文档报表的需求,比如公司的财务报表.医院的患者统计报表.电商平台的销售报表等等. 导出Word方式多种多样,通常有以下几种方式: 使用第三方Java ...
- Java 导出Word文档(含图片)doc格式 (保姆级)
1.maven仓库 <!-- freemarker jar --><dependency><groupId>org.freemarker</groupId&g ...
- Java 导出word文档,遍历表格数据,导出图片
引用:https://www.cnblogs.com/pxblog/p/13072711.html 1.引入maven依赖: <dependency><groupId>cn.a ...
- java导出word文档(easypoi)
导出word,引用的是easypoi的jar 第一步:导入jar <dependency><groupId>cn.afterturn</groupId><ar ...
最新文章
- 被劝退的学渣,逆袭成高级语言之父,改变编程方式却说“不喜欢写代码”!...
- 参数binlog_row_image设置MINIMAL,你今天被坑了吗?
- 死脑筋要活用(我只放贴图)01
- ASP.NET自定义控件组件开发 第一章 第二篇 接着待续
- 【随感】i'm new here
- java selenium (十二) 操作弹出窗口
- 服饰贴图定制小程序V1.2.4安装更新一体包+小程序前端
- 【折腾】斐讯N1 安装 Docker + GUI
- 字段定义_SAP 物料主数据增强自定义字段
- Expression Blend实例中文教程(11) - 视觉管理器快速入门Visual State Manager(VSM)
- 华为手机所有图标变黑_华为手机app图标变成黑色
- CentOs7下Zabbix安装教程——zabbix agent安装和前端配置
- java中new对象_java new对象的理解
- 小波变换matlab代码,matlab小波变换代码
- 如何申请免费的腾讯企业邮箱
- 人脸识别小区门禁系统_(完整版)小区人脸识别门禁系统
- android版本下载京东,京东下载2021安卓最新版_手机app官方版免费安装下载_豌豆荚...
- Elastic:集群相关知识点总结(一)数据流 Data Stream、索引生命周期 ILM、可搜索快照 searchable snapshots、跨集群搜索 CCS、跨集群复制 CCR
- 黑鲨helo支持html吗,黑鲨2和黑鲨helo区别买哪个好
- 乐乐音乐播放器 功能介绍(一)
热门文章
- h264 reorder list
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem H. Parallel Worlds 计算几何...
- 华为鲲鹏HCIA-Kunpeng Application Developer V1.0考试样题
- 华为鲲鹏HCIA-Cloud Service V2.2考试样题
- 星外主机星外主机php,星外主机可写可执行目录
- 两年后小米市值在100亿美元以下
- 教你如何轻松点亮自己的qq图标!
- 行业动态-DALI联盟开放DALI技术手册中文版下载
- 和姚明的本科比起来,翟天临的博士,差了些。
- php拦截登录页面跳转页面,PHP未登录自动跳转到登录页面