需求:銀行的報表下載來有的是正規的excel 有的是不正規的表格 內部是html寫的,所以 產生了此業務. html
原來 用linux上的LibreOffice命令行方式將excel進行轉換,後來發現 轉換過程當中發現不支持多線程併發,轉換科學計數法會出問題.跟Microsoft Office不是一個級別的.最終方案 在一臺windows上搭建一個轉換服務,使用kafka 或httpClient方式進行服務. java
核心工具類:linux
package com.guige.base.util; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComFailException; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; import java.util.Date; /** * TODO * * @author songaw * @date 2018/5/17 9:54 */ public class JacobUtil { //word轉PDF private static final String wdFormatPDF="17"; //html轉xls private static final String htmlFormatExcel="51"; //PPT 轉pdf private static final String pptFormatPDF="32"; //excel轉PDF private static final String xlTypePDF="0"; /** * html轉excel * @param srcFilePath * @param targetFilePath * @return */ public static boolean htmlToExcel(String srcFilePath, String targetFilePath) { ActiveXComponent app = null; Dispatch excel = null; try { app = new ActiveXComponent("Excel.Application"); System.out.println("*****正在轉換...*****"); // 設置Excel應用程序不可見 app.setProperty("Visible", false); // Workbooks表示Excel程序的全部文檔窗口,( app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏 Dispatch excels = app.getProperty("Workbooks").toDispatch(); //打開要轉換的Excel文件 excel = Dispatch.call(excels, "Open", srcFilePath, false, true).toDispatch(); //設置兼容性檢查爲false Dispatch.put(excel, "CheckCompatibility", false); app.setProperty("DisplayAlerts",false); Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[]{targetFilePath, new Variant(htmlFormatExcel)}, new int[1]); app.setProperty("DisplayAlerts",true); System.out.println("*****轉換成功...*****"); return true; } catch (Exception e) { e.printStackTrace(); return false; } finally { if (excel != null) { Dispatch.call(excel, "Close", false); } if (app != null) { app.invoke("Quit", new Variant[] {}); //app.invoke("Quit", 0); } ComThread.Release(); } } /** * word轉pdf * @param srcFilePath * @param targetFilePath * @return */ public static boolean docToPdf(String srcFilePath, String targetFilePath) { ActiveXComponent app = null; Dispatch doc = null; try { ComThread.InitSTA(); app = new ActiveXComponent("Word.Application"); System.out.println("*****正在轉換...*****"); app.setProperty("Visible", false); Dispatch docs = app.getProperty("Documents").toDispatch(); doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { srcFilePath, new Variant(false), new Variant(true),//是否只讀 new Variant(false), new Variant("pwd") }, new int[1]).toDispatch(); //Dispatch.put(doc, "CheckCompatibility", false); //兼容性檢查,爲特定值false不正確 Dispatch.put(doc, "RemovePersonalInformation", false); app.setProperty("DisplayAlerts",false); Dispatch.call(doc, "ExportAsFixedFormat", targetFilePath, wdFormatPDF); // word保存爲pdf格式宏,值爲17 app.setProperty("DisplayAlerts",true); System.out.println("*****轉換成功...*****"); return true; // set flag true; } catch (Exception e) { e.printStackTrace(); return false; } finally { if (doc != null) { Dispatch.call(doc, "Close", false); } if (app != null) { app.invoke("Quit", 0); } ComThread.Release(); } } public static boolean pptToPdf(String srcFilePath, String pdfFilePath) { ActiveXComponent app = null; Dispatch ppt = null; try { ComThread.InitSTA(); app = new ActiveXComponent("PowerPoint.Application"); System.out.println("*****正在轉換...*****"); Dispatch ppts = app.getProperty("Presentations").toDispatch(); // 因POWER.EXE的發佈規則爲同步,因此設置爲同步發佈 ppt = Dispatch.call(ppts, "Open", srcFilePath, true,// ReadOnly true,// Untitled指定文件是否有標題 false// WithWindow指定文件是否可見 ).toDispatch(); //Dispatch.put(ppt, "CheckCompatibility", false); //兼容性檢查,爲特定值false不正確 app.setProperty("DisplayAlerts",false); Dispatch.call(ppt, "SaveAs", pdfFilePath, pptFormatPDF); //ppSaveAsPDF爲特定值32 app.setProperty("DisplayAlerts",true); System.out.println("*****轉換成功...*****"); return true; // set flag true; } catch (Exception e) { e.printStackTrace(); return false; } finally { if (ppt != null) { Dispatch.call(ppt, "Close"); } if (app != null) { app.invoke("Quit"); } ComThread.Release(); } } public static boolean excelToPDF(String srcFilePath, String pdfFilePath) { ActiveXComponent app = null; Dispatch excel = null; try { ComThread.InitSTA(true); app = new ActiveXComponent("Excel.Application"); System.out.println("*****正在轉換...*****"); long date = new Date().getTime(); app.setProperty("Visible", false); app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏 Dispatch excels = app.getProperty("Workbooks").toDispatch(); excel = Dispatch .invoke(excels, "Open", Dispatch.Method, new Object[] { srcFilePath, new Variant(false), new Variant(false) }, new int[9]) .toDispatch(); // 轉換格式 Dispatch.put(excel, "CheckCompatibility", false); app.setProperty("DisplayAlerts",false); Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[] { new Variant(0), // PDF格式=0 pdfFilePath, new Variant(xlTypePDF) // 0=標準 (生成的PDF圖片不會變模糊) 1=最小文件 // (生成的PDF圖片糊的一塌糊塗) }, new int[1]); app.setProperty("DisplayAlerts",true); System.out.println("*****轉換成功...*****"); // 這裏放棄使用SaveAs /* * Dispatch.invoke(excel,"SaveAs",Dispatch.Method,new Object[]{ * outFile, new Variant(57), new Variant(false), new Variant(57), * new Variant(57), new Variant(false), new Variant(true), new * Variant(57), new Variant(true), new Variant(true), new * Variant(true) },new int[1]); */ return true; } catch (Exception e) { e.printStackTrace(); // TODO: handle exception return false; }finally { if (excel != null) { Dispatch.call(excel, "Close", false); } if (app != null) { app.invoke("Quit", new Variant[] {}); //app.invoke("Quit", 0); } ComThread.Release(); } } }
各個轉換代碼: git
Excel操做 轉換 spring
xlAddIn 18 Microsoft Office Excel 加載項 xlAddIn8 18 Excel 2007 加載項 xlCSV 6 CSV xlCSVMac 22 Macintosh CSV xlCSVMSDOS 24 MSDOS CSV xlCSVWindows 23 Windows CSV xlCurrentPlatformText -4158 當前平臺文本 xlDBF2 7 DBF2 xlDBF3 8 DBF3 xlDBF4 11 DBF4 xlDIF 9 DIF xlExcel12 50 Excel 12 xlExcel2 16 Excel 2 xlExcel2FarEast 27 Excel2 FarEast xlExcel3 29 Excel3 xlExcel4 33 Excel4 xlExcel4Workbook 35 Excel4 工做簿 xlExcel5 39 Excel5 xlExcel7 39 Excel7 xlExcel8 56 Excel8 xlExcel9795 43 Excel9795 xlHtml 44 HTML 格式 xlIntlAddIn 26 國際加載項 xlIntlMacro 25 國際宏 xlOpenXMLAddIn 55 打開 XML 加載項 xlOpenXMLTemplate 54 打開 XML 模板 xlOpenXMLTemplateMacroEnabled 53 打開啓用的 XML 模板宏 xlOpenXMLWorkbook 51 打開 XML 工做簿 xlOpenXMLWorkbookMacroEnabled 52 打開啓用的 XML 工做簿宏 xlSYLK 2 SYLK xlTemplate 17 模板 xlTemplate8 17 模板 8 xlTextMac 19 Macintosh 文本 xlTextMSDOS 21 MSDOS 文本 xlTextPrinter 36 打印機文本 xlTextWindows 20 Windows 文本 xlUnicodeText 42 Unicode 文本 xlWebArchive 45 Web 檔案 xlWJ2WD1 14 WJ2WD1 xlWJ3 40 WJ3 xlWJ3FJ3 41 WJ3FJ3 xlWK1 5 WK1 xlWK1ALL 31 WK1ALL xlWK1FMT 30 WK1FMT xlWK3 15 WK3 xlWK3FM3 32 WK3FM3 xlWK4 38 WK4 xlWKS 4 工做表 xlWorkbookDefault 51 默認工做簿 xlWorkbookNormal -4143 常規工做簿 xlWorks2FarEast 28 Works2 FarEast xlWQ1 34 WQ1 xlXMLSpreadsheet 46 XML 電子表格 word 操做 轉換 //0:Microsoft Word 97 - 2003 文檔 (.doc) //1:Microsoft Word 97 - 2003 模板 (.dot) //2:文本文檔 (.txt) //3:文本文檔 (.txt) //4:文本文檔 (.txt) //5:文本文檔 (.txt) //6:RTF 格式 (.rtf) //7:文本文檔 (.txt) //8:HTML 文檔 (.htm)(帶文件夾) //9:MHTML 文檔 (.mht)(單文件) //10:MHTML 文檔 (.mht)(單文件) //11:XML 文檔 (.xml) //12:Microsoft Word 文檔 (.docx) //13:Microsoft Word 啓用宏的文檔 (.docm) //14:Microsoft Word 模板 (.dotx) //15:Microsoft Word 啓用宏的模板 (.dotm) //16:Microsoft Word 文檔 (.docx) //17:PDF 文件 (.pdf) //18:XPS 文檔 (.xps) //19:XML 文檔 (.xml) //20:XML 文檔 (.xml) //21:XML 文檔 (.xml) //22:XML 文檔 (.xml) //23:OpenDocument 文本 (.odt) //24:WTF 文件 (.wtf)
看看效果:windows
excel轉PDF多線程
PPF轉PDF併發
html轉xlsapp