访问工作表、保存Excel文件、Java、Java Com桥(Jacob-1.16)

访问工作表、保存Excel文件、Java、Java Com桥(Jacob-1.16),java,jacob,Java,Jacob,我看了最新版本的JavaComBridge(jacob-1.16)附带的Excel示例,有点失望,因为它创建了一个全新的Excel文件 ActiveSheet.Cells(65536, 1).End(xlUp).Select intLastCellRow = Selection.Row 我想做的是从Java访问现有的Excel文件,在本例中名为“JACOBWithExcel.xls”,但任何Excel文件都可以 ActiveSheet.Cells(65536, 1).End(xlUp).Sel

我看了最新版本的JavaComBridge(jacob-1.16)附带的Excel示例,有点失望,因为它创建了一个全新的Excel文件

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
我想做的是从Java访问现有的Excel文件,在本例中名为“JACOBWithExcel.xls”,但任何Excel文件都可以

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
SourceForge Excel示例在我的计算机上运行良好,当我修改它以访问现有Excel文件时,遇到了以下问题:

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
1.,我无法在工作簿中获取现有工作表,即使我正在尝试 与我获取工作簿的方式相同:

Dispatch sheet = Dispatch.get(workbook, "Worksheets").toDispatch();  
Dispatch.call(sheet, "Select", new Object[]{"Sheet2"}).toDispatch();
// 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"));
ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
此代码将生成以下异常: com.jacob.com.ComFailException:无法将名称映射到dispid:工作表

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
二,。;无法保存工作簿:

Dispatch sheet = Dispatch.get(workbook, "Worksheets").toDispatch();  
Dispatch.call(sheet, "Select", new Object[]{"Sheet2"}).toDispatch();
// 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"));
ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
此代码将生成以下异常: com.jacob.com.ComFailException:无法将名称映射到dispid:SaveAs

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
三,。;就Java COM桥接器的Java语法而言,我不知道如何开始以下简单但非常常见的Excel操作:

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
(这里包括我试图用Java实现的Excel VBA代码)

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
选择单个单元格: 范围(“A4”)。选择

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
将选定范围复制到剪贴板:

Selection.Copy
ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
选择要复制到的多单元格范围:

Range("D9:D17").Select
ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
将剪贴板内容粘贴到选定内容:

ActiveSheet.Paste
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
重命名图纸:

Sheets("Sheet2").Select
Sheets("Sheet2").Name = "MySheet2"
ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
设置单元格格式,例如文本:

Selection.NumberFormat = "@"
ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
删除行:

Rows(intI).Select
Selection.Delete Shift:=xlUp
ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
而且可能

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
对所选内容进行排序:

ActiveSheet.Paste
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
查找工作表中的最后一个单元格:

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
谢谢你的帮助

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
附言:

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
应用程序的完整代码:

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
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();
}

}
}
Hy

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
实际上我现在只能回答你的第一个问题。我使用此代码,它正确地用于*.xltx模板文件:

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
File file = new File("pathToOurTemplate");
ActiveXComponent excel = null;
  ComThread.InitSTA();
  try {
    excel = new ActiveXComponent("Excel.Application");
    Dispatch excelObject = excel.getObject();
    Dispatch workbooks = excel.getProperty("Workbooks").toDispatch();
    Dispatch workbook = Dispatch.call(workbooks, "Add", file.getAbsolutePath()).toDispatch();
} catch (...) 
//...
至少与Office 2010、Java6_u23、Jacob 1.15_M4合作

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row

希望这对第一次尝试有所帮助/这比示例中显示的更符合您的需要(我想您已经看过了)。

免责声明:在这个回答中,我将引用MS Excel对象属性、方法和对象类型,并使用双引号,以避免混淆。我对这个问题的回答如下

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
了解excel API所具有的层次结构非常重要。还可以搜索在每个层次结构级别上可用的方法、属性或事件的类型。我现在要回答你的问题

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
在问题1中,您说您无法打开正确的工作表,这是因为您在错误类型的对象上获取属性“工作表”。在代码片段中

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
Dispatch sheet = Dispatch.get(workbook, "Worksheets").toDispatch();
Dispatch.call(sheet, "Select", new Object[]{"Sheet2"}).toDispatch();
您在类型为“工作簿”的对象上获得属性“工作表”,该属性不正确。 此文档显示,“工作簿”没有属性“工作表”。问题在于如何打开具体的“工作簿”对象

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
您在“工作簿”对象上调用方法“Open”,该对象具有此方法,它将为您打开MS Excel。问题是变量“Dispatch workbook”仍然是“workbook”类型的对象,而不是“workbook”,这是您错误地假定的。正确的方法是保存“Open”方法调用的返回值,它将为您提供具体的“工作簿”对象,然后使用可用于该类型对象的方法。跟在docu女士后面

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
因此,打开混凝土工作表的正确方法如下:

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
//get "Workbooks" property from "Application" object
Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
//call method "Open" with filepath param on "Workbooks" object and save "Workbook" object
Dispatch workbook = Dispatch.call(workbooks, "Open", new Variant(strInputDoc)).toDispatch();
//get "Worksheets" property from "Workbook" object
Dispatch sheets = Dispatch.get(workbook, "Worksheets").toDispatch();
//Call method "Select" on "Worksheets" object with "Sheet2" param   
Dispatch.call(sheets, "Select", new Object[]{"Sheet2"}).toDispatch();
//probably again save "Worksheet" object and continue same way
问题2中的问题与问题1中的问题相同

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
//calling method "SaveAs" on "Workbooks" object instead of "Workbook" type
Dispatch.call(workbook, "SaveAs", new Variant("C:\\jacob-1.16-M1\\Test1.xls"),new   Variant("1"));
所有其他问题都和前面两个问题的原则一样。获取正确MS对象的属性或调用方法。同样,对于任何使用java excel通信的人来说,MS docu的重要性是再怎么强调也不为过的

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
我只举一个例子,如何从一些MS对象中获取实际值。假设您希望值位于“范围”对象内,因为获取需要参数的对象属性的方式与普通值属性稍有不同。为此,您需要首先访问“工作表”对象并从中获取“范围”属性

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row
//lets say we have already some "Worksheet" object
Dispatch worksheet = ...;
//we want a property "Range" of "Worksheet" object with some macro
Variant range = Dispatch.invoke(worksheet, "Range", Dispatch.Get, new Object[]{"A1:L10"}, new int[1]);
//according to docu, you can specify format in which the value is returned
//im using 12 which is one of XML formats available
//in case no such param is provided default "textTable" format is returned
Variant value = Dispatch.invoke(range.getDispatch(), "Value", Dispatch.Get, new Object[]{12}, new int[1]);
//and get the value from it and you are done with MS Excel-Java communication
String xmlFormat = value.toString();



Worksheet.Range property docu:
msdn.microsoft.com/en-us/library/office/ff836512.aspx
Range.Value docu:
msdn.microsoft.com/en-us/library/office/ff195193.aspx
我不允许发布超过2个链接,所以如果这些链接不能正常工作,我很抱歉

ActiveSheet.Cells(65536, 1).End(xlUp).Select
intLastCellRow = Selection.Row