2024年3月29日 星期五

[研究]ASP.NET,WebForm,用 FreeDataExports 1.1.9 匯出 .ods

[研究]ASP.NET,WebForm,用 FreeDataExports 1.1.9 匯出 .ods

2024-03-29

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

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

官方網站
https://github.com/ryankueter/FreeDataExports

官方的程式範例有問題,本篇測試使用時修正修改過。

Default.aspx

<%@ Page Async="true" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.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">
        <asp:Button ID="Button1" runat="server" Text="匯出(.ods)" OnClick="Button_Export_ODS_by_FeeeDataExports_Click" />
        <asp:Button ID="Button2" runat="server" Text="匯出(.ods)" OnClick="Button_Export_ODS_by_FeeeDataExports2_Click" />
    </form>
</body>
</html>



Default.aspx.cs

using FreeDataExports;
using System;

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

        }

        #region === protected void Button_Export_ODS_by_FeeeDataExports_Click(object sender, EventArgs e) ===
        protected async void Button_Export_ODS_by_FeeeDataExports_Click(object sender, EventArgs e)
        {
            // https://github.com/ryankueter/FreeDataExports

            string path = @"C:\Temp\1.ods";

            // Create a new workbook
            var workbook = new DataExport().CreateODSv1_3();

            // Optional - Add some metadata
            workbook.CreatedBy = "Jane Doe";

            // Optional - Change the font size
            workbook.FontSize = 11;

            // Create worksheets
            var orders = workbook.AddWorksheet("Orders");
            var inventory = workbook.AddWorksheet("Inventory");

            // Add column titles
            orders.AddRow()
                .AddCell("OrderId", DataType.String)
                .AddCell("Item", DataType.String)
                .AddCell("Units", DataType.String)
                .AddCell("Price", DataType.String)
                .AddCell("OrderDate", DataType.String)
                .AddCell("SalesAssoc", DataType.String)
                .AddCell("Delivered", DataType.String);

            // Add data
            //foreach (var o in orders)
            //{
            //    orders.AddRow()
            //        .AddCell(o.OrderId, DataType.Number)
            //        .AddCell(o.Item, DataType.String)
            //        .AddCell(o.Units, DataType.Number)
            //        .AddCell(o.Price, DataType.Currency)
            //        .AddCell(o.OrderDate, DataType.LongDate)
            //        .AddCell(o.SalesAssociate, DataType.String)
            //        .AddCell(o.Delivered, DataType.Boolean);
            //}

            // Add data directly without foreach loop
            orders.AddRow()
                .AddCell("12345", DataType.Number)
                .AddCell("Product A", DataType.String)
                .AddCell(10, DataType.Number)
                .AddCell(100.50, DataType.Currency)
                .AddCell(DateTime.Now, DataType.LongDate)
                .AddCell("John Doe", DataType.String)
                .AddCell(true, DataType.Boolean);

            // Add column titles
            inventory.AddRow()
                .AddCell("ItemId", DataType.String)
                .AddCell("Item", DataType.String)
                .AddCell("Number", DataType.String)
                .AddCell("Price", DataType.String);

            // Add data
            //foreach (var i in inventory)
            //{
            //	inventory.AddRow()
            //		.AddCell(i.ItemId, DataType.Number)
            //		.AddCell(i.Item, DataType.String)
            //		.AddCell(i.Number, DataType.Number)
            //		.AddCell(i.Price, DataType.Currency);
            //}

            // Optional - Format the tab color in Hexadecimal
            orders.TabColor = "#00FF00"; // Green
            inventory.TabColor = "#0000FF"; // Blue

            // Optional - Add column widths in inches or centimeters (Specify the unit of measure)
            orders.ColumnWidths(".8in", "1in", ".5in", "1in", "1.5in", "1in", "1in");
            inventory.ColumnWidths(".8in", "1in", ".8in", "1in");

            // Optional - Reformat the datatypes
            workbook.Format(DataType.Decimal, "decimals=8");
            workbook.Format(DataType.Currency, "symbol=$,language=en,country=US,decimals=2");

            // Optional - Add a worksheet to display data type conversion errors, only if they occur
            workbook.AddErrorsWorksheet();

            // Optional - Get the error manually
            workbook.GetErrors();

            // Synchronous GetBytes method
            workbook.GetBytes();

            // Asynchronous GetBytes method
            await workbook.GetBytesAsync();

            // Synchronous save method
            workbook.Save(path);

            // Asynchronous save method
            await workbook.SaveAsync(path);
        }
        #endregion === protected void Button_Export_ODS_by_FeeeDataExports_Click(object sender, EventArgs e) ===

        #region === protected void Button_Export_ODS_by_FeeeDataExports2_Click(object sender, EventArgs e) ===
        protected void Button_Export_ODS_by_FeeeDataExports2_Click(object sender, EventArgs e)
        {
            // https://github.com/ryankueter/FreeDataExports

            string path = @"C:\Temp\1.ods";
            var workbook = new DataExport().CreateODSv1_3();
            var orders = workbook.AddWorksheet("Orders");

            // Add column titles
            orders.AddRow()
                .AddCell("OrderId", DataType.String)
                .AddCell("Item", DataType.String)
                .AddCell("Units", DataType.String)
                .AddCell("Price", DataType.String)
                .AddCell("OrderDate", DataType.String)
                .AddCell("SalesAssoc", DataType.String)
                .AddCell("Delivered", DataType.String);

            orders.AddRow()
                .AddCell("12345", DataType.Number)
                .AddCell("Product A", DataType.String)
                .AddCell(10, DataType.Number)
                .AddCell(100.50, DataType.Currency)
                .AddCell(DateTime.Now, DataType.LongDate)
                .AddCell("John Doe", DataType.String)
                .AddCell(true, DataType.Boolean);

            workbook.Save(path);
                    string exportFilename = "匯出檔案名稱.ods";
                        // 設定回應的標頭資訊
                        Response.Clear();
                        Response.ContentType = "application/vnd.oasis.opendocument.spreadsheet";
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + exportFilename);
                        //Response.BinaryWrite(ms);
                        //Response.BinaryWrite(workbookBytes);
                        Response.TransmitFile(path);
                        Response.End();
} #endregion === protected void Button_Export_ODS_by_FeeeDataExports2_Click(object sender, EventArgs e) === } }

