本文章來自【知識林】
導出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
,內容如圖:
POI導出Word文件的模板
- 編寫導出程序
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
文件中,具體的內容如下圖:
POI導出Word的結果圖片
示例代碼:https://github.com/zsl131/spring-boot-test/tree/master/study14
本文章來自【知識林】