gpt4 book ai didi

java - 如何在没有提示的情况下使用 com4j API 保存 excel 工作簿

转载 作者:搜寻专家 更新时间:2023-10-31 20:07:02 24 4
gpt4 key购买 nike

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

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 对象传递给保存方法

有没有办法在没有提示的情况下保存工作簿?请注意,上面的代码确实打开了文件,并且没有任何错误地计算了公式。

谢谢

最佳答案

好的,这是解决方案:

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

关于java - 如何在没有提示的情况下使用 com4j API 保存 excel 工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3640190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com