使用 OpenOffice Uno CLI 和 C# 创建电子表格

发布于 2024-10-13 02:48:20 字数 595 浏览 4 评论 0原文

到目前为止,我找到了一些讨论 ODS 文件创建的资源:如何在 .Net 中创建 ODS 文档如何使用 C#.NET 创建 .odt 文件?< /a>

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

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

I have so far found a couple of sources that discuss creation of ODS files: How to create ODS documents in .Net and
How to create .odt files with C#.NET?

And most interestingly an explanation for opening calc files. However this opens OpenOffice in fullscreen, what im looking for is some way to write to a Calc file (.ods) without actually opening Openoffice. So that I can write a function that just opens a savefiledialog, gets the filename and then creates and saves the .ods file.

Are there is any C# code examples available to do such a thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

緦唸λ蓇 2024-10-20 02:48:20

所以我终于解决了这个问题,并希望避免其他人再次经历这个问题。对我来说HEADACE的基本点是:

  1. 使用正斜杠而不是反斜杠(例如它的C:/而不是C:\< /code> )
  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);
        }
    }
}

So I have finally resolved this issue and want to save others the hazel of going through this again. Basic points of HEADACE for me were:

  1. Use forward slashes instead of backward slashes (e.g. its C:/ not C:\ )
  2. The Filtername used should be set to the engine used to save the document. Possible values include writer8, calc8, MS Excel 97, so for spreadsheets you obviously need to use calc8
  3. If you dont want that OpenOffice pops up in the forground and wait for it to get filled with your data, then use the PropertyValue and set Hidden to true.

Happy coding and dont forget to install the OpenOffice SDK to be able to add the unoidl references:

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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文