2022年5月7日 星期六

[研究][ASP.NET]免費FreeDataExports 1.1.3匯出儲存.xlsx, .ods, .csv

[研究][ASP.NET]免費FreeDataExports 1.1.3匯出儲存.xlsx, .ods, .csv

2022-05-07

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

Supported file types:

Office Open XML Spreadsheet (.xlsx) v2019

Open Document File (ODF) Spreadsheet (.ods) v1.3

Comma-separated Values (.csv)

https://www.nuget.org/packages/FreeDataExports

https://github.com/ryankueter/FreeDataExports

FreeDataExports.1.1.3 並沒有相依套件。


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

Default.aspx

<%@ Page 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="儲存成.xlsx" OnClick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="儲存成.ods" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="儲存成.csv" OnClick="Button3_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)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string path = @"C:\Temp\1.xlsx";
            // Create a new workbook
            var workbook = new DataExport().CreateXLSX2019();

            // 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
            orders.AddRow()
                .AddCell(1, DataType.Number)
                .AddCell("2", DataType.String)
                .AddCell(3, DataType.Number)
                .AddCell(4, DataType.Currency)
                .AddCell("2022-01-01", DataType.LongDate)
                .AddCell("5", DataType.String)
                .AddCell(true, DataType.Boolean);

            //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 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 - Add a tab color in RGB
            orders.TabColor = "0000FF00"; // Green
            inventory.TabColor = "000000FF"; // Blue

            // Optional - Add column widths, based on character widths
            orders.ColumnWidths("10", "10", "5", "10", "28.5", "15", "10");
            inventory.ColumnWidths("10", "10", "10", "10");

            // Optional - Reformat a data type
            workbook.Format(DataType.DateTime24, @"m/d/yy\ h:mm;@");

            // 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();
            workbook.GetBytesAsync();

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

            // Asynchronous save method
            //await workbook.SaveAsync(path);
            workbook.SaveAsync(path);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            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
            orders.AddRow()
                .AddCell(1, DataType.Number)
                .AddCell("2", DataType.String)
                .AddCell(3, DataType.Number)
                .AddCell(4, DataType.Currency)
                .AddCell("2022-01-01", DataType.LongDate)
                .AddCell("5", DataType.String)
                .AddCell(true, DataType.Boolean);
            //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 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();
            workbook.GetBytesAsync();

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

            // Asynchronous save method
            //await workbook.SaveAsync(path);
            workbook.SaveAsync(path);
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string path = @"C:\Temp\1.csv";
            // Create a new csv file
            var csv = new DataExport().CreateCsv();

            // Add a header row
            csv.AddRow("OrderId", "Item", "Units", "Price", "OrderDate", "SalesAssoc", "Delivered");

            // Add some data
            csv.AddRow("1", "2", "3", "4", "2022-01-01", "5", "6");

            //foreach (var o in Orders)
            //{
            //	csv.AddRow(o.OrderId, o.Item, o.Units, o.Price, o.OrderDate, o.SalesAssociate, o.Delivered);
            //}

            // Synchronous GetBytes method
            csv.GetBytes();

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

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

            // Asynchronous save method
            //await csv.SaveAsync(path);
            csv.SaveAsync(path);
        }
    }
}

執行結果





(完)

相關

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

[研究][ASP.NET]使用 ClosedXML 0.95.4 匯出、寫入 .xlsx
https://shaurong.blogspot.com/2021/02/aspnet-closedxml-0954-xlsx_17.html

沒有留言:

張貼留言