⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 openofficeservices.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            while ((c = fis.read()) != -1) {                baos.write(c);            }            OpenOfficeByteArrayInputStream oobais = new OpenOfficeByteArrayInputStream(baos.toByteArray());            OpenOfficeByteArrayOutputStream oobaos = OpenOfficeWorker.convertOODocByteStreamToByteStream(xmulticomponentfactory, oobais, inputMimeType, outputMimeType);            FileOutputStream fos = new FileOutputStream(stringConvertedFile);            fos.write(oobaos.toByteArray());            fos.close();            fis.close();            oobais.close();            oobaos.close();                        Map results = ServiceUtil.returnSuccess();            return results;        } catch (IOException e) {            Debug.logError(e, "Error in OpenOffice operation: ", module);            return ServiceUtil.returnError(e.toString());        } catch(Exception e) {            Debug.logError(e, "Error in OpenOffice operation: ", module);            return ServiceUtil.returnError(e.toString());        }    }    /**     * Use OpenOffice to convert documents between types     */    public static Map convertDocumentStreamToStream(DispatchContext dctx, Map context) {        XMultiComponentFactory xmulticomponentfactory = null;                String stringUrl = "file:///" + context.get("filenameFrom");        String stringConvertedFile = "file:///" + context.get("filenameTo");        String inputMimeType = (String) context.get("inputMimeType");        String outputMimeType = (String) context.get("outputMimeType");        // if these are empty don't worry, the OpenOfficeWorker down below will take care of it        String oooHost = (String) context.get("oooHost");        String oooPort = (String) context.get("oooPort");                try {                xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort);            File inputFile = new File(stringUrl);            long fileSize = inputFile.length();            FileInputStream fis = new FileInputStream(inputFile);            ByteArrayOutputStream baos = new ByteArrayOutputStream((int)fileSize);            int c;            while ((c = fis.read()) != -1) {                baos.write(c);            }            OpenOfficeByteArrayInputStream oobais = new OpenOfficeByteArrayInputStream(baos.toByteArray());            OpenOfficeByteArrayOutputStream oobaos = OpenOfficeWorker.convertOODocByteStreamToByteStream(xmulticomponentfactory, oobais, inputMimeType, outputMimeType);            FileOutputStream fos = new FileOutputStream(stringConvertedFile);            fos.write(oobaos.toByteArray());            fos.close();            fis.close();            oobais.close();            oobaos.close();                        Map results = ServiceUtil.returnSuccess();            return results;        } catch (IOException e) {            Debug.logError(e, "Error in OpenOffice operation: ", module);            return ServiceUtil.returnError(e.toString());        } catch(Exception e) {            Debug.logError(e, "Error in OpenOffice operation: ", module);            return ServiceUtil.returnError(e.toString());        }    }    /**     * Use OpenOffice to compare documents     */    public static Map compareDocuments(DispatchContext dctx, Map context) {        XMultiComponentFactory xmulticomponentfactory = null;                String stringUrl = "file:///" + context.get("filenameFrom");        String stringOriginalFile = "file:///" + context.get("filenameOriginal");        String stringOutFile = "file:///" + context.get("filenameOut");        // if these are empty don't worry, the OpenOfficeWorker down below will take care of it        String oooHost = (String)context.get("oooHost");        String oooPort = (String)context.get("oooPort");                try {                xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort);        } catch (IOException e) {            Debug.logError(e, "Error in OpenOffice operation: ", module);            return ServiceUtil.returnError(e.toString());        } catch(Exception e) {            Debug.logError(e, "Error in OpenOffice operation: ", module);            return ServiceUtil.returnError(e.toString());        }        //System.out.println("xmulticomponentfactory: " + xmulticomponentfactory);               // Converting the document to the favoured type        try {            // Composing the URL                                    // Query for the XPropertySet interface.            XPropertySet xpropertysetMultiComponentFactory = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xmulticomponentfactory);                        // Get the default context from the office server.            Object objectDefaultContext = xpropertysetMultiComponentFactory.getPropertyValue("DefaultContext");                        // Query for the interface XComponentContext.            XComponentContext xcomponentcontext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, objectDefaultContext);                        /* A desktop environment contains tasks with one or more               frames in which components can be loaded. Desktop is the               environment for components which can instanciate within               frames. */                        Object desktopObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext);            XDesktop desktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktopObj);            XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopObj);                                   // Preparing properties for loading the document            PropertyValue propertyvalue[] = new PropertyValue[ 1 ];            // Setting the flag for hidding the open document            propertyvalue[ 0 ] = new PropertyValue();            propertyvalue[ 0 ].Name = "Hidden";            propertyvalue[ 0 ].Value = new Boolean(true);            //TODO: Hardcoding opening word documents -- this will need to change.            //propertyvalue[ 1 ] = new PropertyValue();            //propertyvalue[ 1 ].Name = "FilterName";            //propertyvalue[ 1 ].Value = "HTML (StarWriter)";                        // Loading the wanted document            Object objectDocumentToStore = xcomponentloader.loadComponentFromURL(stringUrl, "_blank", 0, propertyvalue);                        // Getting an object that will offer a simple way to store a document to a URL.            XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore);                        // Preparing properties for comparing the document            propertyvalue = new PropertyValue[ 1 ];            // Setting the flag for overwriting            propertyvalue[ 0 ] = new PropertyValue();            propertyvalue[ 0 ].Name = "URL";            propertyvalue[ 0 ].Value = stringOriginalFile;            // Setting the filter name            //propertyvalue[ 1 ] = new PropertyValue();            //propertyvalue[ 1 ].Name = "FilterName";            //propertyvalue[ 1 ].Value = context.get("convertFilterName");            XFrame frame = desktop.getCurrentFrame();            //XFrame frame = (XFrame) UnoRuntime.queryInterface(XFrame.class, desktop);            Object dispatchHelperObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.DispatchHelper", xcomponentcontext);            XDispatchHelper dispatchHelper = (XDispatchHelper) UnoRuntime.queryInterface(XDispatchHelper.class, dispatchHelperObj);            XDispatchProvider dispatchProvider = (XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, frame);            dispatchHelper.executeDispatch(dispatchProvider, ".uno:CompareDocuments", "", 0, propertyvalue);                               // Preparing properties for storing the document            propertyvalue = new PropertyValue[ 1 ];            // Setting the flag for overwriting            propertyvalue[ 0 ] = new PropertyValue();            propertyvalue[ 0 ].Name = "Overwrite";            propertyvalue[ 0 ].Value = new Boolean(true);            // Setting the filter name            //propertyvalue[ 1 ] = new PropertyValue();            //propertyvalue[ 1 ].Name = "FilterName";            //propertyvalue[ 1 ].Value = context.get("convertFilterName");                        Debug.logInfo("stringOutFile: "+stringOutFile, module);            // Storing and converting the document            xstorable.storeToURL(stringOutFile, propertyvalue);                        // Getting the method dispose() for closing the document            XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class,            xstorable);                        // Closing the converted document            xcomponent.dispose();                        Map results = ServiceUtil.returnSuccess();            return results;        } catch (Exception e) {            Debug.logError(e, "Error in OpenOffice operation: ", module);            return ServiceUtil.returnError("Error converting document: " + e.toString());        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -