顯示具有 OpenOffice 標籤的文章。 顯示所有文章
顯示具有 OpenOffice 標籤的文章。 顯示所有文章

2021年8月14日 星期六

[研究]OpenOffice 4.1.10、LibreOffice 7.0.6 支援那些檔案格式 (副檔名)

[研究]OpenOffice 4.1.10、LibreOffice 7.0.6 支援那些檔案格式 (副檔名)

2021-08-14

Apache OpenOffice
https://www.openoffice.org/zh-tw/
Apache_OpenOffice_4.1.10_Win_x86_install_zh-TW.exe

LibreOffice
https://zh-tw.libreoffice.org/download/libreoffice-still/
LibreOffice_7.0.6_Win_x64.msi

LibreOffice 可以儲存成 Excel 的 .xlsx 檔案格式。












********************************************************************************





(完)

2019年8月25日 星期日

[研究] [C#] 使用 LibreOffice 6.2.6 SDK、OpenOffice 4.1.6 SDK讀取/寫入試算表(Calc) 檔案 (.ods)

[研究] [C#] 使用 LibreOffice 6.2.6 SDK、OpenOffice 4.1.6 SDK讀取/寫入試算表(Calc) 檔案 (.ods)

2091-08-24

本測試使用 LibreOffice 6.2.6 SDK,是 x64 版。
https://zh-tw.libreoffice.org/download/libreoffice-still/

OpenOffice SDK 4.1.6 實際測試也可以用 ,但它還在 x86 版。
https://www.openoffice.org/zh-tw/





















(下圖) 測試用的檔案,做好記得要關閉,否則後面程式跑會出錯。

(下圖) 執行結果,抓出 Cell (2,2) 的 9


Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using uno.util;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.uno;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            string fileName = "";
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "ods files (*.ods)|*.ods|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        label1.Text = openFileDialog1.FileName; // 包含路徑
                        string inputFilePath = openFileDialog1.FileName; // 包含路徑

                        fileName = openFileDialog1.FileName; // 包含路徑


                        XComponentContext context = Bootstrap.bootstrap();
                        XMultiServiceFactory factory = (XMultiServiceFactory)context.getServiceManager();
                        XComponentLoader loader = (XComponentLoader)factory.createInstance("com.sun.star.frame.Desktop");

                        PropertyValue[] args1 = new PropertyValue[1];
                        args1[0] = new PropertyValue();
                        args1[0].Name = "Hidden";
                        args1[0].Value = new uno.Any((Boolean)true);

                        XSpreadsheetDocument doc = (XSpreadsheetDocument)loader.loadComponentFromURL("file:///" + fileName, "_blank", 0, args1);
                        XSpreadsheets sheets = doc.getSheets();
                        //XSpreadsheet sheet = (XSpreadsheet)sheets.getByName("Sheet1").Value;
                        XSpreadsheet sheet = (XSpreadsheet)sheets.getByName("工作表1").Value;  //  繁體中文版 Office

                        XCell cell = sheet.getCellByPosition(2, 2);

                        label1.Text = cell.getValue().ToString();
                    }
                }
                catch (unoidl.com.sun.star.uno.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}

