简体   繁体   English

Java和VBScript填充并运行宏引发错误

[英]Java and VBScript to populate and run macro throwing error

I have an Excel file which has a macro and I would like to automate the process. 我有一个包含宏的Excel文件,我想自动执行该过程。 I have Java code which fills the Excel columns and I have written the VBScript to run the macro in the Excel. 我有填充Excel列的Java代码,并且编写了VBScript以在Excel中运行宏。

My Java code is (I pass the Excel fileName which has the macro) 我的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) {
            }
        }

The Java code runs on Apache Poi to fill in the columns and moves the Excel file to a particular directory and then I have the below VBScript to run the macro: 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

I call the above VBscript from the java as below, 我从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);
        }

But the problem is, once the Java populates the Excel columns and save the file, the file becomes protected and hence the VBScript is throwing an error stating it can't open/run the macro in protected Excel. 但是问题是,一旦Java填充了Excel列并保存了文件,文件便受到保护,因此VBScript抛出错误,表明它无法在受保护的Excel中打开/运行宏。

Any advice? 有什么建议吗?

感谢@AxelRitcher,解决方案是“请阅读有关Microsoft Office中的错误消息:“ Office检测到此文件有问题”。似乎F:\\ testmacro \\ testmacro位置不是具有以下内容的Excel文件的受信任位置里面的宏。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM