Java读取word模板并在模板相关位置插入数据和表格
来源:互联网 发布:金知元 知乎 编辑:程序博客网 时间:2024/05/22 06:05
这两天一直在实现这样一个功能,即从数据库及其他数据源中获取相关数据,填充到一定的word模板中,然后将该模板下载下来(主要就是为了以简单的方式让老师看到他想看的系统中不同功能不同位置地方的数据)
最终这个功能是使用 freemarker实现的,真的很方便也很强大(也许是我孤陋寡闻了)
步骤:
首先创建要下载的模板,在需要填充数据的地方用${name}来代替。
如下图所示:
2、将doc文件另存为xml文件。直接另存为即可。
3、然后将xml文件重命名为ftl文件,我使用的是firstobject-XML-editor这个软件来编辑ftl文件。比如在需要插入表格的地方添加如下的代码:
为了让代码可读性变高,可以按F8进行代码的校验。
4.、模板准备工作完成,开始写代码
首先把要填充的数据封装好,使用的是Map容器进行封装的,如下图所示
得到的数据类似于下面
5、所有数据封装完毕后,开始将数据填充到模板中,调用下列的类及其create函数,create具体实现如下图所示:
public static File create(Map<?, ?> dataMap, String templateName,String filePath, String fileName) {System.out.println();File file = null;try {Version version = new Version("2.3.22");//创建配置实例Configuration configuration = new Configuration(version);//设置编码configuration.setDefaultEncoding("UTF-8");//ftl模板文件统一放至"/com/glwf/template/" 包下面configuration.setClassForTemplateLoading(WordUtils.class,"/com/glwf/template/"); //获取模板Template template = configuration.getTemplate(templateName);//输出文件File outFile = new File(filePath + File.separator + fileName); //如果输出目标文件夹不存在,则创建if (!outFile.getParentFile().exists()) {outFile.getParentFile().mkdirs();}//将模板和数据模型合并生成文件Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));//生成文件template.process(dataMap, out);//关闭流out.flush();out.close();file = outFile.getAbsoluteFile();} catch (TemplateException e) {log.error("TemplateException");} catch (TemplateNotFoundException e) {log.error("TemplateNotFoundException");} catch (MalformedTemplateNameException e) {log.error("MalformedTemplateNameException");} catch (ParseException e) {log.error("ParseException");} catch (IOException e) {log.error("IOException");}return file;}6、文件生成,下载文件
if(file != null){/** * start down load the zip file * 开始下载 */System.out.println("开始下载。。。。。");HttpServletResponse response = this.getResponse();response.setContentType("application/x-download");response.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes(), "ISO-8859-1"));OutputStream outp = null;FileInputStream in = null;outp = response.getOutputStream();in = new FileInputStream(file);byte[] b = new byte[1024];int i = 0;while ((i = in.read(b)) > 0) {outp.write(b, 0, i);}outp.close();log.info("down load success");file.delete();log.info("delete files success");}else{System.out.println("下载失败");log.error("zip file is not exist");}
阅读全文
0 0
- Java读取word模板并在模板相关位置插入数据和表格
- 读取word模板,并写入数据到word文件中
- C#动态向Word模板插入表格
- java在pdf模板的指定位置插入图片
- java word 模板 数据填充
- Latex插图和插入表格代码模板
- win form 操作word模板插入文字、图片及表格
- Java操作word模板插入图片
- java 读word文档模板,同时往word文档里面插入数据
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- Delphi中的数据添加到Word模板的相应位置
- 在word中插入excel表格并自动更新
- Java 读取.xls模板并另存为.xls
- word模板插入文字、图片
- 用C#实现动态生成Word文档,在Word文档中插入表格,并将读出的数据填入到表格中
- Java操作word模板插入多种编号的列表
- C#读取Word表格数据
- java导出word模板
- hdu4289 最大流最小割
- HDU1698:Just a Hook(线段树)
- 初识框架之struts2(三。登录页面(初级)+拦截器)
- 1067. 试密码
- 数据库
- Java读取word模板并在模板相关位置插入数据和表格
- FilenameFilter介绍
- 集合模拟斗地主
- Android出现In <declare-styleable> MenuView, unable to find attribute android:preserveIconSpacing错误信息
- lintcode刷题——两个链表的交叉
- 第十七天总结
- Android studio设置(1)
- Understanding Subjects in RxJS
- 闭关一年的干货与朋友们分享 先预告一下