2020年5月21日 星期四

[研究] [C#][ASP.NET] 使用 Aspose.Cells 20.6.0 讀寫 LibreOffice、OpenOffice 試算表(Calc) ODF 檔案 (.ods)

[研究] [C#][ASP.NET] 使用 Aspose.Cells 20.6 讀寫 LibreOffice、OpenOffice 試算表(Calc) ODF 檔案 (.ods)

2020-05-21
2020-06-18 更新

Aspose.Cells for .NET - Evaluation Version Limitations
Aspose.Cells 是商業付費軟體,但有試用版,有下面限制。
* 100 次 excel 開檔限制
* 組態設定限制
* 使用中工作表設定

參考

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
寫入 .ods 版本 1.1 和 1.2 方法

設定授權檔案方法
各種範例

開放文件格式(Open Document Format,簡稱ODF)
測試用的 .ods 可以用 Excel 去建立,另存成 .ods 檔案。

Visual Studio 2019 v16.6.0
ASP.NET +  WinForm + WebApplication

NuGet 安裝 Aspose.Cells 的  20.6.0 版,實際測試可用。

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
Inherits="WebApplicationAposeCell1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            <br />
            <asp:Label ID="Label1" runat="server" ></asp:Label>
        </div>
    </form>
</body>
</html>

Default.aspx.cs

using Aspose.Cells;
using System;
using System.IO;

namespace WebApplicationAposeCell1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        // http://www.aspose.com/docs/display/cellsnet/Opening+Files
        protected void Button1_Click(object sender, EventArgs e)
        {
            using (FileStream fs = File.Open(@"C:\\Temp\\活頁簿1.ods", FileMode.Open))
            {
                Workbook workbook = new Workbook(fs);
                Cell cell = workbook.Worksheets[0].Cells["A1"];
                Label1.Text = cell.Value.ToString();
            }
        }
    }
}







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

改寫,增加寫入功能測試。

注意,此測試寫入 C:\temp 目錄,故此目錄必須開放讓 IIS_IUSRS 使用者有寫入權限。

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplicationAposeCell1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
                        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            &nbsp;
                        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
            <br />
            <asp:Label ID="Label1" runat="server" ></asp:Label>
        </div>
    </form>
</body>
</html>



Default.aspx.cs

using Aspose.Cells;
using System;
using System.IO;

namespace WebApplicationAposeCell1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            using (FileStream fs = File.Open(@"C:\\Temp\\活頁簿1.ods", FileMode.Open))
            {
                Workbook workbook = new Workbook(fs);
                Cell cell = workbook.Worksheets[0].Cells["A1"];
                Label1.Text = cell.Value.ToString();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
                // The path to the documents directory.
                //string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                string dataDir = @"C:\temp\";

                // Create workbook
                Workbook workbook = new Workbook();

                // Access first worksheet
                Worksheet worksheet = workbook.Worksheets[0];

                // Put some value in cell A1
                Cell cell = worksheet.Cells["A1"];
                cell.PutValue("Welcome to Aspose!");

                // Save ODS in ODF 1.2 version which is default
                OdsSaveOptions options = new OdsSaveOptions();
                workbook.Save(dataDir + "ODF1.2_out.ods", options);

                // https://docs.aspose.com/display/cellsnet/Different+Ways+to+Save+Files
                // Save ODS in ODF 1.1 version
                options.IsStrictSchema11 = true;
                workbook.Save(dataDir + "ODF1.1_out.ods", options);
                // Save in Excel 97 ~ 2003 format
                workbook.Save(dataDir + ".output.xls");
                // OR
                workbook.Save(dataDir + ".output..xls", new XlsSaveOptions(SaveFormat.Excel97To2003));

                // Save in Excel2007 xlsx format
                workbook.Save(dataDir + ".output.xlsx", SaveFormat.Xlsx);

                // Save in Excel2007 xlsb format
                workbook.Save(dataDir + ".output.xlsb", SaveFormat.Xlsb);

                // Save in ODS format
                workbook.Save(dataDir + ".output.ods", SaveFormat.ODS);

                // Save in Pdf format
                workbook.Save(dataDir + ".output.pdf", SaveFormat.Pdf);

                // Save in Html format
                workbook.Save(dataDir + ".output.html", SaveFormat.Html);

                // Save in SpreadsheetML format
                workbook.Save(dataDir + ".output.xml", SaveFormat.SpreadsheetML);
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message.ToString();
            }
        }
    }
}



如果要輸出不同格式檔案,可參考。

測試結果。




(完)

相關

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


沒有留言:

張貼留言