How to saveas Excel File using Jacob (Java)
I am trying to saveas excel file, but it gives an error: Can't map name to dispid: FileSaveAs.
private ActiveXComponent objExcel = new Ac开发者_运维百科tiveXComponent("Excel.Application");
excelObject = objExcel.getObject();
Dispatch.put(excelObject, "Visible", new Variant(false));
workbook = objExcel.getProperty("Workbooks").toDispatch();
workbook = Dispatch.call(workbook, "Open", filename).toDispatch();
Dispatch oExcel = Dispatch.call(objExcel, "Workbooks").getDispatch();
Dispatch.call(workbook, "FileSaveAs", filename, new Variant(51));
The Parameter of the variant is the taken from: http://msdn.microsoft.com/en-us/library/bb241279(v=office.12).aspx
Can anybody tell me where is the problem in code? Thanks
I'm not sure if I use the correct way to do it, but it works
public Boolean savesAS(String path, String nameFile){
ComThread.InitSTA();
ActiveXComponent excel = new ActiveXComponent("Excel.Application");
excel.setProperty("Visible", new Variant(true));
Object workbooks = excel.getProperty("Workbooks").toDispatch();
Object workbook;
try {
workbook = Dispatch.call((Dispatch)workbooks, "Open", path).toDispatch();
} catch (Exception e) {
showMessageDialog(null,"Unable to open " + path);
return false;
}
try {
//here you can modify the value of "new Variant(int)" with other number
Dispatch.call((Dispatch)workbook, "SaveAs", nameFile ,new Variant(2));
} catch (Exception e) {
showMessageDialog(null,"Unable to convert " + path);
return false;
}
Dispatch.call((Dispatch)workbook, "Close", new Variant(false));
excel.invoke("Quit", new Variant[0]);
excel.safeRelease();
ComThread.Release();
return true;
}
Yes you are right, it does work with "Saveas". Actually "FileSaveAs" works with word document, so i thought to try it with Excel and Powerpoint, but it dint worked...
To saveas excel workbook, this code works:
Dispatch.call(workbook, "SaveAs", filename, new Variant(1));
As stated by the error it is not able to find the method "FileSaveAs". I tried to look for it in Google/Documentation and could not find it (maybe it exists but I could not find it). I found SaveAs instead. So try to use that and see what happens.
精彩评论