Jacob 另存为Word、Excel
import java.util.Random; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class JacobUtil { public static final int WORD_HTML = 8; public static final int WORD_TXT = 7; public static final int EXCEL_HTML = 44; public static final int EXCEL_XML = 46; public static final int EXCEL_43 = 43; // Excel 2003 测试可用 /** * WORD转HTML * * @param docfile * WORD文件全路径 * @param htmlfile * 转换后HTML存放路径 */ public static void wordToHtml(String docfile, String htmlfile) { // 初始化 ComThread.InitSTA(); ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word try { app.setProperty("Visible", new Variant(false)); Dispatch docs = app.getProperty("Documents").toDispatch(); Dispatch doc = Dispatch.invoke( docs, "Open", Dispatch.Method, new Object[] { docfile, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { htmlfile, new Variant(WORD_HTML) }, new int[1]); Variant f = new Variant(false); Dispatch.call(doc, "Close", f); } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); ComThread.Release(); } } /** * EXCEL转HTML * * @param xlsfile * EXCEL文件全路径 * @param htmlfile * 转换后HTML存放路径 */ public static void excelToHtml(String xlsfile, String htmlfile) { // 初始化 ComThread.InitSTA(); ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动Excel try { app.setProperty("Visible", new Variant(false)); Dispatch excels = app.getProperty("Workbooks").toDispatch(); Dispatch excel = Dispatch.invoke( excels, "Open", Dispatch.Method, new Object[] { xlsfile, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] { htmlfile, new Variant(EXCEL_HTML) }, new int[1]); Dispatch.call(excel, "Close", new Variant(false)); } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); ComThread.Release(); } } /** * EXCEL转XML * * @param xlsfile * EXCEL文件全路径 * @param xmlfile * 转换后XML存放路径 */ public static void excelToXml(String xlsfile, String xmlfile) { // 初始化 ComThread.InitSTA(); ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动Excel try { app.setProperty("Visible", new Variant(false)); Dispatch excels = app.getProperty("Workbooks").toDispatch(); Dispatch excel = Dispatch.invoke( excels, "Open", Dispatch.Method, new Object[] { xlsfile, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] { xmlfile, new Variant(EXCEL_XML) }, new int[1]); Variant f = new Variant(false); Dispatch.call(excel, "Close", f); } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); ComThread.Release(); } } public static void main(String[] args) { excelToHtml( "E:\\test.xls", "E:\\" + new Random().nextInt(1000) + ".html"); } }
附: XlFileFormat Enumeration
Name Value Description
xlAddIn 18 Microsoft Excel 97-2003 Add-In
xlAddIn8 18 Microsoft Excel 97-2003 Add-In
xlCSV 6 CSV
xlCSVMac 22 Macintosh CSV
xlCSVMSDOS 24 MSDOS CSV
xlCSVWindows 23 Windows CSV
xlCurrentPlatformText -4158 Current Platform Text
xlDBF2 7 DBF2
xlDBF3 8 DBF3
xlDBF4 11 DBF4
xlDIF 9 DIF
xlExcel12 50 Excel12
xlExcel2 16 Excel2
xlExcel2FarEast 27 Excel2 FarEast
xlExcel3 29 Excel3
xlExcel4 33 Excel4
xlExcel4Workbook 35 Excel4 Workbook
xlExcel5 39 Excel5
xlExcel7 39 Excel7
xlExcel8 56 Excel8
xlExcel9795 43 Excel9795
xlHtml 44 HTML format
xlIntlAddIn 26 International Add-In
xlIntlMacro 25 International Macro
xlOpenDocumentSpreadsheet 60 OpenDocument Spreadsheet
xlOpenXMLAddIn 55 Open XML Add-In
xlOpenXMLTemplate 54 Open XML Template
xlOpenXMLTemplateMacroEnabled 53 Open XML Template Macro Enabled
xlOpenXMLWorkbook 51 Open XML Workbook
xlOpenXMLWorkbookMacroEnabled 52 Open XML Workbook Macro Enabled
xlSYLK 2 SYLK
xlTemplate 17 Template
xlTemplate8 17 Template 8
xlTextMac 19 Macintosh Text
xlTextMSDOS 21 MSDOS Text
xlTextPrinter 36 Printer Text
xlTextWindows 20 Windows Text
xlUnicodeText 42 Unicode Text
xlWebArchive 45 Web Archive
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 Worksheet
xlWorkbookDefault 51 Workbook default
xlWorkbookNormal -4143 Workbook normal
xlWorks2FarEast 28 Works2 FarEast
xlWQ1 34 WQ1
xlXMLSpreadsheet 46 XML Spreadsheet
上一篇: 在中国怎么娶两个老婆
下一篇: 一个数据库连接Java工具类和数据迁移类
推荐阅读
-
微信小程序实现上传word、txt、Excel、PPT等文件功能
-
asp.net+Ligerui实现grid导出Excel和Word的方法
-
使用NOPI读取Word、Excel文档内容
-
微信小程序实现上传word、txt、Excel、PPT等文件功能
-
使用NOPI读取Word、Excel文档内容
-
asp.net+Ligerui实现grid导出Excel和Word的方法
-
利用POI读取word、Excel文件的最佳实践教程
-
WinForm导出文件为Word、Excel、文本文件的方法
-
让谷歌浏览器打开Word2003, Excel和PPT2003文档的设置方法
-
直接在线预览Word、Excel、TXT文件之ASP.NET