自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java技術(shù):SpringBoot集成FreeMarker生成word文件

開源
FreeMarker 是一款開源的模板引擎:是一種基于模板和要動態(tài)填充的數(shù)據(jù),可以用來動態(tài)渲染生成輸出文本(HTML網(wǎng)頁,Word文檔,電子郵件,配置文件,源代碼等)的通用技術(shù)。

今天給大家分享SpringBoot集成FreeMarker模板引擎生成word文件的用法,感興趣的可以學(xué)一下,完整源碼地址在文章末尾處,歡迎互相溝通交流!

一、什么是FreeMarker?

FreeMarker 是一款開源的模板引擎:是一種基于模板和要動態(tài)填充的數(shù)據(jù),可以用來動態(tài)渲染生成輸出文本(HTML網(wǎng)頁,Word文檔,電子郵件,配置文件,源代碼等)的通用技術(shù)。

模板編寫為FreeMarker Template Language (FTL):它是簡單的,專用的語言, 不是像PHP那樣擁有完整成熟的編程語言。所以它主要專注于如何展現(xiàn)數(shù)據(jù),具體要展示什么數(shù)據(jù)那就需要成熟的編程語言來實現(xiàn)(Java、C#、Python)等。

FreeMarker原理圖如下:

二、示例代碼

1、導(dǎo)入freemarker依賴包

<!--freemarker制作Html郵件模板依賴包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2、application.yml 配置freemarker

freemarker:
cache: false #是否啟用緩存,開發(fā)環(huán)境不建議啟動因為涉及經(jīng)常修改模板調(diào)試
settings:
classic_compatible: true
suffix: .html #一般格式tpl居多
charset: UTF-8
template-loader-path: classpath:/templates/ #模板路徑,一般都是這個

3、新建IExportService.java 類

package com.springboot.email.email.service;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public interface IExportService {
/**
* 導(dǎo)出word文件到指定目錄
*/
void exportDocFile(String fileName, String tplName, Map<String, Object> data) throws Exception;
/**
* 導(dǎo)出word文件到客戶端
*/
void exportDocToClient(HttpServletResponse response, String fileName, String tplName, Map<String, Object> data) throws Exception;
}

4、新建IExportService.java接口實現(xiàn)類ExportServiceImpl.java

package com.springboot.email.email.service.impl;

import com.springboot.email.email.service.IExportService;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;

@Service
public class ExportServiceImpl implements IExportService {
private String encoding;
private String exportPath = "D:\\export\\";
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
public Template getTemplate(String name) throws Exception {
return freeMarkerConfigurer.getConfiguration().getTemplate("test.html");
}


/**
* 導(dǎo)出本地文件到指定的目錄
*/
@Override
public void exportDocFile(String fileName, String tplName, Map<String, Object> data) throws Exception {
//如果目錄不存在,則創(chuàng)建目錄
File exportDirs = new File(exportPath);
if (!exportDirs.exists()) {
exportDirs.mkdirs();
}
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + fileName), encoding));
getTemplate(tplName).process(data, writer);
}

/**
* 導(dǎo)出word文件到瀏覽器客戶端
*/
@Override
public void exportDocToClient(HttpServletResponse response, String fileName, String tplName, Map<String, Object> data) throws Exception {
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName , "UTF-8"));
// 把本地文件發(fā)送給客戶端
Writer out = response.getWriter();
Template template = getTemplate(tplName);
template.process(data, out);
out.close();
}
}

5、新建模板文件test.html

具體文件參考源碼,模板文件的制作方式,新建word文件調(diào)整格式后→另存為xml格式的文件→局部調(diào)整文件循環(huán)標(biāo)記→然后格式保存為html格式的文件 放在項目當(dāng)中去。

如果有不清楚的可以留言交流。

6、新建測試類ExportController.java

@RestController
@RequestMapping("/export")
public class ExportController {
@Autowired
private IExportService exportService;
@RequestMapping(value = "/testWord", method= RequestMethod.GET)
public void exportWord(HttpServletRequest request, HttpServletResponse response) throws Exception {
String fileName = "測試word導(dǎo)出.doc"; //文件名稱
// 設(shè)置頭部數(shù)據(jù)
Map<String,Object> dataMap = new HashMap<>();
dataMap.put("name","小明");
dataMap.put("regAddress","蘇州");
// 設(shè)置表格數(shù)據(jù)
List<ScoreVo> list=new ArrayList<>();
ScoreVo vo1=new ScoreVo();
vo1.setCourseName("英語");
vo1.setScore(95);
vo1.setRank(3);
ScoreVo vo2=new ScoreVo();
vo2.setCourseName("數(shù)學(xué)");
vo2.setScore(100);
vo2.setRank(1);
list.add(vo1);
list.add(vo2);
dataMap.put("courseList",list);
exportService.exportDocToClient(response, fileName, "test.html", dataMap);
}
}

訪問地址:http://localhost:8080/export/testWord

導(dǎo)出文件效果:

Gitee地址:https://gitee.com/hgm1989/springboot-email.git

責(zé)任編輯:武曉燕 來源: IT技術(shù)分享社區(qū)
相關(guān)推薦

2024-09-03 08:26:59

Spring格式模板

2011-06-23 09:13:20

JavaWord

2024-07-15 15:05:20

Python數(shù)據(jù)驅(qū)動

2017-07-06 14:32:27

靜態(tài)化FreeMarkerjava

2021-07-11 07:05:28

RedisSpringBoot用法

2023-01-11 15:11:36

SpringEhcache

2021-05-26 06:22:34

SpringBootJPA后端開發(fā)

2021-06-05 07:34:00

SpringBootMybatis用法

2012-03-06 15:34:05

JavaFreeMarker

2024-03-15 14:34:12

Oracle數(shù)據(jù)庫一鍵巡檢

2021-04-21 09:04:43

開發(fā)SpringBoot框架

2023-02-14 07:47:20

SpringBootEhcache

2009-08-07 09:19:13

云計算集成技術(shù)

2012-05-14 17:21:19

ibmdw

2009-09-01 11:25:08

C#讀取Word文件

2009-08-18 13:35:08

C#動態(tài)生成Word文

2024-11-29 12:58:13

2021-03-22 08:06:59

SpringBootSentinel項目

2021-06-25 10:05:58

SpringBootMySQL數(shù)據(jù)庫

2012-11-28 11:14:39

IBMdW
點贊
收藏

51CTO技術(shù)棧公眾號