Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save excel workbook using com4j API without being prompted

I'm trying to save a workbook that has been modified automatically. Here is an example:

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

}

The above code was run from an ant file and produced the following error:

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

I tried the following things and none were successful:

  1. setting the readOnly parameter to false
  2. setting the ignoreReadOnly parameter to true
  3. Doing both 1 and 2
  4. passing the saveBeforeExit object to the save method

Is there a way to save a workbook without being prompted? Note that the above code does open the file, and calculates formulas without any errors.

Thanks

like image 547
kninja Avatar asked Sep 03 '10 23:09

kninja


People also ask

How do you stop Excel from prompting to save?

Try these steps: Open the Excel file > File > Options > Save > Check the option "Save to Computer by Default". To Disable Autosave for a document, see this link: https://support.microsoft.com/en-us/office/what...

Why is Excel not allowing me to save?

You may have problems when you try to save a Microsoft Excel workbook if one or more of the following conditions are true: You save an Excel workbook to a network drive on which you have restricted permissions. You save an Excel workbook to a location that does not have sufficient storage space.

How do you disable or do not allow Save & Save As options in Excel?

VBA 1: disable the Save & Save As options in Excel In the opening Save As window, select a folder to save the workbook, name the workbook as you need and select Excel Macro-Enabled Workbook from the Save as type drop-down list, and finally click the Save button. 4.


1 Answers

Ok here's the solution:

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

}

like image 121
kninja Avatar answered Sep 25 '22 19:09

kninja