2017年1月3日 星期二

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

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

2017-01-03

Apose.Cells 是商業付費軟體,只是有試用版。

參考

Download The Aspose Pty Ltd End User License Agreement
http://www.aspose.com/corporate/purchase/end-user-license-agreement.aspx

.NET APIs to manipulate Excel spreadsheets
http://www.aspose.com/products/cells/net

Opening Files
http://www.aspose.com/docs/display/cellsnet/Opening+Files

Save ODS File in ODF 1.1 and 1.2 Specifications
http://www.aspose.com/docs/display/cellsnet/Save+ODS+File+in+ODF+1.1+and+1.2+Specifications

Your First Aspose.Cells Application - Hello World
http://www.aspose.com/docs/display/cellsnet/Your+First+Aspose.Cells+Application+-+Hello+World

範例
https://github.com/aspose-cells/Aspose.Cells-for-.NET







Visual Studio 2015 with Update 3

Test.ods 內容如下,放在專案的 \bin\Debug 目錄。




using System;
using System.Windows.Forms;
using System.IO;

// 用 Nuget 加入參考
using Aspose.Cells;


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

        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; // 包含路徑

                        // http://www.aspose.com/docs/display/cellsnet/Licensing
                        //Aspose.Cells.License cellsLicense = new Aspose.Cells.License();
                        //cellsLicense.SetLicense("Aspose.Cells.lic");
                        //lic.SetLicense(@"d:\Aspose.Cells.GridWeb.lic.xml");
                        Workbook workbook = new Workbook(myStream);
                        Cell cell = workbook.Worksheets[0].Cells["A1"];
                        label1.Text = cell.Value.ToString();
                    }
                }
                catch (Exception ex)
                {
                    label1.Text =ex.Message.ToString();
                }
            }
        }
    }
}



(完)

沒有留言:

張貼留言