【问题标题】:Opening multiple documents in a same window with UNO使用 UNO 在同一窗口中打开多个文档
【发布时间】:2010-07-28 10:01:53
【问题描述】:

我在 python 中有一个脚本,它使用 pyuno 从许多 excel 文件中提取数据。

我的问题是,对于每个文件,我用

打开和关闭一个窗口
url = unohelper.systemPathToFileUrl(os.path.abspath(file_name))
file = desktop.loadComponentFromURL(url, "_blank", 0, () )

file.close(True)

有没有什么方法可以在不打开窗口的情况下从文件中提取数据?或者至少不用为每个文件打开一个新窗口?

【问题讨论】:

    标签: openoffice.org uno pyuno


    【解决方案1】:

    这会打开一个文档而不显示:

    // Call the bootstrap to get the Component context
    com.sun.star.uno.XComponentContext oComponentContext = null;
    try
    {
        String oooExeFolder = "C:/Program Files/OpenOffice.org 3/program/";
        oComponentContext = BootstrapSocketConnector.bootstrap(oooExeFolder);
    }
    catch(com.sun.star.comp.helper.BootstrapException ex)
    {
        System.out.println(ex.getMessage());
    }
    
    if(oComponentContext != null)
    {
    
        com.sun.star.lang.XComponent oComponent;
        com.sun.star.sheet.XSpreadsheetDocument oSpreadDocument;
        com.sun.star.frame.XComponentLoader oComponentLoader;
        try
        {
            com.sun.star.lang.XComponent oComponent;
                // Get the service manager
            com.sun.star.lang.XMultiComponentFactory oMultiComponentFactory =
                    oComponentContext.getServiceManager();
                // Create a new desktop instance
                Object oDesktop =
                    oMultiComponentFactory.createInstanceWithContext(
                     "com.sun.star.frame.Desktop", oComponentContext);
                 // Create a new component loader within our desktop
                oComponentLoader  =
                    (com.sun.star.frame.XComponentLoader)
                    com.sun.star.uno.UnoRuntime.queryInterface(
                    com.sun.star.frame.XComponentLoader.class,
                                      oDesktop);
                /// Read the created file
                com.sun.star.beans.PropertyValue[] property = new com.sun.star.beans.PropertyValue[2];
                property[0] = new com.sun.star.beans.PropertyValue();
                property[0].Name = new String("ReadOnly");
                property[0].Value = new Boolean(true);
                property[1] = new com.sun.star.beans.PropertyValue();
                property[1].Name = new String("Hidden");
                property[1].Value = new Boolean(true);
                oComponent =
                    oComponentLoader.loadComponentFromURL(
                            "file:///c:/test/sheetdoc.ods",
                            // "private:factory/swriter", //Blank document
                            "_default",                   // new frame
                            0,                      // no search flags
                            // read only
                            property);
                // Get the spread sheet
                oSpreadDocument =
                    (com.sun.star.sheet.XSpreadsheetDocument)
                    com.sun.star.uno.UnoRuntime.queryInterface(
                            com.sun.star.sheet.XSpreadsheetDocument.class, oComponent);
        }
    catch(Exception  ex)
    {
        System.out.println("An exception occurs at opening of document: "+ex.getMessage());
        return;
    }
    
     } // end of if
     System.exit(0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多