顯示具有 ODF 標籤的文章。 顯示所有文章
顯示具有 ODF 標籤的文章。 顯示所有文章

2024年10月29日 星期二

[研究]ODF 1.4 (Open Document Format 1.4) 草案現況

[研究]ODF 1.4 (Open Document Format 1.4) 草案現況

2024-10-29

OpenDocument standardization
https://en.wikipedia.org/wiki/OpenDocument_standardization#OpenDocument_1.4

OASIS
https://en.wikipedia.org/wiki/OASIS_(organization)

OpenDocument
https://en.wikipedia.org/wiki/OpenDocument

  • Microsoft Office 2007 (with Service Pack 2 or 3) supports ODF 1.1[45] (Windows only)
  • Microsoft Office 2010 supports ODF 1.1 (Windows only)
  • Microsoft Office 2013 supports ODF 1.2 (Windows only)
  • Microsoft Office 2016 and 2019 support ODF 1.2 (Windows: read/write; OS X: read-only after online conversion[46])
  • Microsoft Office 2021 supports ODF 1.3[47] (Windows and MacOS)


Microsoft 365 apps now support OpenDocument Format 1.4
https://techcommunity.microsoft.com/t5/microsoft-365-insider-blog/microsoft-365-apps-now-support-opendocument-format-1-4/ba-p/4226735
Office 365 支援 ODF 1.4

What's new in Word 2024 for Windows and Mac
https://support.microsoft.com/en-us/office/what-s-new-in-word-2024-for-windows-and-mac-9fe36ecd-820b-4f42-a9a2-0861f7615954
Office 2024 支援 ODF 1.4

[MS-OODF14]: Office Implementation Information for ODF 1.4 Standards Support
https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oodf14/ad5a09c6-609d-47b3-bc56-7ab49a8d4f35

[ODF1.4] OASIS, "Open Document Format for Office Applications (OpenDocument) Version 1.4", https://go.microsoft.com/fwlink/?linkid=2255466

ODF GitHub
https://github.com/oasis-tcs/odf-tc/tree/master/docs
https://docs.oasis-open.org/office/OpenDocument/v1.4/csd01/

  • 2024-03-18
  • Updating CSD-01 from WD-03
  • CSD-01(Committee Specification Draft 01): 進入了更正式的標準化過程
  • WD-03(Working Draft 03)

ODF Specification Tooling
https://github.com/oasis-tcs/odf-tc

(待續)

[研究]ASP.NET,WebForm,免ODS套件寫入匯出.ods

[研究]ASP.NET,WebForm,免ODS套件寫入匯出.ods

2024-10-29

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

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

Default.aspx

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

<!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">

    </form>
</body>
</html>



Default.aspx.cs

using System;
using System.IO;
using System.IO.Compression;

namespace WebApplication1
{
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GenerateOdsFile();
        }
        private void GenerateOdsFile()
        {
            using (MemoryStream odsStream = new MemoryStream())
            {
                using (ZipArchive zip = new ZipArchive(odsStream, ZipArchiveMode.Create, true))
                {
                    // 添加 mimetype 檔案
                    ZipArchiveEntry mimeEntry = zip.CreateEntry("mimetype", CompressionLevel.NoCompression);
                    using (StreamWriter writer = new StreamWriter(mimeEntry.Open()))
                    {
                        writer.Write("application/vnd.oasis.opendocument.spreadsheet");
                    }

                    // 添加必要的 META-INF/manifest.xml 文件
                    ZipArchiveEntry manifestEntry = zip.CreateEntry("META-INF/manifest.xml", CompressionLevel.Fastest);
                    using (StreamWriter writer = new StreamWriter(manifestEntry.Open()))
                    {
                        writer.Write(
                            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                            "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\">\n" +
                            "  <manifest:file-entry manifest:full-path=\"/\" manifest:media-type=\"application/vnd.oasis.opendocument.spreadsheet\"/>\n" +
                            "  <manifest:file-entry manifest:full-path=\"content.xml\" manifest:media-type=\"text/xml\"/>\n" +
                            "</manifest:manifest>"
                        );
                    }

                    // 添加 content.xml 文件
                    ZipArchiveEntry contentEntry = zip.CreateEntry("content.xml", CompressionLevel.Fastest);
                    using (StreamWriter writer = new StreamWriter(contentEntry.Open()))
                    {
                        writer.Write(
                            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                            "<office:document-content xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" " +
                            "xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\" " +
                            "xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\" " +
                            "xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\" " +
                            "office:version=\"1.2\">\n" +
                            "<office:body><office:spreadsheet><table:table table:name=\"Sheet1\" table:style-name=\"ta1\">\n" +
                            "<table:table-column table:style-name=\"co1\" table:number-columns-repeated=\"2\"/>\n" +
                            "<table:table-row table:style-name=\"ro1\"><table:table-cell><text:p>資料1</text:p></table:table-cell>" +
                            "<table:table-cell><text:p>資料2</text:p></table:table-cell></table:table-row>\n" +
                            "<table:table-row table:style-name=\"ro1\"><table:table-cell><text:p>資料3</text:p></table:table-cell>" +
                            "<table:table-cell><text:p>資料4</text:p></table:table-cell></table:table-row>\n" +
                            "</table:table></office:spreadsheet></office:body></office:document-content>"
                        );
                    }
                }

                odsStream.Position = 0;
                Response.Clear();
                Response.ContentType = "application/vnd.oasis.opendocument.spreadsheet";
                Response.AddHeader("Content-Disposition", "attachment; filename=sample.ods");
                odsStream.CopyTo(Response.OutputStream);
                Response.Flush();
                Response.End();
            }
        }
        //---
    }
}







