2016年12月31日 星期六

[研究] [C#] 使用 GemBox.Spreadsheet 讀取LibreOffice、OpenOffice試算表(Calc) 檔案 (.ods)

[研究] [C#] 使用 GemBox.Spreadsheet 41.3.30.1038讀取 LibreOffice 5.1.6、OpenOffice 4.1.3 試算表(Calc) 檔案 (.ods)

2016-12-30

這是商業付費軟體,雖然有30天試用版,但功能有限制
Maximum number of rows per sheet is 150.
Maximum number of sheets per workbook is 5.

參考

How to Access XLS, XLSX, ODS and CSV files from your .NET applications?
https://www.gemboxsoftware.com/spreadsheet/articles/access-xls-xlsx-ods-csv-net

Excel Library for C# and VB.NET applications
https://www.gemboxsoftware.com/spreadsheet/examples/c-sharp-vb-net-excel-library/601

Visual Studio 2015 with Update 3

Test.ods 內容如下,放在 D:\CodeTemp\ODSDemo3\ODSDemo3\bin\Debug 目錄。










WinForm 的 Form1.cs

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

using GemBox.Spreadsheet;
// https://www.gemboxsoftware.com/spreadsheet/free-version
//Limitations
//
//GemBox.Spreadsheet Free delivers the same performance and set of features as the Professional version.However, the following limitations are imposed:
//
//Maximum number of rows per sheet is 150.
//Maximum number of sheets per workbook is 5.

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

        private void button1_Click(object sender, EventArgs e)
        {
            GemBox.Spreadsheet.SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
            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; // 包含路徑

                        //var workbook = ExcelFile.Load("Workbook.ods", odsLoadOptions);
                        var workbook = ExcelFile.Load(inputFilePath);

                        // Select active worksheet.
                        var worksheet = workbook.Worksheets.ActiveWorksheet;

                        // Display the value of first cell in MessageBox.
                        MessageBox.Show(worksheet.Cells["A1"].GetFormattedValue());
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}


執行結果

如果沒有下面這行

GemBox.Spreadsheet.SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

會出現下面圖片的錯誤

Free version serial key is: FREE-LIMITED-KEY

(完)

沒有留言:

張貼留言