2010-07-28 53 views
0

我有python中的腳本使用pyuno從多個excel文件中提取數據。在與UNO相同的窗口中打開多個文檔

我的問題是,對於每一個文件,我打開和關閉窗口

url = unohelper.systemPathToFileUrl(os.path.abspath(file_name)) 
file = desktop.loadComponentFromURL(url, "_blank", 0,()) 

file.close(True) 

有什麼辦法來提取這些文件數據,而無需在所有打開的窗口?或者至少不打開每個文件的新窗口?

回答

0

這將打開一個文件,而不顯示:

// 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); 
相關問題