結果 LibreOffice 7.6 正常讀取,但 Excel 2021開啟有警告,修復後正常顯示。這是因為Excel 對 .ods 文件有較嚴格的要求,尤其是 XML 標籤和屬性的結構。敝人嘗試多次,暫時仍無法解決,或許可以一開始用 LibreOffice建立一個正常的 template.ods 給程式插入資料使用。

Default.aspx.cs

using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Xml.Linq;

namespace WebApplication1
{
    public partial class Default3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string odsFilePath = @"C:\Users\Administrator\Downloads\Template2.ods";
            string targetOdsPath = @"C:\Users\Administrator\Downloads\S2.ods";
            File.Copy(odsFilePath, targetOdsPath, overwrite: true);
            InsertDataToODS(targetOdsPath);
        }
        public void InsertDataToODS(string odsFilePath)
        {
            // 定義要插入的資料
            var data = new string[,]
            {
                { "A1", "B1" },
                { "A2", "B2" }
            };

            // 打開 .ods 檔案 (實際上是 Zip 檔案)
            using (var zip = ZipFile.Open(odsFilePath, ZipArchiveMode.Update))
            {
                // 尋找 content.xml
                var entry = zip.GetEntry("content.xml");
                if (entry == null)
                {
                    throw new FileNotFoundException("content.xml not found in the .ods file.");
                }

                // 讀取 content.xml 並載入 XML 文件
                XDocument doc;
                using (var stream = entry.Open())
                {
                    doc = XDocument.Load(stream);
                }

                // 尋找要插入資料的表格位置 (這裡假設是 Sheet1)
                XNamespace ns = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
                var sheet = doc.Descendants(ns + "table").FirstOrDefault();

                if (sheet != null)
                {
                    // 生成 2x2 表格的 Row 和 Cell
                    for (int i = 0; i < 2; i++)
                    {
                        var row = new XElement(ns + "table-row");
                        for (int j = 0; j < 2; j++)
                        {
                            var cell = new XElement(ns + "table-cell",
                                new XElement(ns + "p", data[i, j]));
                            row.Add(cell);
                        }
                        sheet.Add(row);
                    }
                }

                // 將修改後的 XML 內容重新寫入 content.xml
                using (var stream = entry.Open())
                {
                    stream.SetLength(0); // 清除原始內容
                    doc.Save(stream);
                }
            }
        }
        //---
    }
}

實際測試,還是有問題,在無 ODS 套件情況,想自己或靠 ChatGPT 的 Code 產出 Excel 2021 可以正常讀取的 .ods 並不容易,運氣好是警告,然後可以讀取,運氣不好是完全不能讀取。

(完)

相關

2021年2月18日 星期四

[研究][ASP.NET]讀取、匯出、寫入、匯入 Excel .xlsx .xls ODF .ods

[研究][ASP.NET]讀取、匯出、寫入、匯入 Excel .xlsx .xls ODF .ods

2020-04-09、2020-04-17、2020-04-28、2021-02-18、2023-01-15 更新

Visual Studio + ASP.NET + WebForm + C#

微軟官方並不建議在伺服器端使用直接存取 Excel 物件模型的方式來控制 Excel 檔案,除了上述的資源無法釋放的問題外,還有像是權限的問題,以及安全性問題等等,詳細的資料請參考:

Considerations for server-side Automation of Office - Microsoft Support
http://support.microsoft.com/default.aspx/kb/257757

在 Server 端控制 Excel 的難處 | Microsoft Learn
2014/06/12

Office 伺服器端自動化的考量因素 - Microsoft Support
https://support.microsoft.com/zh-tw/help/257757/considerations-for-server-side-automation-of-office

Considerations for server-side Automation of Office
https://support.microsoft.com/fi-fi/help/257757/considerations-for-server-side-automation-of-office

Excel 與舊版 Excel 搭配使用
Excel 97-2003 支援256欄寬、65536列高,超出此欄和列限制的儲存格資料將會遺失。
Excel 2007 開始,工作表大小是1048576列高。

Excel 的規格及限制 - Microsoft 365 Excel, Excel 2019, Excel 2016, Excel 2013, Excel 2010, Excel 2007

(下圖)實際測試 Excel 2016,超過 256 欄位資料儲存成 .xls 時候,會有警告。
65536列的限制在 .xls 檔案,不管 Excel 是甚麼版本。



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

匯出、寫入Excel (.xlsx) 

注意元件更新狀況,多年未更新或停止研發的建議別用。

[研究][ASP.NET]使用 OpenXML 2.12.2 匯出、寫入 .xlsx

[研究][ASP.NET]使用 ClosedXML 0.95.4 匯出、寫入 .xlsx

[研究][ASP.NET]使用 Aspose.Cells 21.2.0 匯出、寫入 .ods 或 .xlsx