實際測試可以成功匯入。








(完)

相關

[研究]ASP.NET,WebForm,要傳遞 SqlDataSource1.SelectParameters 當時的值https://shaurong.blogspot.com/2024/03/aspnetwebform-sqldatasource1selectparam.html

[研究]ASP.NET,WebForm,用 FreeDataExports 1.1.9 匯出資料庫某資料表成 .ods
https://shaurong.blogspot.com/2024/03/aspnetwebform-freedataexports-119-ods_29.html

[研究]ASP.NET,WebForm,用 FreeDataExports 1.1.9 匯出 .ods
https://shaurong.blogspot.com/2024/03/aspnetwebform-freedataexports-119-ods.html

[研究]ASP.NET,WebForm, 把 OdsReadWrite.cs 從 Ionic.Zip 或 DotNetZip 改成用 System.IO.Compression
https://shaurong.blogspot.com/2024/03/aspnetwebform-odsreadwritecs-ioniczip.html

[研究]ASP.NET,WebForm,用 OdsReaderWriter + DotNetZip (Ionic.Zip) 1.16.0 共用匯出 .ods
https://shaurong.blogspot.com/2024/03/aspnetwebform-odsreaderwriter-dotnetzip_79.html

[研究]ASP.NET,WebForm,用 OdsReaderWriter + DotNetZip (Ionic.Zip) 1.16.0 匯出 .ods
https://shaurong.blogspot.com/2024/03/aspnetwebform-odsreaderwriter-dotnetzip_23.html

[研究]ASP.NET,WebForm,用 OdsReaderWriter + DotNetZip (Ionic.Zip) 1.16.0 匯入 .ods
https://shaurong.blogspot.com/2024/03/aspnetwebform-odsreaderwriter-dotnetzip_93.html

How to Read and Write ODF/ODS Files (OpenDocument Spreadsheets) - CodeProject
https://www.codeproject.com/Articles/38425/How-to-Read-and-Write-ODF-ODS-Files-OpenDocument-2

[研究][ASP.NET]讀取、匯出、寫入、匯入 Excel .xlsx .xls ODF .ods
https://shaurong.blogspot.com/2020/04/caspnetwebform-excel-xlsx-xls-odf-ods.html

[研究][ASP.NET]使用 OdsReaderWriter + DotNetZip 1.15.0 (Ionic.Zip) 匯出寫入 .ods
https://shaurong.blogspot.com/2021/02/aspnet-odsreaderwriter-dotnetzip-1150_9.html

[研究][ASP.NET]使用 OdsReaderWriter + DotNetZip 1.15.0 讀取匯入 .ods 到資料庫
https://shaurong.blogspot.com/2021/02/aspnet-odsreaderwriter-dotnetzip-1150.html

[研究][C#]用OdsReaderWrite匯出(寫入)、匯入(讀取) ODF/.ods 檔案 (使用 Ionic.Zip、DotNetZip)
https://shaurong.blogspot.com/2020/09/caspnetwebform-ioniczip-odfods.html

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

[研究] [C#] 用OdsReaderWrite讀取 LibreOffice 5.1.6 的試算表(Calc) 檔案 (.ods)(使用 DotNetZip)
https://shaurong.blogspot.com/2016/12/c-libreoffice-516-calc-ods.html

NuGet Gallery | OdsReaderWriter 1.0.10
https://www.nuget.org/packages/OdsReaderWriter

sortings/OdsReadWrite at master · Luviz/sortings · GitHub
https://github.com/Luviz/sortings/tree/master/OdsReadWrite

沒有留言:

張貼留言