1. 程式人生 > >java呼叫Excel巨集並儲存更改

java呼叫Excel巨集並儲存更改

設定

64位電腦:將jacob-1.17-M2-x64.dll放到專案使用的jdk的bin目錄下,將jacob.jar放到專案的lib下

若執行時出現java.lang.NoClassDefFoundError: Could not initialize class com.jacob.com.ComThread錯誤,將jacob-1.17-M2-x64.dll放到專案使用的jdk的jre/bin目錄下再重啟服務

工具類

package com.hikvision.service.quote.util;

import com.jacob.activeX.ActiveXComponent;
import
com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class JacobExcelTool { private ActiveXComponent xl = null; // Excel物件 private Dispatch workbooks = null; // 工作簿物件 private Dispatch workbook = null; // 具體工作簿 private Dispatch sheets = null;// 獲得sheets集合物件
private Dispatch currentSheet = null;// 當前sheet public ActiveXComponent getXl() { return xl; } public Dispatch getWorkbooks() { return workbooks; } public Dispatch getWorkbook() { return workbook; } /** * * 開啟excel檔案 * @param
filepath 檔案路徑名稱 * @param visible 是否顯示開啟 * @param readonly 是否只讀方式開啟 * */
public void OpenExcel(String filepath, boolean visible, boolean readonly) { try { initComponents(); // 清空原始變數 ComThread.InitSTA();//僅允許同時執行一個執行緒,其他執行緒鎖住 //ComThread.InitMTA(true);//可同時執行多個 if (xl == null) xl = new ActiveXComponent("Excel.Application"); // Excel物件 xl.setProperty("Visible", new Variant(visible));// 設定是否顯示開啟excel if (workbooks == null) workbooks = xl.getProperty("Workbooks").toDispatch(); // 工作簿物件 workbook = Dispatch.invoke( // 開啟具體工作簿 workbooks, "Open", Dispatch.Method, new Object[] { filepath, new Variant(false), new Variant(readonly) }, // 是否以只讀方式開啟 new int[1]).toDispatch(); } catch (Exception e) { e.printStackTrace(); releaseSource(); } } /** * * 工作簿另存為 * @param filePath 另存為的路徑 * */ public void SaveAs(String filePath) { Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] { filePath, new Variant(44) }, new int[1]); } /** * * 關閉excel文件 * @param f 含義不明 (關閉是否儲存?預設false) */ public void CloseExcel(boolean f, boolean quitXl) { try { Dispatch.call(workbook, "Save"); Dispatch.call(workbook, "Close", new Variant(f)); } catch (Exception e) { e.printStackTrace(); } finally { if (quitXl) { releaseSource(); } } } /** * * 釋放資源 * */ public void releaseSource() { if (xl != null) { xl.invoke("Quit", new Variant[] {}); xl = null; } workbooks = null; ComThread.Release(); System.gc(); } /** * * 新增新的工作表(sheet),(新增後為預設為當前啟用的工作表) * */ public Dispatch addSheet() { return Dispatch.get(Dispatch.get(workbook, "sheets").toDispatch(), "add").toDispatch(); } /** * * 修改當前工作表的名字 * @param newName * */ public void modifyCurrentSheetName(String newName) { Dispatch.put(getCurrentSheet(), "name", newName); } /** * * 得到當前工作表的名字 * @return * */ public String getCurrentSheetName() { return Dispatch.get(getCurrentSheet(), "name").toString(); } /** * * 得到工作薄的名字 * @return * */ public String getWorkbookName() { if (workbook == null) return null; return Dispatch.get(workbook, "name").toString(); } /** * * 得到sheets的集合物件 * @return * */ public Dispatch getSheets() { if (sheets == null) sheets = Dispatch.get(workbook, "sheets").toDispatch(); return sheets; } /** * * 得到當前sheet * @return * */ public Dispatch getCurrentSheet() { currentSheet = Dispatch.get(workbook, "ActiveSheet").toDispatch(); return currentSheet; } /** * * 通過工作表名字得到工作表 * @param name sheetName * @return * */ public Dispatch getSheetByName(String name) { return Dispatch.invoke(getSheets(), "Item", Dispatch.Get, new Object[] { name }, new int[1]).toDispatch(); } /** * * 通過工作表索引得到工作表(第一個工作簿index為1) * @param index * @return sheet物件 * */ public Dispatch getSheetByIndex(Integer index) { return Dispatch.invoke(getSheets(), "Item", Dispatch.Get, new Object[] { index }, new int[1]).toDispatch(); } /** * * 得到sheet的總數 * * @return * */ public int getSheetCount() { int count = Dispatch.get(getSheets(), "count").toInt(); return count; } /** * * 呼叫excel巨集 * @param macroName 巨集名 * */ public void callMacro(String macroName) { Dispatch.call(xl, "Run", new Variant(macroName)); } /** * * 呼叫excel巨集 * @param macroName 巨集名 * @param param 傳遞引數 */ public void callMacro(String macroName,Object param) { Dispatch.call(xl, "Run", new Variant(macroName),new Variant(param)); } /** * * 單元格寫入值 * @param sheet 被操作的sheet * @param position 單元格位置,如:C1 * @param type 值的屬性 如:value * @param value * */ public void setValue(Dispatch sheet, String position, String type, Object value) { Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { position }, new int[1]) .toDispatch(); Dispatch.put(cell, type, value); } /** * * 單元格讀取值 * @param position 單元格位置,如: C1 * @param sheet * @return * */ public Variant getValue(String position, Dispatch sheet) { Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { position }, new int[1]) .toDispatch(); Variant value = Dispatch.get(cell, "Value"); return value; } private void initComponents() { workbook = null; currentSheet = null; sheets = null; } }

使用

JacobExcelTool tool = new JacobExcelTool();
//開啟
tool.OpenExcel("test.xlsm",true,false);
//呼叫Excel巨集
tool.callMacro("closeOption");
//關閉並儲存,釋放物件
tool.CloseExcel(true, true);