[研究][C#][ASP.NET][WebForm] 用 ClosedXML 0.95.0 把 GridView1 匯出成 Excel (.xlsx)
https://shaurong.blogspot.com/2020/04/caspnetwebform-closedxml-0950-gridview1.html
ClosedXML 需要 Microsoft OpenXML SDK ( NuGet 上稱為 DocumentFormat.OpenXml )

[研究][C#][ASP.NET][WebForm] 使用 Microsoft OpenXML 2.10.1 (DocumentFormat.OpenXml) 把 GridView 匯出成 .xlsx
https://shaurong.blogspot.com/2020/04/caspnetwebform-openxml-2101.html

[研究][C#][ASP.NET] GridView 匯出成 Excel (.xlsx) (使用 Microsoft OpenXML SDK 2.5)
http://shaurong.blogspot.tw/2016/03/caspnet-gridview-excel-xlsx-openxml-sdk.html

[研究] [C#] 用 ClosedXML 在 Server端操作 Excel,安裝與試用 (NuGet安裝)
http://shaurong.blogspot.com/2017/11/c-closedxml-server-excel-nuget.html

EPPLus 待測
https://www.nuget.org/packages/EPPlus/
目前最新為  2020-04-07 的 v5.1.0 版
From version 5 EPPlus changes the licence model using a dual license, Polyform Non Commercial / Commercial license.

SpreadSheetLight 待測
https://www.nuget.org/packages/SpreadsheetLight/
http://spreadsheetlight.com/
目前最新 2020年11月的 3.5.0 版 ( 目前2021/02/09 )
需要 OpenXMLSDK 2.5

ExcelPackage 1.0.0
最後釋出 2015年5月22日 (2015/5/22),目前2021/02/09,已經多年未更新。

public static void WriteToExcel(string[] newInfoArray)
        {
            FileInfo fileInfo = new FileInfo(pathName);

            using (ExcelPackage packge = new ExcelPackage(fileInfo))
            {
                ExcelWorksheet worksheet = packge.Workbook.Worksheets[sheetName];

                for (int i = 0; i < newInfoArray.Length; ++i)
                {
                    worksheet.Cells[selectIndex + 2, i + 1].Value = newInfoArray[i];
                }

                packge.Save();
            }
        }
********************************************************************************

Excel (.xlsx) 匯出、匯入 注意事項

目前拿來匯入的 .xlsx 檔案,若為直接用 Microsoft Excel 建立的,不會有問題。用任何元件匯出產生的 .xlsx,不保證匯入成功;就算此時再拿 Microsoft Excel 另存過一次也不保證匯入成功。

而用任何元件產生的 .xlsx 檔案,用 Microsoft Excel 開啟,也不保證 100% 成功。

有可能是元件本身相容性問題 or Bug or 程式寫法問題。


[研究][C#][ASP.NET][WebForm] ClosedXML 匯入錯誤:參數名稱: The range 工作表1!A1:L2 is already part of table 'Table1'
https://shaurong.blogspot.com/2020/04/closedxml-range-1a1l2-is-already-part.html

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

匯入、讀取Excel (.xlsx) 


[研究][ASP.NET]使用 OpenXML SDK 2.12.1 讀取匯入 .xlsx 到資料庫
DocumentFormat.OpenXml 2.12.1

[研究][ASP.NET]使用 ClosedXML 0.95.4 讀取匯入 .xlsx 到資料庫

[研究][ASP.NET]使用 Aspose.Cells 21.1.0 讀取匯入 .xlsx 或 .ods 到資料庫

[研究][ASP.NET] 用 ExcelDataReader 讀取匯入 .xlsx , .xls , csv 檔案到 GridView1
https://shaurong.blogspot.com/2020/04/caspnetwebform-exceldatareader-xlsx-xls.html
Web Server 不需安裝 Office,目前最新為 2019/5/2 的 v3.6.0,支援 .csv, .xls, .xlsx 讀取,不支援寫入

[研究][ASP.NET]使用 NPOI 2.5.2 讀取匯入 .xlsx 到資料庫

[研究][ASP.NET]使用 ExcelDataReader 3.6.0 讀取匯入 .xlsx 到資料庫
https://shaurong.blogspot.com/2021/02/aspnet-exceldatareader-360-xlsx.html

[研究] [C#] [WinForm] 取得 Excel 的所有 工作表 名稱,及儲存格內容 (使用Access Database Engine 2010)
http://shaurong.blogspot.tw/2016/11/c-winform-excel.html

[研究] Excel 2016 (.xlsx) 匯入 SQL Server 2019
https://shaurong.blogspot.com/2019/12/excel-2016-xlsx-sql-server-2019.html

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

匯出 Excel (.xls)


[研究][C#][ASP.NET][WebForm] 使用 Microsoft OpenXML SDK 2.10.1 ( DocumentFormat.OpenXml ) 把 Excel .xlsx 匯入
https://shaurong.blogspot.com/2020/04/caspnetwebform-microsoft-openxml-sdk.html

[研究][C#][ASP.NET] GridView 匯出成 .xls (使用 Excel)
http://shaurong.blogspot.tw/2016/03/caspnet-gridview-xls-excel.html
電腦要安裝 Office 軟體

[研究][ASP.NET][C#]使用 NPOI 的 RenderDataTableToExcel 匯出資料成 Excel (.xls) 檔案
待測
Server電腦要不安裝 Office 軟體,NOPI 支援 xls, xlsx, docx.

[研究][C#][ASP.NET] 用 NPOI v2.2.1 在 Server 端存取 Excel 檔案 (從官方下載安裝)
http://shaurong.blogspot.com/2017/11/caspnet-npoi-v221-server-excel.html
NOPI 支援 xls, xlsx, docx.

[研究][C#][ASP.NET] 用 NPOI v2.3.0 在 Server 端存取 Excel 檔案 (NuGet安裝)
http://shaurong.blogspot.com/2017/11/caspnet-npoi-server-excel-nuget.html

[研究][C#][ASP.NET] GridView 匯出成 Excel (.xls)(以Html方式輸出)
http://shaurong.blogspot.tw/2016/03/caspnet-gridview-excel-xlshtml.html

EPPlus
https://github.com/JanKallman/EPPlus
EPPlus will from version 5 switch license from LGPL to Polyform Noncommercial 1.0.0 license.
With the new license EPPlus is still free to use in some cases, but will require a commercial license to be used in a commercial business.
EPPlus is a .NET library that reads and writes Excel files using the Office Open XML format (xlsx). EPPlus has no dependencies other than .NET.

EPPlus 待測

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

匯入 Excel (.xls) 

待測

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

開放文件格式 (Open Document Format, ODF)

開放文檔格式 - 維基百科,自由的百科全書
https://zh.wikipedia.org/wiki/%E5%BC%80%E6%94%BE%E6%96%87%E6%A1%A3%E6%A0%BC%E5%BC%8F

ODF 中相對 MS-Excel 的試算表檔案為 .ods (OpenDocument Spreadsheet)

Office 2010 只支援到 ODF 1.1 版,Excel 2010 開啟 LibreOffice Calc 試算表 .ods 內容會失敗,Office 2013 和 2016 才支援目前最新的 ODF 1.2版

Comparison of ODF software - The Document Foundation Wiki
ODF release dates:
ODF 1.0 May 2005
ODF 1.1 February 2007
ODF 1.2 September 2012
ODF 1.3 January 2020


How to Read and Write ODF/ODS Files (OpenDocument Spreadsheets)
28 Jul 2011
所以 odsreadwrite.zip 可能只支援到 ODF, ODS 1.1 版

Microsoft Office 2007(Service Pack 2或3)開始支援 ODF 1.1(開啟、編輯與儲存) 僅限Windows)
Microsoft Office 2013 開始支援 ODF 1.2 開啟、編輯與儲存;對 ODF 1.1僅開啟與編輯(僅限Windows)
Microsoft Office 2021 開始支援 ODF 1.3(僅限Windows)
https://support.microsoft.com/zh-tw/office/office-2021-%E7%9A%84%E6%96%B0%E5%8A%9F%E8%83%BD-43848c29-665d-4b1b-bc12-acd2bfb3910a

Comparison of ODF software - The Document Foundation Wiki
MS-Office 對 ODF 的支援並非100%
Libre Office 5.0 和 Apache OpenOffice 4.1 支援 ODF 1.0. 1.1, 1.2 

Differences between the OpenDocument Spreadsheet (.ods) format and the Excel for Windows (.xlsx) format - Microsoft Support
.ods 對 .xlsx 的支援也沒有100%,所以 .xlsx 另存為 .ods,會喪失部分資訊。

History - The Document Foundation Wiki
https://wiki.documentfoundation.org/History
  • OpenOffice.org 1.0 (May 2002) ~ 1.1.5 (Sep 2005)
  • OpenOffice.org 2.0 (Oct 2005) 支援 ODF ~ 2.4.3 (Sep 2009)
  • OpenOffice.org 3.0 (Oct 2008) ~ 3.3.0 (Jan 2011)
  • Apache OpenOffice 4.0 (Apr 2011)
LibreOffice - Wikipedia
  • LibreOffice 3.3.0 (January 25, 2011)  支援 ODF
  • LibreOffice 4.0 (February 2013)
  • LibreOffice 5.0 (August 2015)
  • LibreOffice 6.0 (31 January 2018) 支援 ODF 1.2
  • LibreOffice 7.0 (5 August 2020) 支援 ODF 1.3
Apache OpenOffice - Wikipedia
  • 3.4.0 2012-05-08,支援ODF
  • 3.4.1 2012-08-23
  • 4.0.0 2013-07-23,支援ODF 1.2
  • 4.0.1 2013-10-01
  • 4.1.0 2014-04-29
  • 4.1.1 2014-08-21
  • 4.1.2 2015-10-28
  • 4.1.3        2016-10-12
  • 4.1.4 2017-10-19
  • 4.1.5 2017-12-30
  • 4.1.6 2018-11-18
  • 4.1.7 2019-09-21
  • 4.1.9 2021-02-07,支援ODF 1.3
  • 4.1.10 2021-05-04
  • 4.1.11 2021-10-06

Libre Office 
LibreOffice_6.3.5_Win_x64_sdk.msi
https://zh-tw.libreoffice.org/download/libreoffice-still/

OpenOffice 4.1.7 SDK
https://openoffice.apache.org/downloads.html

微軟 Office 軟體對 ODF 的支援 FAQ
https://docs.microsoft.com/zh-tw/archive/blogs/officetw/office-odf-faq

[研究] C# .NET 存取 OpenOffice Calc 試算表 .ods 套件評估
http://shaurong.blogspot.com/2016/12/c-net-openoffice-calc-ods.html


Top 20 NuGet ods Packagesz

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

匯出、寫入ODF/.ODS

目前免費最佳做法是安裝 Java JRE  + LibreOffice + LibreOffice SDK,沒有免費好用元件,或付費買元件用。

[研究][ASP.NET]使用 Aspose.Cells 21.2.0 匯出、寫入 .ods 或 .xlsx
https://shaurong.blogspot.com/2021/02/aspnet-asposecells-2120-ods-xlsx.html
[研究][ASP.NET][C#] Aspose.Cells 評估版限制、訂價、授權、程式使用授權檔
試用版只能使用Aspose.Cells庫打開100個Excel文件。如果您的應用程序超出此數目,將引發異常。

[研究][ASP.NET]使用 OdsReaderWriter + DotNetZip 1.15.0 (Ionic.Zip) 匯出寫入 .ods

[研究][C#]匯出(寫入)、匯入(讀取) ODF/.ods 檔案 (使用 OdsReaderWriter、Ionic.Zip、DotNetZip)

[研究]Ionic.Zip 1.9.1.8 即將淘汰,替代套件 DotNetZip >= 1.9.1.8

[研究][C#][ASP.NET] GridView 匯出成 Excel / .ODF (.ods) (以Html方式輸出)
https://shaurong.blogspot.com/2020/04/caspnet-gridview-excel-odf-ods-html.html

[研究] [C#] 使用 LibreOffice 6.2.6 SDK、OpenOffice 4.1.6 SDK讀取/寫入試算表(Calc) 檔案 (.ods)
https://shaurong.blogspot.com/2019/08/c-libreoffice-626-sdk-calc-ods.html
Libre Office 或 Apache Open Office 當 .ods 的讀寫軟體就算了,它的 SDK 感覺不太穩;且
Apache OpenOffice 4.1.3 SDK (2016-10-12)  只支援 .NET 2,但不支援 .NET 4.x
LibreOffice 5.3.0 SDK (2017-01-30) 支援 .NET 4.x,但不支援 x64
.NET Framework 4.0 於 2010-04-12 釋出,兩份SDK似乎沒有很好支援.NET

NuGet 排名前面的是付費元件 (可能有免費天數限制或功能限制的版本)
https://www.nuget.org/packages?q=ODS

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

GleamTech.DocumentUltimate 是商業付費軟體,有全功能30天試用版

GroupDocs 是商業付費軟體,有全功能xx天試用版

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

匯入、讀取ODF/.ODS

目前免費最佳做法可能是安裝 Java JRE  + LibreOffice + LibreOffice SDK,或使用使用 .NET 4.5 的 ZipFile,沒有免費好用元件,或付費買元件用。

[研究][C#]匯出(寫入)、匯入(讀取) ODF/.ods 檔案 (使用 OdsReaderWriter、Ionic.Zip、DotNetZip)

[研究]Ionic.Zip 1.9.1.8 即將淘汰,替代套件 DotNetZip >= 1.9.1.8

[研究] [C#] 使用 LibreOffice 6.2.6 SDK、OpenOffice 4.1.6 SDK讀取/寫入試算表(Calc) 檔案 (.ods)
https://shaurong.blogspot.com/2019/08/c-libreoffice-626-sdk-calc-ods.html
這是 Windows Forms 程式

[研究] [C#] 使用 OpenOffice 4.1.3 SDK 讀取試算表(Calc) 檔案 (.ods)
http://shaurong.blogspot.com/2016/12/c-openoffice-413-sdk-calc-ods.html

[研究] [C#] 使用 LibreOffice 5.1.6、5.3.0 SDK 讀取試算表(Calc) 檔案 (.ods)
http://shaurong.blogspot.com/2016/12/c-libreoffice-516-sdk-calc-ods.html

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

[研究] [C#] 使用 Apose.Cells 讀取 LibreOffice 5.1.6、OpenOffice 4.1.3 試算表(Calc) 檔案 (.ods)
http://shaurong.blogspot.com/2017/01/c-aposecells-libreoffice-516openoffice.html
Apose.Cells 是商業付費元件,但有無限期的試用版,但是限制只能開啟 100 個檔案。
https://docs.aspose.com/display/cellsnet/Licensing#Licensing-EvaluationVersionLimitations

[研究] [C#] 使用 GemBox.Spreadsheet 讀取LibreOffice、OpenOffice試算表(Calc) 檔案 (.ods)
http://shaurong.blogspot.com/2016/12/c-gemboxspreadsheet-libreofficeopenoffi.html
這是商業付費軟體,雖然有30天試用版,但功能有限制
Maximum number of rows per sheet is 150.
Maximum number of sheets per workbook is 5.

[研究] OpenOffice 4.1.3 試算表(Calc) 保護鎖定儲存格不被修改
http://shaurong.blogspot.com/2016/12/openoffice-413-calc.html

[研究] 使用OpenOffice 4.1.3試算表(Calc)建立「下拉式選單」來輸入資料
http://shaurong.blogspot.com/2016/12/openoffice-413calc.html

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

匯出、寫入.CSV


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

相關文章 (其他)

[研究] 匯出、匯入 Excel 檔案 (.NET)
https://shaurong.blogspot.com/2017/11/excel-net.html

[研究] .ods、.csv 轉 .xlsx 檔案
https://shaurong.blogspot.com/2020/04/odscsv-xlsx.html

(待續)

相關文章

在 Server 端存取 Excel 檔案的利器:NPOI Library
https://msdn.microsoft.com/zh-tw/ee818993.aspx


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)