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();
}