标签:docx Word Spring Boot new template paramMap word 模板
Maven 加入如下坐标
<!--导出Docx-->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.11.0</version>
</dependency>
<!--导出Docx-->
/**
* word工具类
*
* @author mijunl
*/
public class WordUtil {
/**
* 根据模板填充内容生成word,并下载
*
* @param templatePath word模板文件路径
* @param paramMap 替换的参数集合
*/
public static void downloadWord(OutputStream out, InputStream templatePath, Map<String, Object> paramMap, Configure config) throws Exception {
/*
// 生成本地文件
Long time = System.currentTimeMillis();
// 生成的word格式
String formatSuffix = ".docx";
// 拼接后的文件名
String fileName = time + formatSuffix;
//设置生成的文件存放路径,可以存放在你想要指定的路径里面
String rootPath = "D:/tmp/" + File.separator + "file/word/";
String filePath = rootPath + fileName;
File newFile = new File(filePath);
//判断目标文件所在目录是否存在
if (!newFile.getParentFile().exists()) {
//如果目标文件所在的目录不存在,则创建父目录
newFile.getParentFile().mkdirs();
}
*/
// 读取模板templatePath并将paramMap的内容填充进模板,即编辑模板(compile)+渲染数据(render)
XWPFTemplate template = XWPFTemplate.compile(templatePath, config).render(
paramMap);
// 生成本地文件
//out = new FileOutputStream(filePath);//输出路径(下载到指定路径)
// 将填充之后的模板写入filePath
//将template写到OutputStream中
template.write(out);
out.flush();
out.close();
template.close();
}
/**
* 远程文件url 转为输入流
* @param urlOrPath 远程文件url
* @return
* @throws Exception
*/
public static InputStream byteByUrl(String urlOrPath) throws Exception {
URL url = new URL(urlOrPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3 * 1000);
//防止屏蔽程序抓取而返回403错误
//conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
return conn.getInputStream();
}
}
/**
* WordController
* @author CoderKK
*/
@RestController
public class WordController {
@GetMapping("/word-download")
public void word(HttpServletResponse response) throws Exception {
response.setContentType("application/octet-stream");
//设置文件名称
response.setHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis() + ".docx");
//获取 resources 目录 本地&打Jar包都可用 路径前不需要加/
//InputStream inputStream = getClass().getClassLoader().getResourceAsStream("word/template.docx");
//路径前需要加/ 具体区别请看https://www.cnblogs.com/geek233/p/15817853.html
InputStream inputStream = getClass().getResourceAsStream("/word/template.docx");
//获取远程文件
//InputStream inputStream = WordUtil.byteByUrl("https://blog.52ipc.top/word/template.docx");
// 创建用于插入数据的Map 实际开发要放在service层
Map<String, Object> paramMap = new HashMap<>(18);
paramMap.put("title", "Hi, Word模板");
paramMap.put("uname", "小蜜蜂~");
List<Map<String,Object>> voList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Map<String,Object> vo = new HashMap<>();
vo.put("areaId" , String.valueOf(i));
vo.put("name" , "北京"+ i);
voList.add(vo);
}
paramMap.put("area", voList);
// 循环行表渲染策略
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
// 插件绑定
Configure config = Configure.builder()
//area 变量名
//policy 策略
.bind("area", policy)
.build();
WordUtil.downloadWord(response.getOutputStream(), inputStream, paramMap, config);
}
}
word模板文件路径如下 模板文件下载链接 https://blog.52ipc.top/word/template.docx
模板内容如下图
标签:docx,Word,Spring,Boot,new,template,paramMap,word,模板 来源: https://www.cnblogs.com/geek233/p/15826227.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。