我查看了包含的最新版本 Java Com Bridge (jacob-1.16) 的 Excel 示例,有点失望,因为它创建并处理了一个全新的 Excel 文件。
我想做的是从 Java 访问现有的 Excel 文件,在本例中名为“JACOBWithExcel.xls”,但任何 Excel 文件都应该没问题。
Excel 的 SourceForge 示例在我的机器上运行良好,当我修改它以访问现有的 Excel 文件时,我遇到了以下问题:
1.,我无法在工作簿中获取现有工作表,即使我正在尝试以与获取工作簿相同的方式:
Dispatch sheet = Dispatch.get(workbook, "Worksheets").toDispatch();
Dispatch.call(sheet, "Select", new Object[]{"Sheet2"}).toDispatch();
此代码将生成以下异常:com.jacob.com.ComFailException: Can't map name to dispid: Worksheets
2.; 无法保存工作簿:
// Save the open workbook as "C:\jacob-1.16-M1\Test1.xls" file:
Dispatch.call(workbook, "SaveAs", new Variant("C:\\jacob-1.16-M1\\Test1.xls"),new Variant("1"));
此代码将生成以下异常:com.jacob.com.ComFailException: Can't map name to dispid: SaveAs
3.; 我不知道如何开始使用以下简单但非常常见的 Excel 操作,就 Java COM 桥的 Java 语法而言:
(包括这里我试图用 Java 实现的 Excel VBA 代码)
选择单个单元格: Range("A4").Select
将选定范围复制到剪贴板:
Selection.Copy
选择要复制到的多单元格范围:
Range("D9:D17").Select
将剪贴板内容粘贴到选择:
ActiveSheet.Paste
重命名工作表:
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "MySheet2"
格式化单元格,例如文本:
Selection.NumberFormat = "@"
删除行:
Rows(intI).Select
Selection.Delete Shift:=xlUp
并且可能...
对选择进行排序:
Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
查找工作表中的最后一个单元格:
ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
谢谢您的帮助。
PS:
应用程序的完整代码:
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class TestJACOBWithExcel {
public static void main(String[] args) {
String strInputDoc = "C:\\jacob-1.16-M1\\JACOBWithExcel.xls"; // file to be opened.
ComThread.InitSTA();
ActiveXComponent xl = new ActiveXComponent("Excel.Application"); // Instance of application object created.
try {
// Get Excel application object properties in 2 ways:
System.out.println("version=" + xl.getProperty("Version"));
System.out.println("version=" + Dispatch.get(xl, "Version"));
// Make Excel instance visible:
Dispatch.put(xl, "Visible", new Variant(true));
// Open XLS file, get the workbooks object required for access:
Dispatch workbook = xl.getProperty("Workbooks").toDispatch();
Dispatch.call(workbook, "Open", new Variant(strInputDoc),new Variant("1"));
Dispatch sheet = Dispatch.get(workbook, "Worksheets").toDispatch();
Dispatch.call(sheet, "Select", new Object[]{"Sheet2"}).toDispatch();
// put in a value in cell A22 and place a a formula in cell A23:
Dispatch a22 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A22" }, new int[1]).toDispatch();
Dispatch a23 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A23" }, new int[1]).toDispatch();
Dispatch.put(a22, "Value", "123.456");
Dispatch.put(a23, "Formula", "=A22*2");
// Get values from cells A1 and A2
System.out.println("a22 from excel:" + Dispatch.get(a22, "Value"));
System.out.println("a23 from excel:" + Dispatch.get(a23, "Value"));
// Save the open workbook as "C:\jacob-1.16-M1\Test1.xls" file:
Dispatch.call(workbook, "SaveAs", new Variant("C:\\jacob-1.16-M1\\Test1.xls"),new Variant("1"));
// Close the Excel workbook without saving:
Variant saveYesNo = new Variant(false);
Dispatch.call(workbook, "Close", saveYesNo);
} catch (Exception e) {
e.printStackTrace();
} finally {
// Quit Excel:
// xl.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}
}