日期:2014-05-17 浏览次数:20698 次
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import ooo.connector.BootstrapSocketConnector; import com.sun.star.awt.Size; import com.sun.star.beans.PropertyValue; import com.sun.star.beans.XPropertySet; import com.sun.star.container.XNameAccess; import com.sun.star.document.XDocumentInsertable; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XStorable; import com.sun.star.graphic.XGraphicProvider; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.text.XSentenceCursor; import com.sun.star.text.XText; import com.sun.star.text.XTextCursor; import com.sun.star.text.XTextDocument; import com.sun.star.text.XTextGraphicObjectsSupplier; import com.sun.star.uno.Exception; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; public class Export{ private XComponentContext mxRemoteContext; private XMultiComponentFactory mxRemoteServiceManager; private XTextCursor mxDocCursor; private XText mxDocText; private XTextDocument mxDoc; private XSentenceCursor xSentenceCursor; private XPropertySet propertySet; private Object desktop; private XComponent xEmptyWriterComponent; /** * get the remote service manager * * @return * @throws java.lang.Exception */ private XMultiComponentFactory getRemoteServiceManager() throws java.lang.Exception { if (mxRemoteContext == null && mxRemoteServiceManager == null) { // get the remote office context String oooExeFolder = "D:/Program Files/OpenOffice.org 3/program"; mxRemoteContext = BootstrapSocketConnector.bootstrap(oooExeFolder); System.out.println("Connected to a running office ..."); mxRemoteServiceManager = mxRemoteContext.getServiceManager(); } return mxRemoteServiceManager; } /** * get the interfaces to control the UNO * * @param docType * @return * @throws java.lang.Exception */ private XComponent newDocComponent(String docType) throws java.lang.Exception { String loadUrl = "private:factory/" + docType; mxRemoteServiceManager = this.getRemoteServiceManager(); // get the Desktop service desktop = mxRemoteServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop", mxRemoteContext); // retrieve the current component and access the controller XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime .queryInterface(XComponentLoader.class, desktop); PropertyValue[] propertyValue = new PropertyValue[1]; propertyValue[0] = new com.sun.star.beans.PropertyValue(); propertyValue[0].Name = "Hidden";// set the OpenOffice not open propertyValue[0].Value = Boolean.TRUE; return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, propertyValue); } /** * editing the export paper * @throws java.lang.Exception */ public void editing() throws java.lang.Exception{ xEmptyWriterComponent = newDocComponent("swriter"); mxDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xEmptyWriterComponent); mxDocText = mxDoc.getText(); // the controller gives us the TextViewCursor mxDocCursor = mxDocText.createTextCursor(); xSentenceCursor = (XSentenceCursor) UnoRuntime.queryInterface( XSentenceCursor.class, mxDocCursor); // query its XPropertySet interface, we want to set character and // paragraph // properties propertySet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, mxDocCu