您好,欢迎来到爱问旅游网。
搜索
您的当前位置:首页Java生成复杂word文档 结合freemarker+docx4j+POI

Java生成复杂word文档 结合freemarker+docx4j+POI

来源:爱问旅游网

        简单说下java生成word文档的各个组件优缺点(详细网上有很多),POI、JXL等过于原生,如果制作简单的几页word文档还能接受,如果文档十几二十页。。。会头疼死,并且word一旦大了以后,样式很容易乱,很不美观;freemarker利用模板生成word文档,开发相对简单,但是freemarker是利用xml标签传入模板的,一旦在模板里加了部分标签(例如 list),模板就不可以用office打开了,再去调整模板样式,或者增加内容,又或者需要增加很多list,会无从下手,除非对xml标签很熟练。。。。

所以以下所有工作均是针对复杂的word文档

        首先说下报告制作的总体思路:使用freemarker进行模板式开发,以数据列表为界限(或者章节,具体以业务为准)将word模板拆分为多个模板,方便后期业务要求调整模板,docx4j进行多个文档合并,POI调整文档细节或者插入图片等。

注意事项:

下面是封装的相关方法(都是我实际代码用的,拷贝时请稍加调整):

1、创建单个docx文档:

/**
	 * 
	 * <p>描述: 生成docx文件</p>  
	 * @author XXX  
	 * @date 2018年8月13日  
	 * @param templateName  模板名称 (请勿带后缀)
	 * @param templatePath  模板路径:请从templates下开始填写
	 * @param userCode	用户名称:用以区分生成的文件
	 * @param sign	标记创建报告的类型 (gjfs-国检分省,)
	 * @param date	报告创建日期:请精确至秒
	 * @param data	报告填充的数据
	 * @return
	 * @throws FileNotFoundException
	 */
	public static String creatDocx(String templateName,String templatePath,String userCode,String sign,String date,Map<String,Object> data) throws FileNotFoundException {

		//获取跟目录
		File path = new File(ResourceUtils.getURL("classpath:").getPath());

		//如果上传目录为/static/images/upload/,则可以如下获取:
		File upload = new File(path.getAbsolutePath(),"templates/"+templatePath);
		if(!upload.exists()) upload.mkdirs();
		System.out.println("upload url:"+upload.getAbsolutePath());

		// 路径
		String templatepath = upload.getAbsolutePath();
		String docxname = "test.docx";//空白docx文件即可
		//String templateName = "test.xml";模板名称

		//结果文件
		String resxmlpath = "d:/report/"+userCode+"/sign/"+templateName+"(date).xml";
		String reswordpath = "d:/report/"+userCode+"/sign/"+templateName+"(date).docx";

		//如果文件不存在则自动创建
		File xmlFile = new File("d:/report/"+userCode+"/sign/");
		File docxFile = new File("d:/report/"+userCode+"/sign/");

		if(!xmlFile.exists()) xmlFile.mkdirs();
		if(!docxFile.exists()) docxFile.mkdirs();

		// 生成文档
		try {
			File file = generate(templatepath, docxname, templateName+".xml", resxmlpath, reswordpath, data);
			return reswordpath;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
private static File generate(String templatepath, String docxname, String xmlname,
			String resxmlpath, String reswordpath, Map<String, Object> param) throws Exception {
		Configuration cfg = new Configuration();
		cfg.setDirectoryForTemplateLoading(new File(templatepath));
		Template template = cfg.getTemplate(xmlname);
		template.setOutputEncoding("UTF-8");
		Writer out = new FileWriter(new File(resxmlpath));
		// 数据放到模板xml里面,生成带数据的xml
		template.process(param, out);
		if (out != null) {
			out.close();
		}
		// 带数据的xml生成docx
		File file = new File(resxmlpath);
		File docxFile = new File(templatepath + "/" + docxname);
		ZipFile zipFile = new ZipFile(docxFile);
		Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries();
		ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(reswordpath));
		int len = -1;
		byte[] buffer = new byte[1024];
		while (zipEntrys.hasMoreElements()) {
			ZipEntry next = zipEntrys.nextElement();
			InputStream is = zipFile.getInputStream(next);
			// 把输入流的文件传到输出流中 如果是word/document.

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- awee.cn 版权所有 湘ICP备2023022495号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务