增加寫入測試

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using uno.util;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.sdbc;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.uno;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            string fileName = "";
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "ods files (*.ods)|*.ods|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        label1.Text = openFileDialog1.FileName; // 包含路徑
                        string inputFilePath = openFileDialog1.FileName; // 包含路徑

                        fileName = openFileDialog1.FileName; // 包含路徑


                        XComponentContext context = Bootstrap.bootstrap();
                        XMultiServiceFactory factory = (XMultiServiceFactory)context.getServiceManager();
                        XComponentLoader loader = (XComponentLoader)factory.createInstance("com.sun.star.frame.Desktop");

                        PropertyValue[] args1 = new PropertyValue[1];
                        args1[0] = new PropertyValue();
                        args1[0].Name = "Hidden";
                        args1[0].Value = new uno.Any((Boolean)true);

                        XSpreadsheetDocument doc = (XSpreadsheetDocument)loader.loadComponentFromURL("file:///" + fileName, "_blank", 0, args1);
                        XSpreadsheets sheets = doc.getSheets();
                        //XSpreadsheet sheet = (XSpreadsheet)sheets.getByName("Sheet1").Value;
                        XSpreadsheet sheet = (XSpreadsheet)sheets.getByName("工作表1").Value;  //  繁體中文版 Office

                        XCell cell = sheet.getCellByPosition(2, 2);

                        label1.Text = cell.getValue().ToString();

                        // ----- Write test
                        cell.setValue(1000);
                        cell = sheet.getCellByPosition(1, 1);
                        cell.setFormula("Title");

                        XStorable xstorable = (XStorable)doc;
                        PropertyValue[] storeProps = new PropertyValue[1];
                        storeProps[0] = new PropertyValue();
                        storeProps[0].Name = "Overwrite";
                        storeProps[0].Value = new uno.Any((Boolean)true);

                        try
                        {
                            String sURL = "file:///d:/Temp/SaveTest2.ods";
                            xstorable.storeAsURL(sURL, storeProps);
                            //if (doc != null)
                            //{
                            //    //XCloseable xCloseable = (XCloseable)doc;
                            //    //xCloseable.close();
                            //    doc = null;
                            // }
                        }
                        catch (unoidl.com.sun.star.uno.Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
                catch (unoidl.com.sun.star.uno.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}



********************************************************************************
2020-05-21 補充

安裝 LibreOffice_6.3.6_Win_x64_sdk.msi,不安裝 LibreOffice_6.3.6_Win_x64.msi
( 實際測試只安裝 SDK,不安裝 LibreOffice 本身) 
Visual Studio 2019 v16.6.0 ,方案使用 .NET Framework 4.7.2 或 4.8,編譯正常,執行出錯


WindowsFormsApp1.exe' (CLR v4.0.30319: DefaultDomain): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: DefaultDomain): 已載入 'C:\CodeTemp\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\WindowsFormsApp1.exe'。已載入符號。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\CodeTemp\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\cli_basetypes.dll'。建置的模組沒有符號。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\CodeTemp\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\cli_uretypes.dll'。建置的模組沒有符號。
於 System.BadImageFormatException 擲回例外狀況: 'System.Windows.Forms.dll'
類型 'System.BadImageFormatException' 的未處理例外狀況發生於 System.Windows.Forms.dll
無法載入檔案或組件 'cli_cppuhelper, Version=1.0.23.0, Culture=neutral, PublicKeyToken=ce2cb7e279207b9e' 或其相依性的其中之一。 試圖載入格式錯誤的程式。

'[8552] WindowsFormsApp1.exe: 程式追蹤' 程式以返回碼 0 (0x0) 結束。
'[8552] WindowsFormsApp1.exe' 程式以返回碼 -1 (0xffffffff) 結束。

把 LibreOffice 6.3.6.2 安裝了,關閉方案,重新開啟,依然是編譯成功,執行失敗。

把參照  cli_cppuhelper 移除,編譯到下面這行失敗。
 XComponentContext context = Bootstrap.bootstrap();

暫時解決不了。

********************************************************************************
2020-05-21 補充

loader.loadComponentFromURL() 只能用於 .NET Framework 的 Windows Form App 桌面應用程式,無法用 ASP.NET 的 WinForms 網頁程式。

********************************************************************************
2020-05-21 補充

移除 LibreOffice 和 LibreOffice SDK,改安裝
Apache_OpenOffice-SDK_4.1.7_Win_x86_install_en-US.exe
沒安裝 Apache_OpenOffice_4.1.7_Win_x86_install_zh-TW.exe

Visual Studio 2019 v16.6.0 中,把程式的舊參照砍光,參照改用
C:\Program Files (x86)\OpenOffice_4.1.7_SDK\sdk\cli
目錄的,編譯成功,執行失敗。

WindowsFormsApp1.exe' (CLR v4.0.30319: DefaultDomain): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: DefaultDomain): 已載入 'C:\CodeTemp\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\WindowsFormsApp1.exe'。已載入符號。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\CodeTemp\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\cli_basetypes.dll'。建置的模組沒有符號。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\CodeTemp\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\cli_uretypes.dll'。建置的模組沒有符號。
WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_zh-Hant_b77a5c561934e089\mscorlib.resources.dll'。建置的模組沒有符號。
於 System.IO.FileLoadException 擲回例外狀況: 'System.Windows.Forms.dll'
類型 'System.IO.FileLoadException' 的未處理例外狀況發生於 System.Windows.Forms.dll
混合模式組件是針對版本 'v2.0.50727' 的執行階段建置的,無法在沒有其他組態資訊的情況下載入 4.0 執行階段中。

WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): 已載入 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\common7\ide\PrivateAssemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
'[6548] WindowsFormsApp1.exe: 程式追蹤' 程式以返回碼 0 (0x0) 結束。
'[6548] WindowsFormsApp1.exe' 程式以返回碼 -1 (0xffffffff) 結束。

