poi-tl根据模板生成word文件

基础操作

  • 本质上是通过占位符进行内容替换
  • 本文章仅操作docx格式的文档
    .doc (Word 97-2003): 使用OLE2格式,对应POI的 HWPF 组件
    .docx (Word 2007+): 使用OOXML格式,对应POI的 XWPF 组件

基础操作_模板部分

  • 将模板放入resources 资源目录下,自定义文件夹中,例如:templet/word_template/demo_template.docx

代码部分

  • maven

    xml 复制代码
    		<!-- 用于生成word版报告 -->
    		<dependency>
    		    <groupId>com.deepoove</groupId>
    		    <artifactId>poi-tl</artifactId>
    		    <version>1.12.1</version>
    		</dependency>
  • 处理过程

    java 复制代码
    	// 模板文件
        String templateFilePath = "templet/word_template/demo_template.docx";
        // 读取模板文件
        InputStream templateIn = getClass().getClassLoader().getResourceAsStream(templateFilePath);
    
        // 插入文本数据
        Map<String, Object> templateData = new HashMap<String, Object>();
        templateData.put("str1", "替换成功");
    
        // 生成模板文件
        XWPFTemplate template = XWPFTemplate.compile(templateIn).render(templateData);
        
        // 写入数据并关闭流
        template.writeAndClose(new FileOutputStream("D:/output.docx"));
        templateIn.close();
  • 此时,文件便下载到了 D:/output.docx 的位置。

  • 若为Web

java 复制代码
public void pgdExport(HttpServletResponse response) throws IOException {

        // 模板文件
        String templateFilePath = "templet/word_template/demo_template.docx";
        // 读取模板文件
        InputStream templateIn = getClass().getClassLoader().getResourceAsStream(templateFilePath);

        // 插入文本数据
        Map<String, Object> templateData = new HashMap<String, Object>();
        templateData.put("str1", "替换成功");

        // 生成模板文件
        XWPFTemplate template = XWPFTemplate.compile(templateIn).render(templateData);
        ServletOutputStream outputStream = response.getOutputStream();

        // 防止文件以及文件名乱码
        String fileName = "文件名.docx";
        String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"; filename*=utf-8''" + encodedFileName);
        response.setCharacterEncoding("UTF-8");

			  // 写入数据并关闭流
        template.writeAndClose(outputStream);
        templateIn.close();
    }
js 复制代码
// 前端js代码
// 如果此接口需要被鉴权,那么需要后端同时从Params中获取token,然后前端拼接token传递,否则此方法无法使用
// 需要注意跨域问题
window.location.href =  '你的接口地址';

插入图片

  • 模板:占位符格式是{{@xxxx}}
  • 代码
java 复制代码
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;

templateData.put("img", Pictures.ofStream(in, PictureType.JPEG).size(300, 300).create());

参考文章

相关推荐
骆驼爱记录5 天前
WPS页码设置:第X页共Y-1页
自动化·word·excel·wps·新人首发
2301_816997885 天前
Word 清除格式的方法
word
微光feng6 天前
毕业论文word引用操作汇总
word·目录·公式·毕业论文·交叉引用·题注
2301_816997886 天前
Word 功能区与快速访问工具栏
word
halen3336 天前
Hellowordl: The Masters Tool for Word Puzzle Enthusiasts
word
lpfasd1236 天前
Markdown 导出 Word 文档技术方案
开发语言·c#·word
Cxiaomu6 天前
Python 文件解析: Excel / Word / PDF 的解析、处理、预览与下载
python·word·excel
bu_shuo6 天前
Word中插入文本内容控件并交叉引用
word·内容控件
缺点内向6 天前
C#中如何创建目录(TOC):使用Spire.Doc for .NET实现Word TOC自动化
c#·自动化·word·.net
2301_816997886 天前
Word 创建打开与保存文档
c#·word·xhtml