我已经使用 Java 和 SWT 成功地编写了 Microsoft Word 的自动化程序。现在我需要为 Open Office / Libre Office 作家做同样的事情。
在 MS Word 的情况下,我使用了 Word.Application,如下所示:
...
wordSite = new OleClientSite(frame, SWT.NONE, "Word.Application");
wordAutomation = new OleAutomation(wordSite);
...
wordautomation.getIDsOfNames ....
我相信这应该以与 Open Office 类似的方式完成。我应该使用哪个对象?如何?谢谢
EDIT1: 我试图避免创建冗长的问题,但由于我的问题被否决了,我将尝试更详细地解释它:
- 我的长期需求是通过 Java 小程序打开和自动化打开的办公文档
- 我尝试了 UNO 和 Office bean,但在我的情况下两者似乎都没有用(路径和环境设置依赖项),当您从 Web 浏览器启动某些内容时不接受这些
- 由于我以前使用 Microsoft Word 的经验,我要求提供对象。当我使用 Word.Document 时,我可以打开文档,但自动化不起作用。当我通过 Word.Application 打开文档时,自动化确实起作用了,但它的行为不像我喜欢并想使用的 ole 对象。
目前我的代码(如下)不允许我使用自动化功能,它像 Word.Document 一样工作。
OODocument(String systemName, String fileName) {
this.fileName = fileName;
display = new Display();
shell = new Shell(display);
shell.setSize(1000, 700);
shell.setLayout(new FillLayout());
oleFrame = new OleFrame(shell, SWT.NONE);
oleClientSite = new OleClientSite(oleFrame, SWT.NONE, new File(fileName));
oleClientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
addMenu(oleFrame, systemName);
shell.open();
OleAutomation oleDocument = new OleAutomation(oleClientSite);
infoLabel.setVisible(false);
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
oleDocument.dispose();
display.dispose();
}
这就是我问这个问题的方式:“我应该使用哪些对象”。我想找到类似于 Word.Application 的对象。应该是 com.sun.star.frame.Desktop 吗?如果是这样,我还没有成功使用它。
我认为过去可以通过 OLe 编程 OOO 访问,并可以分享一些想法或代码。
谢谢你。
编辑 2 下面是来自 OpenOffice SDK 的代码,用 vbs 编写。如果有人可以帮助我将其转换为 JAVA 和 SWT,那将是解决方案:
'The service manager is always the starting point
'If there is no office running then an office is started up
Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
'Create the CoreReflection service that is later used to create structs
Set objCoreReflection= objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
'Create the Desktop
Set objDesktop= objServiceManager.createInstance("com.sun.star.frame.Desktop")
'Open a new empty writer document
Dim args()
Set objDocument= objDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)
'Create a text object
Set objText= objDocument.getText
'Create a cursor object
Set objCursor= objText.createTextCursor
'Inserting some Text
objText.insertString objCursor, "The first line in the newly created text document." & vbLf, false
'Inserting a second line
objText.insertString objCursor, "Now we're in the second line", false
'Create instance of a text table with 4 columns and 4 rows
Set objTable= objDocument.createInstance( "com.sun.star.text.TextTable")
objTable.initialize 4, 4
'Insert the table
objText.insertTextContent objCursor, objTable, false
'Get first row
Set objRows= objTable.getRows
Set objRow= objRows.getByIndex( 0)
'Set the table background color
objTable.setPropertyValue "BackTransparent", false
objTable.setPropertyValue "BackColor", 13421823
'Set a different background color for the first row
objRow.setPropertyValue "BackTransparent", false
objRow.setPropertyValue "BackColor", 6710932
'Fill the first table row
insertIntoCell "A1","FirstColumn", objTable
insertIntoCell "B1","SecondColumn", objTable
insertIntoCell "C1","ThirdColumn", objTable
insertIntoCell "D1","SUM", objTable
objTable.getCellByName("A2").setValue 22.5
objTable.getCellByName("B2").setValue 5615.3
objTable.getCellByName("C2").setValue -2315.7
objTable.getCellByName("D2").setFormula"sum <A2:C2>"
objTable.getCellByName("A3").setValue 21.5
objTable.getCellByName("B3").setValue 615.3
objTable.getCellByName("C3").setValue -315.7
objTable.getCellByName("D3").setFormula "sum <A3:C3>"
objTable.getCellByName("A4").setValue 121.5
objTable.getCellByName("B4").setValue -615.3
objTable.getCellByName("C4").setValue 415.7
objTable.getCellByName("D4").setFormula "sum <A4:C4>"
'Change the CharColor and add a Shadow
objCursor.setPropertyValue "CharColor", 255
objCursor.setPropertyValue "CharShadowed", true
'Create a paragraph break
'The second argument is a com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK constant
objText.insertControlCharacter objCursor, 0 , false
'Inserting colored Text.
objText.insertString objCursor, " This is a colored Text - blue with shadow" & vbLf, false
'Create a paragraph break ( ControlCharacter::PARAGRAPH_BREAK).
objText.insertControlCharacter objCursor, 0, false
'Create a TextFrame.
Set objTextFrame= objDocument.createInstance("com.sun.star.text.TextFrame")
'Create a Size struct.
Set objSize= createStruct("com.sun.star.awt.Size")
objSize.Width= 15000
objSize.Height= 400
objTextFrame.setSize( objSize)
' TextContentAnchorType.AS_CHARACTER = 1
objTextFrame.setPropertyValue "AnchorType", 1
'insert the frame
objText.insertTextContent objCursor, objTextFrame, false
'Get the text object of the frame
Set objFrameText= objTextFrame.getText
'Create a cursor object
Set objFrameTextCursor= objFrameText.createTextCursor
'Inserting some Text
objFrameText.insertString objFrameTextCursor, "The first line in the newly created text frame.", _
false
objFrameText.insertString objFrameTextCursor, _
vbLf & "With this second line the height of the frame raises.", false
'Create a paragraph break
'The second argument is a com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK constant
objFrameText.insertControlCharacter objCursor, 0 , false
'Change the CharColor and add a Shadow
objCursor.setPropertyValue "CharColor", 65536
objCursor.setPropertyValue "CharShadowed", false
'Insert another string
objText.insertString objCursor, " That's all for now !!", false
On Error Resume Next
If Err Then
MsgBox "An error occurred"
End If
Sub insertIntoCell( strCellName, strText, objTable)
Set objCellText= objTable.getCellByName( strCellName)
Set objCellCursor= objCellText.createTextCursor
objCellCursor.setPropertyValue "CharColor",16777215
objCellText.insertString objCellCursor, strText, false
End Sub
Function createStruct( strTypeName)
Set classSize= objCoreReflection.forName( strTypeName)
Dim aStruct
classSize.createObject aStruct
Set createStruct= aStruct
End Function
上面的代码,当保存到 .vbs 文件并以以下方式启动时: cscript WriterDemo.vbs 启动 Open Office 编写器并将一些数据写入文档。我无法让它在 Java 中工作。
如果我尝试这样开始:
final String PROG_ID = "com.sun.star.ServiceManager";
display = new Display();
shell = new Shell(display);
frame = new OleFrame(shell, SWT.NONE);
writterSite = new OleControlSite(frame, SWT.NONE, PROG_ID);
writterAutomation = new OleAutomation(writterSite);
然后我得到了例外
Caught: org.eclipse.swt.SWTException
Failed to create Ole Client. result = -2147221164
org.eclipse.swt.SWTException: Failed to create Ole Client. result = -2147221164
at org.eclipse.swt.ole.win32.OLE.error(Unknown Source)
at org.eclipse.swt.ole.win32.OleControlSite.<init>(Unknown Source)
at office.WritterAutomation.<init>(WritterAutomation.java:39)
at office.WritterAutomation.main(WritterAutomation.java:454)
我想我正在尝试使用错误的对象或不正确地使用它们。谢谢你的帮助。