天天看點

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

繼續閱讀