[英]Java app that runs an VBA macro works only on my computer
我創建了一個執行以下操作的Java應用程序:
1.基於一些輸入,它會生成一些文本-在其他計算機上可以使用
2.根據輸入修改Excel文件-在其他計算機上工作
3.它從先前修改的excel文件運行一個宏-僅在我的計算機上工作
這是excel文件並運行宏的部分代碼:
try {
File myFile = new File(atmScriptGeneratorPathTxt.getText() + "\\ATM Script Generator.xlsm");
FileInputStream fis = new FileInputStream(myFile);
XSSFWorkbook myWorkBook = new XSSFWorkbook(fis);
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Cell myCell = null;
myCell= mySheet.getRow(2).getCell(2);
myCell.setCellValue(newRNCCDRFolderTxt.getText());
fis.close();
FileOutputStream output_file =new FileOutputStream(new File(atmScriptGeneratorPathTxt.getText() + "\\ATM Script Generator.xlsm"));
myWorkBook.write(output_file);
output_file.close();
myWorkBook.close();
String macroName = "'ATM Script Generator.xlsm'!ATM_Script_Generator.ATM_Script_Generator";
ComThread.InitSTA();
ActiveXComponent excel = new ActiveXComponent("Excel.Application");
try {
//This will open the excel if the property is set to true
Dispatch workbooks = excel.getProperty("Workbooks") .toDispatch();
Dispatch workBook = Dispatch.call(workbooks, "Open", myFile.getAbsolutePath()).toDispatch();
// Calls the macro
final Variant result = Dispatch.call(excel, "Run", new Variant(macroName));
System.out.println(result);
// Saves and closes
Dispatch.call(workBook, "Save");
com.jacob.com.Variant f = new com.jacob.com.Variant(true);
Dispatch.call(workBook, "Close", f);
} catch (Exception error) {
error.printStackTrace();
} finally {
excel.invoke("Quit", new Variant[0]);
ComThread.Release();
}
}
catch(Exception err) {
System.out.println(err);
}
}
這些是我正在為此應用程序使用的庫:
導入org.apache.poi.ss.usermodel.Cell;
導入org.apache.poi.xssf.usermodel.XSSFSheet;
導入org.apache.poi.xssf.usermodel.XSSFWorkbook;
導入com.jacob.activeX.ActiveXComponent;
導入com.jacob.com.ComThread;
導入com.jacob.com.Dispatch;
導入com.jacob.com.Variant;
有誰知道為什么這個程序只能在我的計算機上工作? 如果有人需要更多信息以找出問題所在,請告訴我。
Jacob Libray包含兩個.dll文件:
jacob-1.18-x64.dll
jacob-1.18-x86.dll
我將這兩個.dll文件復制到:C:\\ Program Files \\ Java \\ jre1.8.0_111 \\ bin,此后它可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.