【问题标题】:Executing VB script from Spring Boot Application从 Spring Boot 应用程序执行 VB 脚本
【发布时间】:2021-01-01 18:08:06
【问题描述】:

我想从 Spring Boot 应用程序中执行一个 VB 脚本,脚本如下:

' Creating a file as test acces to macro 
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CreateTextFile("C:\excel\FLAG_TEST")
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\excel\excelFile.xls")
objExcel.Application.EnableEvents = False
objExcel.Application.DisplayAlerts = False
objExcel.Application.Run "excelFile.xls!executeMacroFunction"
objExcel.Application.Quit

我尝试通过以下方式从 Spring Boot 服务执行脚本:

Runtime.getRuntime().exec(new String[]{"C:\\Windows\\System32\\wscript.exe", "C:/vb/script.vbs"}); 

上述行代码不适用于部署在 Tomcat 9 上的应用程序(在 Eclipse 上作为 Spring Boot App 运行时可以正常工作)

然后我尝试通过从应用程序启动的批处理文件执行脚本 在批处理文件上,我尝试通过

执行VB脚本
rem First way
Set wscript = CreateObject("WScript.Shell")
wscript.Run "C:/vb/script.vbs"
--------------------
rem Second way (System 64bit)
C:\Windows\System32\wscript.exe C:/vb/script.vbs
--------------------
rem Third way (System 64bit)
wscript C:/vb/script.vbs

还可以通过不同的方式从 Java 运行批处理文件:

// First way    
ProcessBuilder processBuilder = new ProcessBuilder("C:/batch/executor.bat");
final Process process = processBuilder.start();

// Second way
Process process = Runtime.getRuntime().exec("cmd /c start \"\" C:/batch/executor.bat");

// Third way
Process process = Runtime.getRuntime().exec(
        "cmd /c start \"\" C:/batch/executor.bat",
        null,
        new File("C:\\Windows\\SysWOW64"));

所有这些方法都不起作用,脚本没有问题,因为当我手动运行批处理文件(通过双击)时它们可以工作

在 Spring Boot 执行宏时,会创建测试文件 (FLAG_TEST),然后在该行阻止执行

Set objExcel = CreateObject("Excel.Application")

我错过了什么吗?...提前非常感谢!

【问题讨论】:

    标签: excel spring-boot vbscript tomcat9


    【解决方案1】:

    还尝试使用 Jacob DLL 在 excel 文件中运行宏,但它在 Tomcat 服务器中也不适用于我 最后我决定放弃任何外部工具,在 Spring Boot 中做这件事,通过 Excel Java API 解析 Excel 文件并将宏程序迁移到 Java:JExcel

    <dependency>
          <groupId>net.sourceforge.jexcelapi</groupId>
          <artifactId>jxl</artifactId>
          <version>2.6.12</version>
    </dependency>
    

    与其他 Excel 解析 API 不同,Jexcel 按原样获取单元格文本:字符串、数字、布尔值、公式(其显示结果),无需处理内容类型的不同情况

    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    import jxl.WorkbookSettings;
    
    
    // Set Encoding 1252 that supports many European languages (special characters included)
    WorkbookSettings workbookSettings = new WorkbookSettings(); 
    workbookSettings.setEncoding("Cp1252");
    
    Workbook workbook = Workbook.getWorkbook(new File(EXCEL_FILE_LOCATION), workbookSettings);
    
    Sheet sheet = workbook.getSheet(0);
    Cell cellDemo = sheet.getCell(0, 0); // Or sheet.getCell("A1");
    // Get the displayed cell centent as a text
    String cellContent = cellDemo.getContents();
    System.out.print("Cell A1 centent:" + cellContent);
    

    Java解析使得数据校验的异常处理更简单更精确,没有抽象的HMI错误信息(处理Excel文件时发生错误...)

    事实证明,Excel Java 读取 API 实现所用的时间比几次尝试在外部运行宏的时间要少:D

    【讨论】:

      最近更新 更多