本文章來自【知識林】
導(dǎo)出Word文件其實與Springboot沒有多大關(guān)系,這都是Apache子項目POI
的功勞。下面簡單介紹一下在Springboot項目中如何使用POI導(dǎo)出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>
關(guān)鍵的依賴是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>
- 創(chuàng)建Word模板文件
創(chuàng)建一個Word文件代乃,命名為:template.doc
,內(nèi)容如圖:
POI導(dǎo)出Word文件的模板
- 編寫導(dǎo)出程序
private void build(File tmpFile, Map<String, String> contentMap, String exportFile) throws Exception {
FileInputStream tempFileInputStream = new FileInputStream(tmpFile);
HWPFDocument document = new HWPFDocument(tempFileInputStream);
// 讀取文本內(nèi)容
Range bodyRange = document.getRange();
// 替換內(nèi)容
for (Map.Entry<String, String> entry : contentMap.entrySet()) {
bodyRange.replaceText("${" + entry.getKey() + "}", entry.getValue());
}
//導(dǎo)出到文件
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
document.write(byteArrayOutputStream);
OutputStream outputStream = new FileOutputStream(exportFile);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.close();
}
參數(shù)說明:
tmpFile
: 模板文件
contentMap
:數(shù)據(jù)模型仁卷,包含具體數(shù)據(jù)的map對象
exportFile
:需要保存導(dǎo)出文件的路徑
- 導(dǎo)出文件測試方法
@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", "標(biāo)題部份");
datas.put("content", "這里是內(nèi)容纵刘,測試使用POI導(dǎo)出到Word的內(nèi)容邀窃!");
datas.put("author", "知識林");
datas.put("url", "http://www.zslin.com");
build(new File(tmpFile), datas, expFile);
}
注意:這里的模板文件是放到D:/temp
目錄下,在實際項目應(yīng)用中這些模板文件都是需要放在項目的classpath中的假哎,這樣的做法很明顯不能滿足需求。
- 模板文件中
classpath
中的導(dǎo)出文件測試方法
@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", "標(biāo)題部份");
datas.put("content", "這里是內(nèi)容鞍历,測試使用POI導(dǎo)出到Word的內(nèi)容舵抹!");
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ǎo)出的文件都放在:D:/temp/result.doc
文件中惧蛹,具體的內(nèi)容如下圖:
POI導(dǎo)出Word的結(jié)果圖片
示例代碼:https://github.com/zsl131/spring-boot-test/tree/master/study14
本文章來自【知識林】