Java 如何使用com4j API保存excel工作簿而不出现提示

Java 如何使用com4j API保存excel工作簿而不出现提示,java,excel,autosave,com4j,Java,Excel,Autosave,Com4j,我正在尝试保存已自动修改的工作簿。以下是一个例子: import excel.*; import com4j.Variant; import static com4j.Variant.MISSING; public class ExcelDemo { public static void main(String[] args) { _Application app = excel.ClassFactory.createApplication(); app.visib

我正在尝试保存已自动修改的工作簿。以下是一个例子:

import excel.*;
import com4j.Variant;
import static com4j.Variant.MISSING; 


public class ExcelDemo {
    public static void main(String[] args) {
    _Application app = excel.ClassFactory.createApplication();
    app.visible(0,false);

    //Variant readOnly = new Variant(Variant.Type.VT_BOOL);
    //readOnly.set(0);
    //Variant ignoreReadOnly = new Variant(Variant.Type.VT_BOOL);
    //ignoreReadOnly.set(1);
    //Variant saveBeforeExit = new Variant(Variant.Type.VT_BOOL);
    //saveBeforeExit.set(1);

    app.workbooks().open(
        "C:/dev/test.xlsx",
        MISSING, 
        MISSING, 
        MISSING, 
        MISSING,    
        MISSING,    
        MISSING,    
        MISSING, 
        MISSING,    
        MISSING,    
        MISSING,    
        MISSING, 
        MISSING,    
        MISSING,    
        MISSING,    
        0);
    app.calculate(0);
    app.save(MISSING,0);
    app.workbooks().close(0);
    //app.workbooks().close(saveBeforeExit,MISSING,MISSING);
}
}

上述代码是从ant文件运行的,并产生以下错误:

run:
 [java] Exception in thread "main" com4j.ComException: 800a03ec (Unknown error) : The file could not be accessed. Try one of the following:
 [java]
 [java] ò Make sure the specified folder exists.
 [java] ò Make sure the folder that contains the file is not read-only.
 [java] ò Make sure the file name does not contain any of the following characters:  <  >  ?  [  ]  :  | or  *
 [java] ò Make sure the file/path name doesn't contain more than 218 characters. : .\invoke.cpp:460
 [java]     at com4j.Wrapper.invoke(Wrapper.java:122)
 [java]     at $Proxy5.save(Unknown Source)
 [java]     at ExcelDemo.main(ExcelDemo.java:36)
 [java] Caused by: com4j.ComException: 800a03ec (Unknown error) : The file could not be accessed. Try one of the following:
 [java]
 [java] ò Make sure the specified folder exists.
 [java] ò Make sure the folder that contains the file is not read-only.
 [java] ò Make sure the file name does not contain any of the following characters:  <  >  ?  [  ]  :  | or  *
 [java] ò Make sure the file/path name doesn't contain more than 218 characters. : .\invoke.cpp:460
 [java]     at com4j.Native.invoke(Native Method)
 [java]     at com4j.StandardComMethod.invoke(StandardComMethod.java:95)
 [java]     at com4j.Wrapper$InvocationThunk.call(Wrapper.java:258)
 [java]     at com4j.Task.invoke(Task.java:44)
 [java]     at com4j.ComThread.run0(ComThread.java:149)
 [java]     at com4j.ComThread.run(ComThread.java:125)
 [java] Java Result: 1
运行:
线程“main”com4j.ComException:800a03ec中出现[java]异常(未知错误):无法访问该文件。请尝试以下操作之一:
[爪哇]
[java]确保指定的文件夹存在。
[java]确保包含该文件的文件夹不是只读的。
[java]确保文件名不包含以下任何字符:<>?[]:|或*
[java]确保文件/路径名不包含超过218个字符。:\调用.cpp:460
[java]位于com4j.Wrapper.invoke(Wrapper.java:122)
[java]位于$Proxy5.save(未知源)
[java]位于ExcelDemo.main(ExcelDemo.java:36)
[java]原因:com4j.ComException:800a03ec(未知错误):无法访问该文件。请尝试以下操作之一:
[爪哇]
[java]确保指定的文件夹存在。
[java]确保包含该文件的文件夹不是只读的。
[java]确保文件名不包含以下任何字符:<>?[]:|或*
[java]确保文件/路径名不包含超过218个字符。:\调用.cpp:460
[java]位于com4j.Native.invoke(本机方法)
[java]位于com4j.StandardComMethod.invoke(StandardComMethod.java:95)
[java]位于com4j.Wrapper$InvocationThunk.call(Wrapper.java:258)
[java]位于com4j.Task.invoke(Task.java:44)
[java]位于com4j.ComThread.run0(ComThread.java:149)
[java]位于com4j.ComThread.run(ComThread.java:125)
[java]java结果:1
我尝试了以下几件事,但没有一件成功:

  • 将readOnly参数设置为false
  • 将ignoreReadOnly参数设置为true
  • 同时做1和2
  • 将saveBeforeExit对象传递给save方法
  • 有没有一种方法可以在不提示的情况下保存工作簿?请注意,上面的代码确实打开了该文件,并且计算公式时没有任何错误


    谢谢

    好的,下面是解决方案:

    import excel.*;
    import com4j.Variant;
    import static com4j.Variant.MISSING; 
    
    
    public class ExcelDemo {
        public static void main(String[] args) {
        _Application app = excel.ClassFactory.createApplication();
        app.visible(0,false);
    
     Variant saveBeforeExit = new Variant(Variant.Type.VT_BOOL);
     saveBeforeExit.set(1);
    
     _Workbook wb = app.workbooks().open(
        "C:/dev/test.xlsx",
        MISSING, //0
        MISSING, //false
        MISSING, //5
        MISSING, //"" 
        MISSING, //"" 
        MISSING, //true 
        MISSING, //true 
        MISSING, //obj 
        MISSING, //false 
        MISSING, //false 
        MISSING, //obj
        MISSING, 
        MISSING, 
        MISSING, 
     0);
     app.calculate(0);
     wb.close(saveBeforeExit, MISSING,MISSING, 0);
     app.quit();
    }
    

    }

    您不想使用POI的任何原因:@tronda:我需要计算包含多张公式的工作簿。POI只能在单元格内计算公式。尽管使用com4japi,我已经接近找到解决方案了。我很快就要发帖了。