java word 模板_java通过word模板生成word文档

这篇博客介绍如何使用Java从Word模板生成文档。通过HashMap存储数据,包括文本、表格、有序列表和图片,然后利用XWPFTemplate库将数据渲染到模板中,最终输出为新的Word文档。
摘要由CSDN通过智能技术生成

public static void main(String[] args) {

//模板、文件、图片路径

String workPath=System.getProperty("user.dir") + "/static-utils/src/main/resources/word/";

String templateName="test.docx";

Map datas = new HashMap() {

{

//文本

put("name","xiaoguo");

put("sex","男");

put("year","20200105");

put("hello","xiaoguo写于2020年一月");

//自定义表格

RowRenderData header = RowRenderData.build(new TextRenderData("1C86EE", "姓名"), new TextRenderData("1C86EE", "学历"));

RowRenderData row0 = RowRenderData.build("张三", "研究生");

RowRenderData row1 = RowRenderData.build("李四", "博士");

RowRenderData row2 = RowRenderData.build("王五", "博士后");

put("tables", new MiniTableRenderData(header, Arrays.asList(row0, row1, row2)));

//自定义有序列表

put("testText", new NumbericRenderData(NumbericRenderData.FMT_DECIMAL, new ArrayList() {

{

add(new TextRenderData("Plug-in grammar"));

add(new TextRenderData("Supports word text, header..."));

add(new TextRenderData("Not just templates, but also style templates"));

}

}));

//网落图片

put("picture", new PictureRenderData(200, 150, ".jpg", BytePictureUtils.getUrlBufferedImage("https://gss3.bdstatic.com/7Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=61c551093f6d55fbd1cb7e740c4b242f/d8f9d72a6059252d937820d3369b033b5ab5b9fd.jpg")));

//本地图片

put("picture2", new PictureRenderData(200, 150, ".jpg", BytePictureUtils.getLocalByteArray(new File(workPath + "c1.jpg"))));

}

};

generateWord(datas, workPath + templateName, workPath);

}

/**

* 通过word模板并生成word文档

*

* @param paramData    参数数据

* @param templatePath word模板地址加模板文件名字

* @param outFilePath  输出文件地址(不带文件名字)

* @return 生成的word文件

*/

public static File generateWord(Map paramData, String templatePath, String outFilePath) {

String outFileName = "word_" + System.currentTimeMillis() + "_" + random.nextInt(100) + ".doc";

return generateWord(paramData, templatePath, outFilePath, outFileName);

}

/**

* 通过word模板并生成word文档

*

* @param paramData    参数数据

* @param templatePath word模板地址加模板文件名字

* @param outFilePath  输出文件地址(不带文件名字)

* @param outFileName  输出文件名字

* @return 生成的word文件

*/

public static File generateWord(Map paramData, String templatePath, String outFilePath, String outFileName) {

//判断输出文件路径和文件名是否含有指定后缀

outFilePath = CommonUtil.addIfNoSuffix(outFilePath, "/", "\\");

outFileName = CommonUtil.addIfNoSuffix(outFileName, ".doc", ".docx");

//解析word模板

XWPFTemplate template = XWPFTemplate.compile(templatePath).render(paramData);

//输出文件

FileOutputStream out = null;

File outFile = new File(outFilePath + outFileName);

try {

out = new FileOutputStream(outFile);

template.write(out);

out.flush();

} catch (IOException e) {

log.error("生成word写入文件失败", e);

} finally {

if (template != null) {

try {

template.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (out != null) {

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return outFile;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值