2010-09-03 90 views
5

我試圖保存已自動修改的工作簿。下面是一個例子:如何在不提示的情況下使用com4j API保存excel工作簿

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 

我嘗試以下的事情和沒有一個是成功的:

  1. 將readOnly參數設置爲false
  2. 設置ignoreReadOnly參數設置爲true
  3. 既做1和2
  4. 傳遞saveBeforeExit對象的保存方法

有沒有辦法保存工作簿不提示?請注意,上面的代碼不會打開文件,並且可以毫無錯誤地計算公式。

感謝

+0

你不希望使用任何POI原因:http://poi.apache.org/ – tronda 2010-09-08 08:28:28

+0

@tronda:我需要計算一個包含多個公式的工作簿。 POI只能在單元格內評估公式。儘管使用com4j API,我接近了解決方案。很快就會發布。 – kninja 2010-09-08 18:02:37

回答

5

好吧,這裏的解決方案:

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(); 
} 

}

相關問題