到目前为止,我找到了一些来源,讨论了ODS文件的创建: 如何在.NET中创建ODS文档如何使用C#.NET创建.ODT文件?

最有趣的是 打开计算文件的解释. 。但是,这会在全屏上打开OpenOffice,IM寻找的是将其写入Calc File(.ods)而不实际打开OpenOffice的方法。这样我就可以编写一个仅打开SaveFileDialog,获取文件名然后创建并保存.ods文件的函数。

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

有帮助吗?

解决方案

因此,我终于解决了这个问题,并希望拯救他人再次经历这一问题。基本点 对我来说是:

  1. 利用 向前斜线 而不是向后斜线(例如 C:/ 不是 C:\ )
  2. Filtername 所使用的应设置为用于保存文档的引擎。可能的值包括 writer8, calc8, MS Excel 97, ,因此对于电子表格,您显然需要使用 calc8
  3. 如果您不希望那个开放式弹出式弹出式弹出,然后等待它填充您的数据,请使用 PropertyValue 并设置 Hiddentrue.

快乐的编码,不要忘记安装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);
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top