使用 OpenOffice 3 示例代码的“Hello World”

Posted

技术标签:

【中文标题】使用 OpenOffice 3 示例代码的“Hello World”【英文标题】:"Hello World" using OpenOffice 3 sample code 【发布时间】:2010-01-13 04:08:29 【问题描述】:

我正在尝试使用OpenOffice 3.0 API 和 SDK 在 WinXP MSVC 2008.NET 上编写一个简单的“Hello World”窗口。

这篇文章对初学者来说很大...http://api.openoffice.org/docs/DevelopersGuide/OfficeDev/OfficeDev.xhtml#1_1_OpenOffice.org_Application_Environment

“Hello World”的代码是什么样的?

【问题讨论】:

@aiw33k:应该是什么语言? javascript、Python、Java、C#、VB.NET、OOo Basic? C# 对我来说是完美的。谢谢。 【参考方案1】:

设置您的解决方案:http://blog.nkadesign.com/2008/net-working-with-openoffice-3/

然后是简单的例子:(http://opendocument4all.com/content/view/68/47/)

using  System;
// Add all needed CLI namespaces to the current class.
using unoidl.com.sun.star.lang;  
using unoidl.com.sun.star.uno;  
using unoidl.com.sun.star.bridge;  
using unoidl.com.sun.star.frame;  

namespace OOTutorial
 
    class OpenOfficeApp
     
        //Define a file name. Change this to an existing path!  
        private static string FileName = @"F:\odtfiles   est.odt";

        [STAThread] 
        static void Main( string [] args)  
        
            //Call the bootstrap method to get a new ComponentContext  
            //object. If OpenOffice isn't already started this will
            //start it and then return the ComponentContext.
            unoidl.com.sun.star.uno.XComponentContext localContext =         
                uno.util.Bootstrap.bootstrap(); 

            //Get a new service manager of the MultiServiceFactory type  
            //we need this to get a desktop object and create new CLI  
            //objects.  
            unoidl.com.sun.star.lang.XMultiServiceFactory multiServiceFactory =  
                (unoidl.com.sun.star.lang.XMultiServiceFactory)
                localContext.getServiceManager();

            //Create a new Desktop instance using our service manager  
            //Notice: We cast our desktop object to XComponent loader  
            //so that we could load or create new documents.  
            XComponentLoader componentLoader = 
                (XComponentLoader) 
                multiServiceFactory.createInstance(  "com.sun.star.frame.Desktop" ); 

            //Create a new blank writer document using our component  
            //loader object.  
            XComponent xComponent = componentLoader.loadComponentFromURL(   
                "private:factory/swriter",    //a blank writer document  
                "_blank", 0,       //into a blank frame use no searchflag  
                //use no additional arguments.  
                new unoidl.com.sun.star.beans.PropertyValue[0]); 

            //Cast our component to a the XText interface  

            //and write some simple text into document.                  
            ((unoidl.com.sun.star.text.XTextDocument)xComponent). 
                getText().setString("Hello I'm the first text!"); 

            //After we insert our text, we cast our component to XStorable  
            //to save it onto the harddisk  
            ((XStorable)xComponent).storeToURL( 
                //Convert the file path into a OpenOffice path  
                PathConverter(FileName), 
                //no additional arguments  
                new unoidl.com.sun.star.beans.PropertyValue[0]);  

            Console.WriteLine("Your first OpenOffice document is saved!"); 
            Console.ReadLine(); 
         

        ///  
        /// Convert into OO file format  
        ///  
        private static string PathConverter( string file)  
         
            try  
             
                file = file.Replace(@"\", "/"); 
                return "file:///"+file;  
             
            catch (System.Exception ex)  
             
                throw ex;  
             
         
     
 

【讨论】:

这是否需要在系统上安装现有的 OpenOffice?调用它的函数是Bootstrap.bootsrap() 服务的吗?

以上是关于使用 OpenOffice 3 示例代码的“Hello World”的主要内容,如果未能解决你的问题,请参考以下文章

OpenOffice Basic 宏与 openoffice API

Openoffice3.1 pyuno混淆错误

OpenOffice 3.3 破坏了我的代码

从 OpenOffice 保存为 pdf

使用 c# 操作 .ods (OpenOffice) 文件

将 Java UNO 代码从 OpenOffice 2.4 迁移到 3.0