【问题标题】:Use OpenOffice Uno CLI with C# to create a spreadsheet使用 OpenOffice Uno CLI 和 C# 创建电子表格
【发布时间】:2011-06-12 05:44:32
【问题描述】:

到目前为止,我已经找到了几个讨论 ODS 文件创建的来源:How to create ODS documents in .NetHow to create .odt files with C#.NET?

最有趣的是an explanation for opening calc files。但是,这会以全屏方式打开 OpenOffice,我正在寻找的是在不实际打开 Openoffice 的情况下写入 Calc 文件 (.ods) 的某种方式。这样我就可以编写一个函数,只打开一个保存文件对话框,获取文件名,然后创建并保存 .ods 文件。

是否有任何 C# 代码示例可用于执行此类操作?

【问题讨论】:

    标签: c# openoffice.org uno


    【解决方案1】:

    所以我终于解决了这个问题,并希望避免其他人再次经历这个问题。 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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多