2011-01-20 113 views
4

我迄今發現一對夫婦的是討論建立的ODS文件來源:How to create ODS documents in .NetHow to create .odt files with C#.NET?使用OpenOffice歐諾CLI與C#創建一個電子表格

而且最有趣an explanation for opening calc files。然而,這將在全屏幕下打開OpenOffice,我正在尋找的是一些在沒有實際打開Openoffice的情況下寫入Calc文件(.ods)的方法。這樣我就可以編寫一個只打開savefiledialog的函數,獲取文件名,然後創建並保存.ods文件。

是否有任何C#代碼示例可用於做這樣的事情?

回答

6

因此,我終於解決了這個問題,並希望拯救他人再次經歷這個榛子。的HEADACE我的基本點是:

  1. 使用正斜槓而不是反斜線(例如其C:/沒有C:\
  2. 使用應設置爲用來保存文檔的引擎Filtername。可能的值包括writer8calc8MS Excel 97,因此對於電子表格你顯然需要使用calc8
  3. 如果你不希望出現這種情況的OpenOffice中於地面彈起並等待它得到填補你的數據,然後使用PropertyValue和將Hidden設置爲true

快樂編碼,不要忘記安裝OpenOffice的SDK,以便能夠添加UNOIDL引用:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using unoidl.com.sun.star.uno; 
using unoidl.com.sun.star.lang; 
using unoidl.com.sun.star.frame; 
using unoidl.com.sun.star.beans; 
using unoidl.com.sun.star.sheet; 
using unoidl.com.sun.star.container; 
using unoidl.com.sun.star.table; 
using unoidl.com.sun.star.text; 

namespace TimeScanner { 
    class ReportGenerator { 
     private const string fileName = 
      @"file:///C:/Documents and Settings/My Documents/Hours Report.ods"; 

     //Concrete Methods 
     internal XComponent openCalcSheet() { 
      XComponentContext oStrap = uno.util.Bootstrap.bootstrap(); 
      XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager(); 
      XComponentLoader desktop = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop"); 
      string url = @"private:factory/scalc"; 
      PropertyValue[] loadProps = new PropertyValue[1]; 
      loadProps[0] = new PropertyValue(); 
      loadProps[0].Name = "Hidden"; 
      loadProps[0].Value = new uno.Any(true); 
      //PropertyValue[] loadProps = new PropertyValue[0]; 
      XComponent document = desktop.loadComponentFromURL(url, "_blank", 0, loadProps); 
      return document; 
     } 

     public void writeToSheet(XComponent document) { 
      XSpreadsheets oSheets = ((XSpreadsheetDocument)document).getSheets(); 
      XIndexAccess oSheetsIA = (XIndexAccess) oSheets; 
      XSpreadsheet sheet = (XSpreadsheet) oSheetsIA.getByIndex(0).Value; 
      XCell cell = sheet.getCellByPosition(0, 0); //A1 
      ((XText)cell).setString("Cost"); 
      cell = sheet.getCellByPosition(1, 0); //B1 
      cell.setValue(200); 
      cell = sheet.getCellByPosition(1, 2); //B3 
      cell.setFormula("=B1 * 1.175"); 
     } 

     public void saveCalcSheet(XComponent oDoc) {   
      PropertyValue[] propVals = new PropertyValue[1]; 
      propVals[0] = new PropertyValue(); 
      propVals[0].Name = "FilterName"; 
      propVals[0].Value = new uno.Any("calc8"); 
      ((XStorable)oDoc).storeToURL(fileName, propVals); 
     } 
    } 
} 
相關問題