Jacob工具类使用文件互转服务 word转html html转excel word转pdf excel转pdf ppt转pdf

  1. 安装Microsoft Office
  2. JDK 最低1.6
  3. jacob-1.18-M2-1.zip
  4. idea
  5. spring boot项目 

需求:银行的报表下载来有的是正规的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

 

 

 

 

 

源码地址https://gitee.com/javaqingchun/rocky-convert.gitide