Springboot 之 POI导出Word文件
2016-10-28 17:00
477 查看
本文章来自【知识林】
导出Word文件其实与Springboot没有多大关系,这都是Apache子项目
pom.xml文件
关键的依赖是
创建Word模板文件
创建一个Word文件,命名为:
编写导出程序
参数说明:
导出文件测试方法
注意:这里的模板文件是放到
模板文件中
注意: 使用
以上两种方法导出的文件都放在:
示例代码:https://github.com/zsl131/spring-boot-test/tree/master/study14
本文章来自【知识林】
导出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:需要保存导出文件的路径
导出文件测试方法
@Test public 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中的导出文件测试方法
@Test public 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
本文章来自【知识林】
相关文章推荐
- Spring Boot--POI导出excel文件下载
- Springboot/SpringMVC+POI 实现Excel导出功能(点击下载方式实现)
- SpringMVC/SpringBoot使用easypoi实现Excel文件导入导出功能实现
- Springboot+Mybatis+Maven项目导出.csv文件
- SpringBoot中使用POI,快速实现Excel导入导出
- spring boot打造Excel,word文档文件在线预览解决方案
- SpringMVC+POI下载文件模板和导出Excel
- springboot+poi导出指定格式Excel模板详解+Demo
- springboot poi 导出excel
- SpringBoot中使用POI,快速实现Excel导入导出
- Spring Boot利用poi导出Excel
- Spring Boot学习笔记----POI(Excel导入导出)
- springboot easypoi导出excel
- poi 导出word 表格文件
- poi用于写入,导出excel,word等文件
- spring boot + spring data jpa 批量插入 + POI读取 + 文件上传
- JAVASCRIPT将网页表格导出EXCEL或WORD文件
- asp.net从数据库导出数据到word、excel、txt文本文件
- ASP.NET中导出excel/word等类型文件的简单方法
- GridView导出到Excel或Word文件