天天看点

java freemarker+jacob生成wordfreemarker模板jacob xml转为docx

freemarker模板

<dependency>
  <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.28</version>
</dependency>
<dependency>
    <groupId>net.sf.jacob-project</groupId>
    <artifactId>jacob</artifactId>
    <version>1.19.3</version>
</dependency>
           
// 设置FreeMarker的版本和编码格式
Configuration configuration = new Configuration(new Version("2.3.28"));
configuration.setDefaultEncoding("UTF-8");

// 设置FreeMarker生成Word文档所需要的模板
configuration.setClassForTemplateLoading(this.getClass(), "/ftl");
Template t = configuration.getTemplate("summary.ftl", "UTF-8");
// 创建一个Word文档的输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer out = new BufferedWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
//FreeMarker使用Word模板和数据生成Word文档
t.process(dataMap, out);
out.flush();
out.close();
byte[] buf = baos.toByteArray();
           

jacob xml转为docx

jacob下载

public class WordUtils {
	//xml转为docx
    public static void xml2docx1(String xmlFile, String docxFile) {
        ComThread.InitSTA();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");//Word.Application代表COM OLE编程标识,可查询MSDN得到
            app.setProperty("Visible", false);//设置Word不可见
            Dispatch docs = app.getProperty("Documents").toDispatch();//调用Application对象的Documents属性,获得Documents对象
            doc = Dispatch.call(docs,"Open", xmlFile, new Variant(false), new Variant(true)).getDispatch();

            File file = new File(docxFile);
            if (file.exists())
                file.delete();

            //更新目录,有两个方法:Update 更新域,UpdatePageNumbers 只更新页码
            Dispatch activeDocument = app.getProperty("ActiveDocument").toDispatch();
            Dispatch tablesOfContents = Dispatch.get(activeDocument,"TablesOfContents").toDispatch();//获取目录
            Dispatch tablesOfContent = Dispatch.call(tablesOfContents, "Item", new Variant(1)).toDispatch();//获取第一个目录。若有多个目录,则传递对应的参数
            Dispatch.call(tablesOfContent, "Update");

            //设置目录字体
            Dispatch tableOfContentsRange = Dispatch.get(tablesOfContent, "Range").toDispatch();
            Dispatch.call(tableOfContentsRange, "Select");//选中文档内容(不选中无法修改)
            Dispatch selection = Dispatch.get(app, "Selection").toDispatch();
            Dispatch font = Dispatch.get(selection, "Font").toDispatch();//字体
            Dispatch.put(font, "Name" , new Variant("微软雅黑"));//设置字体,只能设置系统中存在的字体

            Dispatch.call(doc, "SaveAS", docxFile, 12);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (doc != null)
                Dispatch.call(doc, "Close", false);//关闭打开的Word文件
            if (app != null)
                app.invoke("Quit",0);//关闭Word应用程序
        }
        ComThread.Release();
    }

	//word转为pdf
    public static void docx2Pdf(String docxFile, String pdfFile) {
        ComThread.InitSTA();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.call(docs, "Open", docxFile).toDispatch();

            File tofile = new File(pdfFile);
            if (tofile.exists()) {
                tofile.delete();
            }

            Dispatch.call(doc, "SaveAs", pdfFile, 17);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (doc != null)
                Dispatch.call(doc, "Close", false);
            if (app != null)
                app.invoke("Quit", new Variant[] {});
        }
        ComThread.Release();// 如果没有这句话,winword.exe进程将不会关闭
    }

    public static void xml2docx2(String xmlFilePath, String docxFilePath, String pictureFilePath) {
        ComThread.InitSTA();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.call(docs, "Open", xmlFilePath).toDispatch();

            Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();//所有表格
            Dispatch table = Dispatch.call(tables, "Item", new Variant(1)).toDispatch();//获取第1个表格

            //表格中填充图片
            File imageDir = new File(pictureFilePath);
            File[] files = imageDir.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    File imageFile = files[i];
                    Dispatch cell = Dispatch.call(table, "Cell", new Variant(i + 2), new Variant(4)).toDispatch();
                    Dispatch cellRange = Dispatch.get(cell, "Range").toDispatch();
                    Dispatch image = Dispatch.get(cellRange, "InLineShapes").toDispatch();
                    Dispatch.call(image, "AddPicture", pictureFilePath + "/" + imageFile.getName());
                }
            }

            File file = new File(docxFilePath);
            if (file.exists())
                file.delete();

            //更新目录,有两个方法:Update 更新域,UpdatePageNumbers 只更新页码
            Dispatch activeDocument = app.getProperty("ActiveDocument").toDispatch();
            Dispatch tablesOfContents = Dispatch.get(activeDocument,"TablesOfContents").toDispatch();//获取目录
            Dispatch tablesOfContent = Dispatch.call(tablesOfContents, "Item", new Variant(1)).toDispatch();//获取第一个目录。若有多个目录,则传递对应的参数
            Dispatch.call(tablesOfContent, "Update");

            //设置目录字体
            Dispatch tableOfContentsRange = Dispatch.get(tablesOfContent, "Range").toDispatch();
            Dispatch.call(tableOfContentsRange, "Select");//选中文档内容(不选中无法修改)
            Dispatch selection = Dispatch.get(app, "Selection").toDispatch();
            Dispatch font = Dispatch.get(selection, "Font").toDispatch();//字体
            Dispatch.put(font, "Name" , new Variant("微软雅黑"));//设置字体,只能设置系统中存在的字体

            Dispatch.call(doc, "SaveAS", docxFilePath, 12);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (doc != null)
                Dispatch.call(doc, "Close", false);
            if (app != null)
                app.invoke("Quit", new Variant[] {});
        }
        ComThread.Release();
    }
    
    
    public static void xml2docx3(String xmlFile, String docxFile) {
        ComThread.InitSTA();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");//Word.Application代表COM OLE编程标识,可查询MSDN得到
            app.setProperty("Visible", false);//设置Word不可见
            Dispatch docs = app.getProperty("Documents").toDispatch();//调用Application对象的Documents属性,获得Documents对象
            doc = Dispatch.call(docs,"Open", xmlFile, new Variant(false), new Variant(true)).getDispatch();

            File file = new File(docxFile);
            if (file.exists())
                file.delete();

            Dispatch.call(doc, "SaveAS", docxFile, 12);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (doc != null)
                Dispatch.call(doc, "Close", false);//关闭打开的Word文件
            if (app != null)
                app.invoke("Quit",0);//关闭Word应用程序
        }
        ComThread.Release();
    }