Java和VBScript填充并运行宏抛出错误

时间:2017-11-21 05:01:12

标签: java excel-vba vbscript apache-poi vba

我有一个Excel文件,它有一个宏,我想自动化这个过程。我有Java代码填充Excel列,我编写了VBScript以在Excel中运行宏。

我的Java代码是(我传递具有宏的Excel fileName)

public void excelupdate(String fileName) {
    FileInputStream file = null;
    FileOutputStream out = null;
    try {
        file = new FileInputStream(new File(fileName));
        HSSFWorkbook yourworkbook = new HSSFWorkbook(file);
        HSSFSheet sheet1 = null;
        for (int i = 0; i < yourworkbook.getNumberOfSheets(); i++) {
            if (yourworkbook.getSheetName(i).contains("Sheet-Macro")) {
                sheet1 = yourworkbook.getSheetAt(i);
            }
        }

        Cell cell = null;
        int rowValue = 10;
        for (int i = 0; i < list.size() - 1; i++) {
            cell = sheet1.getRow(rowValue).getCell(2);
            cell.setCellValue(list.get(i));
            rowValue++;
        }

        Cell cell1 = null;
        int rowValue1 = 10;
        for (int j = 0; j < Input1list.size() - 1; j++) {
            cell1 = sheet1.getRow(rowValue1).getCell(3);
            cell1.setCellValue(Input1list.get(j));
            rowValue1++;
        }

        Cell cell2 = null;
        int rowValue2 = 22;
        for (int k = 0; k < Input2list.size() - 1; k++) {
            cell2 = sheet1.getRow(rowValue2).getCell(4);
            cell2.setCellValue(Input2list.get(k));
            rowValue2++;
        }

        out = new FileOutputStream(("C:\\Users\\Desktop\\EXCEL.xls"));
        yourworkbook.write(out);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (Exception e) {
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
            }
        }

Java代码在Apache Poi上运行以填充列并将Excel文件移动到特定目录,然后我有以下VBScript来运行宏:

Dim objXL
Set objXL = CreateObject("Excel.Application")
Set objWorkbook = objXL.Workbooks.Open("F:\testmacro\testmacro\EXCEL.xls")
objWorkbook.Sheets("AD stages").Cells(6, 4) = "F:\set1\set.txt"
objXL.Application.DisplayAlerts = False
objXL.ActiveWorkbook.Save
objXL.Application.Run "macro_cal"
objXL.ActiveWorkbook.Save
objXL.ActiveWorkbook.Close
objXL.Application.DisplayAlerts = True
objXL.Application.Quit
WScript.Echo "ExCEL file updated successfully"
WScript.Quit 
Set objXL = Nothing

我从java调用上面的VBscript,如下所示

File file = new File(excelFilename);
            file.setExecutable(true);
            file.setReadable(true);
            file.setWritable(true);

        Runtime runtime = Runtime.getRuntime();
        try {
            String sample="cmd /c start "+vbScript+" "+"\"" +excelFilename + "\"" + " "+"\"" +outFile + "\"";
            System.out.println(sample);
            Process process1 = runtime.exec(sample);


        } catch (IOException e) {
            logger.error(e);
        }

但问题是,一旦Java填充Excel列并保存文件,该文件就会受到保护,因此VBScript会抛出错误,指出它无法在受保护的Excel中打开/运行宏。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

感谢@AxelRitcher,解决方案是“请阅读Microsoft Office中的错误消息:”Office已检测到此文件存在问题“。所以好像位置F:\ testmacro \ testmacro不是受信任的位置其中包含宏的Excel文件。“