Springboot 之 POI导出Word文件
来源:互联网 发布:江苏清华紫光软件集团 编辑:程序博客网 时间:2024/05/18 09:52
本文章来自【知识林】
导出Word文件其实与Springboot没有多大关系,这都是Apache子项目POI
的功劳。下面简单介绍一下在Springboot项目中如何使用POI导出Word文件。
- pom.xml文件
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --></parent><dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.15</version> </dependency></dependencies>
关键的依赖是poi
的jar包:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.15</version></dependency><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.15</version></dependency>
- 创建Word模板文件
创建一个Word文件,命名为:template.doc
,内容如图:
- 编写导出程序
private void build(File tmpFile, Map<String, String> contentMap, String exportFile) throws Exception { FileInputStream tempFileInputStream = new FileInputStream(tmpFile); HWPFDocument document = new HWPFDocument(tempFileInputStream); // 读取文本内容 Range bodyRange = document.getRange(); // 替换内容 for (Map.Entry<String, String> entry : contentMap.entrySet()) { bodyRange.replaceText("${" + entry.getKey() + "}", entry.getValue()); } //导出到文件 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); document.write(byteArrayOutputStream); OutputStream outputStream = new FileOutputStream(exportFile); outputStream.write(byteArrayOutputStream.toByteArray()); outputStream.close();}
参数说明:
tmpFile
: 模板文件
contentMap
:数据模型,包含具体数据的map对象
exportFile
:需要保存导出文件的路径
- 导出文件测试方法
@Testpublic void testExportWord() throws Exception { String tmpFile = "D:/temp/template.doc"; String expFile = "D:/temp/result.doc"; Map<String, String> datas = new HashMap<String, String>(); datas.put("title", "标题部份"); datas.put("content", "这里是内容,测试使用POI导出到Word的内容!"); datas.put("author", "知识林"); datas.put("url", "http://www.zslin.com"); build(new File(tmpFile), datas, expFile);}
注意:这里的模板文件是放到D:/temp
目录下,在实际项目应用中这些模板文件都是需要放在项目的classpath中的,这样的做法很明显不能满足需求。
- 模板文件中
classpath
中的导出文件测试方法
@Testpublic void testExportWord2() throws Exception { String tmpFile = "classpath:template.doc"; String expFile = "D:/temp/result.doc"; Map<String, String> datas = new HashMap<String, String>(); datas.put("title", "标题部份"); datas.put("content", "这里是内容,测试使用POI导出到Word的内容!"); datas.put("author", "知识林"); datas.put("url", "http://www.zslin.com"); build(ResourceUtils.getFile(tmpFile), datas, expFile);}
注意: 使用ResourceUtils
工具类的getFile
方法即可读取classpath
中的文件,所以这里读模板文件的方法是:ResourceUtils.getFile("classpath:template.doc")
。
以上两种方法导出的文件都放在:D:/temp/result.doc
文件中,具体的内容如下图:
示例代码:https://github.com/zsl131/spring-boot-test/tree/master/study14
本文章来自【知识林】
1 0
- Springboot 之 POI导出Word文件
- Springboot 之 使用POI导出Excel文件
- springboot + poi word文档导出,Excel文件读取
- poi 导出word 表格文件
- POI之根据模板导出word-yellowcong
- POI导出word 文档之 WordHelper
- SpringBoot poi 导出Excel
- POI 导出word
- poi导出word[1]
- poi导出word[2]
- POI模板导出word
- POI导出word 文档
- 使用poi导出word
- POI 导出word
- POI 导出 Word 表格
- poi导出word
- poi用于写入,导出excel,word等文件
- 【Apache POI】Java Web根据模板导出word文件
- Openwrt squafs文件系统及sysupgrade升级探究
- nyoj zb的生日
- 单一职责原则(SRP - Single Responsibility Principle)
- FPGA 学习之路(一)EDA软件设计流程
- swift_009(Swift 类型别名/安全/推断)
- Springboot 之 POI导出Word文件
- Java类加载器
- 谈我对网络协议的理解过程:
- Redis 订阅功能实现
- Java_JVM参数-XX:MaxDirectMemorySize 与 两种 ByteBuffer: heap,direct ByteBuffer
- Linux与Windows的文件共享
- 基于centos6的Hadoop2.6.5准备与安装
- nginx+tomcat+memcached负载均衡集群搭建(一)
- 伪装类