(下圖)本電腦是 Windows 10 Version 1909,確認 .NET Framework 2.0 是有安裝的。

又碰上以前某版本 LibreOffice SDK 和 OpenOffice SDK 時碰到的不好經驗了。

(完)

相關

[研究] [C#] 使用 LibreOffice 6.2.6 SDK、OpenOffice 4.1.6 SDK讀取/寫入試算表(Calc) 檔案 (.ods)
https://shaurong.blogspot.com/2019/08/c-libreoffice-626-sdk-calc-ods.html

[研究] [C#] 使用 OpenOffice 4.1.3 SDK 讀取試算表(Calc) 檔案 (.ods)
http://shaurong.blogspot.com/2016/12/c-openoffice-413-sdk-calc-ods.html

[研究] [C#] 使用 LibreOffice 5.1.6、5.3.0 SDK 讀取試算表(Calc) 檔案 (.ods)
http://shaurong.blogspot.com/2016/12/c-libreoffice-516-sdk-calc-ods.html

[研究] [C#] 讀取 LibreOffice、OpenOffice 的試算表(Calc) 檔案 (.ods)(使用 .NET 4.5 的 ZipFile)
http://shaurong.blogspot.com/2016/12/c-libreoffice-516-calc-ods-net-45.html

[研究] [C#] 使用 Apose.Cells 讀取 LibreOffice 5.1.6、OpenOffice 4.1.3 試算表(Calc) 檔案 (.ods)
http://shaurong.blogspot.com/2017/01/c-aposecells-libreoffice-516openoffice.html

[研究] [C#] 使用 GemBox.Spreadsheet 讀取LibreOffice、OpenOffice試算表(Calc) 檔案 (.ods)
http://shaurong.blogspot.com/2016/12/c-gemboxspreadsheet-libreofficeopenoffi.html

[研究] OpenOffice 4.1.3 試算表(Calc) 保護鎖定儲存格不被修改
http://shaurong.blogspot.com/2016/12/openoffice-413-calc.html

[研究] 使用OpenOffice 4.1.3試算表(Calc)建立「下拉式選單」來輸入資料
http://shaurong.blogspot.com/2016/12/openoffice-413calc.html

2016年12月30日 星期五

[研究] [C#] 使用 OpenOffice 4.1.3 SDK 讀取試算表(Calc) 檔案 (.ods)

[研究] [C#] 使用 OpenOffice 4.1.3 SDK 讀取試算表(Calc) 檔案 (.ods)

2016-12-30

先簡單說結論
OpenOffice 4.1.3 SDK  只支援 .NET 2,但不支援 .NET 4.x
LibreOffice 5.3.0 SDK 支援 .NET 4.x,但不支援 x64

Apache OpenOffice 4.1.3 SDK 下載
https://www.openoffice.org/download/sdk/

工具:Visual Studio 2015 with Update 3

參考這篇作程式
[研究] [C#] 讀取 LibreOffice、OpenOffice 的試算表(Calc) 檔案 (.ods)(使用 .NET 4.5 的 ZipFile)
http://shaurong.blogspot.com/2016/12/c-libreoffice-516-calc-ods-net-45.html

下載安裝 OpenOffice 4.1.3 SDK
方案的 "參考" 加入 C:\Program Files(x86)\OpenOffice 4\sdk\cli 目錄的
cli_basetypes, cli_cppuhelper, cli_oootypes, cli_ure, and cli_uretypes.dll

其中 WinForm 程式 Form1.cs 片段換成下面


private void button2_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "ods files (*.ods)|*.ods|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        label1.Text = openFileDialog1.FileName; // 包含路徑
                        string inputFilePath = openFileDialog1.FileName; // 包含路徑

                        fileName = openFileDialog1.FileName; // 包含路徑


                        XComponentContext context = Bootstrap.bootstrap();
                        XMultiServiceFactory factory = (XMultiServiceFactory)context.getServiceManager();
                        XComponentLoader loader = (XComponentLoader)factory.createInstance("com.sun.star.frame.Desktop");

                        PropertyValue[] args1 = new PropertyValue[1];
                        args1[0] = new PropertyValue();
                        args1[0].Name = "Hidden";
                        args1[0].Value = new uno.Any((Boolean)true);

                        //XSpreadsheetDocument doc = (XSpreadsheetDocument)loader.loadComponentFromURL("file:///c:/a.ods", "_blank", 0, args1);
                        XSpreadsheetDocument doc = (XSpreadsheetDocument)loader.loadComponentFromURL("file:///" + fileName, "_blank", 0, args1);
                        XSpreadsheets sheets = doc.getSheets();
                        //XSpreadsheet sheet = (XSpreadsheet)sheets.getByName("Sheet1").Value;
                        XSpreadsheet sheet = (XSpreadsheet)sheets.getByName("工作表1").Value;

                        XCell cell = sheet.getCellByPosition(2, 2);
                        label2.Text = cell.ToString();

                        cell.setValue(1000);
                        cell = sheet.getCellByPosition(2, 3);
                        cell.setFormula("Title");

                        XStorable xstorable = (XStorable)doc;
                        PropertyValue[] storeProps = new PropertyValue[1];
                        storeProps[0] = new PropertyValue();
                        storeProps[0].Name = "Overwrite";
                        storeProps[0].Value = new uno.Any((Boolean)true);

                        try
                        {
                            String sURL = "file:///c:/a2.ods";
                            xstorable.storeAsURL(sURL, storeProps);
                            if (doc != null)
                            {
                                XCloseable xCloseable = (XCloseable)doc;
                                xCloseable.close(true);
                                doc = null;
                            }
                        }
                        catch (unoidl.com.sun.star.uno.Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }



                        //DataSet ds = ReadOdsFile(inputFilePath);
                        DataSet ds = ReadOdsFile2(inputFilePath);

                        dataGridView1.AutoGenerateColumns = true;
                        dataGridView1.DataSource = ds; // dataset
                        dataGridView1.DataSource = ds.Tables[0];



                        //using (myStream)
                        //{
                        //    // Insert code to read the stream here.
                        //}
                    }
                }
                catch (unoidl.com.sun.star.uno.Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

(下圖)執行後發生錯誤,Apache OpenOffice 4.1.3 SDK  疑似支援 .NET 2,但不支援 .NET 4.x


*****

用 .NET 2.0 改寫一次
WinForm 程式 Form1.cs 片段


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

// 下載安裝 OpenOffice 4.1.3 SDK
// "參考" 加入 C:\Program Files(x86)\OpenOffice 4\sdk\cli 目錄的
// cli_basetypes, cli_cppuhelper, cli_oootypes, cli_ure, and cli_uretypes.dll
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;
using uno.util;

using unoidl.com.sun.star.util;
//using unoidl.com.sun.star.sdbc;

namespace ODSOpenOffice413SDKDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string fileName = "";
        private void button1_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "ods files (*.ods)|*.ods|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        label1.Text = openFileDialog1.FileName; // 包含路徑
                        string inputFilePath = openFileDialog1.FileName; // 包含路徑

                        fileName = openFileDialog1.FileName; // 包含路徑

                        //XComponentContext context = Bootstrap.bootstrap();
                        XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
                        XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager();
                        XComponentLoader loader = (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 = loader.loadComponentFromURL(url, "_blank", 0, loadProps);

                        PropertyValue[] args1 = new PropertyValue[1];
                        args1[0] = new PropertyValue();
                        args1[0].Name = "Hidden";
                        args1[0].Value = new uno.Any((Boolean)true);

                        //XSpreadsheetDocument doc = (XSpreadsheetDocument)loader.loadComponentFromURL("file:///c:/a.ods", "_blank", 0, args1);
                        XSpreadsheetDocument doc = (XSpreadsheetDocument)loader.loadComponentFromURL("file:///" + fileName, "_blank", 0, args1);

                        XSpreadsheets sheets = doc.getSheets();
                        //XSpreadsheet sheet = (XSpreadsheet)sheets.getByName("Sheet1").Value;
                        XSpreadsheet sheet = (XSpreadsheet)sheets.getByName("工作表1").Value;

                        XCell cell = sheet.getCellByPosition(2, 2);
                        label1.Text = cell.ToString();

                        cell.setValue(1000);
                        cell = sheet.getCellByPosition(2, 3);
                        cell.setFormula("Title");

                        XStorable xstorable = (XStorable)doc;
                        PropertyValue[] storeProps = new PropertyValue[1];
                        storeProps[0] = new PropertyValue();
                        storeProps[0].Name = "Overwrite";
                        storeProps[0].Value = new uno.Any((Boolean)true);

                        try
                        {
                            String sURL = "file:///c:/a2.ods";
                            xstorable.storeAsURL(sURL, storeProps);
                            if (doc != null)
                            {
                                XCloseable xCloseable = (XCloseable)doc;
                                xCloseable.close(true);
                                doc = null;
                            }
                        }
                        catch (unoidl.com.sun.star.uno.Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }



                        //DataSet ds = ReadOdsFile(inputFilePath);
                        //DataSet ds = ReadOdsFile2(inputFilePath);

                        //dataGridView1.AutoGenerateColumns = true;
                        //dataGridView1.DataSource = ds; // dataset
                        //dataGridView1.DataSource = ds.Tables[0];



                        //using (myStream)
                        //{
                        //    // Insert code to read the stream here.
                        //}
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }
    }
}


(下圖) 編譯有警告,似乎只支援 x86,不支援 x64
OpenOffice 4.1.3 SDK 安裝目錄為 C:\Program Files(x86)\OpenOffice 4\sdk\   也可知道


(下圖) 實際執行也不行


以下是雜七雜八參考資料
**********

http://jehupc.exblog.jp/12764408/


XComponentContext context = Bootstrap.bootstrap();
XMultiServiceFactory factory = (XMultiServiceFactory)context.getServiceManager();
XComponentLoader loader = (XComponentLoader)factory.createInstance("com.sun.star.frame.Desktop");

PropertyValue[] args1 = new PropertyValue[1];
args1[0] = new PropertyValue();
args1[0].Name = "Hidden";
args1[0].Value = new uno.Any((Boolean)true);

XSpreadsheetDocument doc = (XSpreadsheetDocument)loader.loadComponentFromURL("file:///c:/a.ods", "_blank", 0, args1);
XSpreadsheets sheets = doc.getSheets();
XSpreadsheet sheet = (XSpreadsheet)sheets.getByName("Sheet1").Value;

XCell cell = sheet.getCellByPosition(2, 2);
cell.setValue(1000);
cell = sheet.getCellByPosition(2, 3);
cell.setFormula("Title");

XStorable xstorable = (XStorable)doc;
PropertyValue[] storeProps = new PropertyValue[1];
storeProps[0] = new PropertyValue();
storeProps[0].Name = "Overwrite";
storeProps[0].Value = new uno.Any((Boolean)true);

try
{
    String sURL = "file:///c:/a2.ods" ;
    xstorable.storeAsURL(sURL, storeProps);
    if (doc != null)
    {
        XCloseable xCloseable = (XCloseable)doc;
        xCloseable.close(true);
        doc = null;
 }
}
catch (unoidl.com.sun.star.uno.Exception ex)
{
    MessageBox.Show(ex.Message);
}


**********

https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/CLI/Writing_Client_Programs

**********

Use OpenOffice Uno CLI with C# to create a spreadsheet
http://stackoverflow.com/questions/4743738/use-openoffice-uno-cli-with-c-sharp-to-create-a-spreadsheet

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);
        }
    }
}

Calc/API/Programming
https://wiki.openoffice.org/wiki/Calc/API/Programming
很多範例,但為 C++

How to read ODS Open Office Calc spreadsheet document using Bytescout Spreadsheet for .NET
https://bytescout.com/products/developer/spreadsheetsdk/bytescoutxls_read_ods_open_office_calc_spreadsheet.html

**********

http://www.it1352.com/387358.html


Tutorial for implementing OpenOffice in C++/C#/Java
https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=60522

Read ODS (Open Office Calc) Files
https://s3.amazonaws.com/bytescout.com/files/help/BytescoutSpreadsheetSDK/html/71015e6a-b760-44c5-b53a-0b9c15f7d366.htm
(完)

相關

[研究] [C#] 讀取 LibreOffice、OpenOffice 的試算表(Calc) 檔案 (.ods)(使用 .NET 4.5 的 ZipFile)
http://shaurong.blogspot.com/2016/12/c-libreoffice-516-calc-ods-net-45.html