springboot~aspose操作word模板实现导出功能
事情是这样的,系统有这样一个需求,有一些单子供客户下载打印,做为凭证,而这些单子一般属于word格式的,里面的排版非常固定,只是上面的内容不同,这就属于word模板的范畴了,目前比较不好的操作word的组件就是aspose了,下面我来说一下它的使用方法。
word模板
主要使用了word里的域,然后选择“邮件合并”,在“域名”处输入你的word变量名,然后在java代码里为这个变量赋值就可以了
添加组件引用
把组件放到resource/lib目录下
<dependency>
<groupId>com.bdyh.common</groupId>
<artifactId>common</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
代码生成
aspose组件存在授权问题,没有授权的会有水印出现
private static InputStream license;
private static InputStream fileInput;
public static void generateApproveForm(HttpServletResponse response,
List<CompanyLawyerEducationAddDTO> counterpartDetails) {
// 验证License
if (!getLicense("templates/companyLawyerApprove.docx")) {
return;
}
try {
long old = System.currentTimeMillis();
Document doc = new Document(fileInput);
//主要调用aspose.words的邮件合并接口MailMerge
//3.1 填充单个文本域
String[] Flds = new String[]{"Title", "Name", "URL", "Note"}; //文本域
Object[] Vals = new Object[]{"标题", "测试", "http://test.com", word模板导出"}; //值
doc.getMailMerge().execute(Flds, Vals); //调用接口
response.setHeader("Content-Disposition", "attachment; filename=审批单.pdf");
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream output = response.getOutputStream();
doc.save(output, SaveFormat.PDF);
output.flush();
output.close();
}
public static boolean getLicense(String templateName) {
boolean result = false;
try {
license = new ClassPathResource("lib/license.xml").getInputStream();
fileInput = new ClassPathResource(templateName).getInputStream();
License aposeLic = new License();
aposeLic.setLicense(license);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
以上模板是最简单的文本域的,如果有兴趣还可以把表格域也放上去,实现列表的输出等。
合集:
springboot(1)
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-03-07 ELK系列~Fluentd对大日志的处理过程~16K
2016-03-07 Bootstrap~日期控制
2014-03-07 JS~重写alter与confirm,让它们变成fancybox风格
2014-03-07 JS~Boxy和JS模版实现一个标准的消息提示框
2013-03-07 EF架构~简洁关联表插入,优越的代码性能!