與其他組件的比較 IronXL 和 Epplus 的比較 Jordi Bardia 更新日期:8月 20, 2025 Download IronXL NuGet 下載 DLL 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 在本文中,我們探討 IronXL 和 EPPlus 軟件之間的相似性和差異。 對於大多數組織來說,Microsoft Excel 已被證明是一個非常有用的工具。 像數據庫這樣的 Excel 文件,將數據簡單地存儲在單元格中,便於管理需要存儲的數據。 .xls 和 .xlsx 文件格式也被 Excel 使用。 C# 語言可能使管理 Excel 文件變得困難。 然而,IronXL 和 EPPlus 軟件使得處理這些過程變得更加容易。 使用此軟件不需要 Microsoft Office。 請注意,您可以在不安裝 Microsoft Office 的情況下用 C# 讀取和創建 Excel 文件。今天,我們將看看一些易於實施的不同選擇。 class="hsg-featured-snippet"> 如何在 C# 中使用 EPPlus 讀取 Excel 文件 下載並安裝 EPPlus C# 庫 利用 ExcelPackage EPPlus C# 類來讀取 Excel 文件 根據工作表中的行和列位置讀取和修改數據 使用LoadFromDataTable方法快速填充 Excel 工作表 使用 EPPlus C# 導出 Excel 文件 EPPlus 軟件是什麼? EPPlus 是一個基於 NuGet 的 .NET Framework/.NET Core 庫,用於處理 Office Open XML 試算表。 版本 5 支持 .NET Framework 3.5 和 .NET Core 2.0。EPPlus 不依賴於任何其他庫,例如 Microsoft Excel。 EPPlus 有一個 API,可以讓您操作 Office Excel 文檔。 EPPlus 是一個 .NET 庫,可以讀取和寫入 Office OpenXML 格式的 Excel 文件。 此庫可作為 NuGet 的一個包使用。 這個庫是為程序員設計的。 目標一直是讓熟悉 Excel 或其他試算表庫的開發者能夠快速學習 API。 或者,正如某人所說,“使用 IntelliSense 來獲勝!” EPPlus 安裝 要從 Visual Studio 安裝 EPPlus,請轉到 查看 > 其他窗口 > 包管理器控制台,並輸入以下命令: Install-Package EPPlus 如果您願意使用 .NET CLI,請從提升的命令提示符或 PowerShell 提示符運行以下命令: dotnet add package EPPlus EPPlus 是一個可以添加到項目中的 dotnet 包。 什麼是 IronXL? IronXL 是一個簡單的 C# 和 VB Excel API,允許您在 .NET 中以極快的速度讀取、編輯和創建 Excel 試算表文件。 不需要安裝 Microsoft Office 甚至 Excel Interop。此庫也可用於處理 Excel 文件。 .NET Core、.NET Framework、Xamarin、移動、Linux、macOS 和 Azure 都受到 IronXL 的支持。 有多種不同的方法可以將數據讀寫到您的試算表中。 使用 NuGet 包添加 IronXL 我們可以通過三種方式將 IronXL 包添加到您的帳戶中,這樣您可以選擇最適合您的方法。 使用_package_manager_console安裝 IronXL 在項目中打開包管理器控制台使用以下命令: 要訪問包管理器控制台,請轉到 工具 => NuGet 包管理器 => 包管理器控制台。 class="content-img-align-center"> class="center-image-wrapper"> 這將帶您進入包管理器控制台。 然後,在包管理器終端輸入以下命令: Install-Package IronXL.Excel class="content-img-align-center"> class="center-image-wrapper"> 使用 NuGet 包管理器安裝 IronXL 這是獲得 NuGet 包管理器安裝的另一種方法。 如果您已經通過先前的方法完成了安裝,則無需使用此方法。 要訪問 NuGet 包管理器,請轉到工具 > NuGet 包管理器 => 選擇管理解決方案的 NuGet 包。 這將啟動 NuGet 解決方案; 選擇“瀏覽”並尋找 IronXL。 在搜索欄中輸入 Excel: class="content-img-align-center"> class="center-image-wrapper"> 當您單擊“安裝”按鈕時,IronXL 將為您安裝。 安裝 IronXL 後,您可以進入您的表單並開始開發它。 使用 IronXL 創建 Excel 文件 使用 IronXL 創建新的 Excel 工作簿再簡單不過了! 這只需要一行代碼! 是的,這是真的: // Create a new Excel workbook in XLSX format WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX); // Create a new Excel workbook in XLSX format WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX); ' Create a new Excel workbook in XLSX format Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX) $vbLabelText $csharpLabel IronXL 可以創建 XLS(以前的 Excel 文件版本)和 XLSX(當前和較新的 Excel 文件版本)格式的文件。 設置默認工作表 設置默認工作表甚至更容易: // Create a worksheet named "2020 Budget" in the workbook var sheet = workbook.CreateWorkSheet("2020 Budget"); // Create a worksheet named "2020 Budget" in the workbook var sheet = workbook.CreateWorkSheet("2020 Budget"); ' Create a worksheet named "2020 Budget" in the workbook Dim sheet = workbook.CreateWorkSheet("2020 Budget") $vbLabelText $csharpLabel 工作表在以上代碼片段中由sheet表示,您可以用它來設置單元格值並完成 Excel 幾乎能夠做的所有事情。 您還可以將 Excel 文檔編碼為只讀文件並執行刪除操作。 您也可以像 Excel 一樣連接您的工作表。 讓我澄清一下工作簿和工作表之間的區別,以防您不確定。 工作表被包含在一本工作簿中。 這意味著您可以在工作簿中放置任意多的工作表。 我會在之後的文章中解釋如何做到這一點。 工作表由行和列組成。 行和列的交叉是所謂的單元格,它是您在 Excel 中將要互動的地方。 使用 EPPlus Software AB 創建 Excel 文件 可以使用 EPPlus 創建 Excel 文件並執行諸如創建樞紐表、樞紐區域、甚至條件格式化和更改字體等操作。 事不宜遲,以下是將普通 DataTable 轉換為 XLSX Excel 文件並發送給用戶以下載的整個源代碼: public ActionResult ConvertToXLSX() { byte[] fileData = null; // Replace the GetDataTable() method with your DBMS-fetching code. using (DataTable dt = GetDataTable()) { // Create an empty spreadsheet using (var p = new ExcelPackage()) { // Add a worksheet to the spreadsheet ExcelWorksheet ws = p.Workbook.Worksheets.Add(dt.TableName); // Initialize rows and columns counter: note that they are 1-based! var row = 1; var col = 1; // Create the column names on the first line. // In this sample, we'll just use the DataTable column names row = 1; col = 0; foreach (DataColumn dc in dt.Columns) { col++; ws.SetValue(row, col, dc.ColumnName); } // Insert the DataTable rows into the XLS file foreach (DataRow r in dt.Rows) { row++; col = 0; foreach (DataColumn dc in dt.Columns) { col++; ws.SetValue(row, col, r[dc].ToString()); } // Alternate light-gray color for uneven rows (3, 5, 7, 9)... if (row % 2 != 0) { ws.Row(row).Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; ws.Row(row).Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray); } } // Output the XLSX file using (var ms = new MemoryStream()) { p.SaveAs(ms); ms.Seek(0, SeekOrigin.Begin); fileData = ms.ToArray(); } } } string fileName = "ConvertedFile.xlsx"; string contentType = System.Web.MimeMapping.GetMimeMapping(fileName); Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", fileName)); return File(fileData, contentType); } public ActionResult ConvertToXLSX() { byte[] fileData = null; // Replace the GetDataTable() method with your DBMS-fetching code. using (DataTable dt = GetDataTable()) { // Create an empty spreadsheet using (var p = new ExcelPackage()) { // Add a worksheet to the spreadsheet ExcelWorksheet ws = p.Workbook.Worksheets.Add(dt.TableName); // Initialize rows and columns counter: note that they are 1-based! var row = 1; var col = 1; // Create the column names on the first line. // In this sample, we'll just use the DataTable column names row = 1; col = 0; foreach (DataColumn dc in dt.Columns) { col++; ws.SetValue(row, col, dc.ColumnName); } // Insert the DataTable rows into the XLS file foreach (DataRow r in dt.Rows) { row++; col = 0; foreach (DataColumn dc in dt.Columns) { col++; ws.SetValue(row, col, r[dc].ToString()); } // Alternate light-gray color for uneven rows (3, 5, 7, 9)... if (row % 2 != 0) { ws.Row(row).Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; ws.Row(row).Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray); } } // Output the XLSX file using (var ms = new MemoryStream()) { p.SaveAs(ms); ms.Seek(0, SeekOrigin.Begin); fileData = ms.ToArray(); } } } string fileName = "ConvertedFile.xlsx"; string contentType = System.Web.MimeMapping.GetMimeMapping(fileName); Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", fileName)); return File(fileData, contentType); } Public Function ConvertToXLSX() As ActionResult Dim fileData() As Byte = Nothing ' Replace the GetDataTable() method with your DBMS-fetching code. Using dt As DataTable = GetDataTable() ' Create an empty spreadsheet Using p = New ExcelPackage() ' Add a worksheet to the spreadsheet Dim ws As ExcelWorksheet = p.Workbook.Worksheets.Add(dt.TableName) ' Initialize rows and columns counter: note that they are 1-based! Dim row = 1 Dim col = 1 ' Create the column names on the first line. ' In this sample, we'll just use the DataTable column names row = 1 col = 0 For Each dc As DataColumn In dt.Columns col += 1 ws.SetValue(row, col, dc.ColumnName) Next dc ' Insert the DataTable rows into the XLS file For Each r As DataRow In dt.Rows row += 1 col = 0 For Each dc As DataColumn In dt.Columns col += 1 ws.SetValue(row, col, r(dc).ToString()) Next dc ' Alternate light-gray color for uneven rows (3, 5, 7, 9)... If row Mod 2 <> 0 Then ws.Row(row).Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid ws.Row(row).Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray) End If Next r ' Output the XLSX file Using ms = New MemoryStream() p.SaveAs(ms) ms.Seek(0, SeekOrigin.Begin) fileData = ms.ToArray() End Using End Using End Using Dim fileName As String = "ConvertedFile.xlsx" Dim contentType As String = System.Web.MimeMapping.GetMimeMapping(fileName) Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", fileName)) Return File(fileData, contentType) End Function $vbLabelText $csharpLabel 如您所見,這是可以在任何 ASP.NET MVC 控制器上使用的 ActionResult 方法; 如果您不使用 ASP.NET MVC,只需複製方法內容並將其粘貼到需要的地方(例如,經典的 ASP.NET、控制台應用程序、Windows 窗體等)。 代碼本身包含足夠的注釋來幫助您理解各種處理過程。 但首先,讓我們快速回顧一下我們在這裡做的事情: 使用自定義數據提供程序方法,我們獲得 DataTable 對象。 我們建立了一個 ExcelPackage 對象,這是 EPPlus 的 Xlsx 文件的主要容器。 我們向ExcelPackage添加了一個ExcelWorksheet,這將是輸入數據的一份工作表。 為了創建表頭行,我們遍歷 DataTable 列,將它們添加到我們工作表的第一行中。 我們遍歷 DataTable 行,逐行將每一行添加到我們的工作表中(從第 2 行開始),以便每個 DataTable 行都對應工作表行。 我們建立了一個 MemoryStream 來儲存 ExcelPackage 的二進制數據,然後在完成 DataTable 到 ExcelPackage 的轉換後,將其轉換為字節數組。 我們創建 HTML 回答並將 XLSX 文件發送給用戶,通過 Content-Disposition 附件自動使瀏覽器下載文件。 IronXL 在這種情況下獲勝,因為創建過程非常簡單 — 您只需要一行代碼就能完成; 這幫助節省時間和調試,而 EPPlus 則提供了一些令人乏味的代碼行,難以調試。 EPPlus Software AB 如何寫入 Excel 文件 EPPlus 支持處理 Excel 文件。 它是一個 .NET 庫,用來讀取和寫入 Excel 文件。 讀取 Excel 文件 要這樣做,您需要首先安裝 EPPlus 包:轉到 「工具」-> 「NuGet 包管理器」-> 「管理此解決方案的 NuGet」-> 「安裝 EPPlus」-> 「安裝 EPPlus」-> 「安裝 EPPlus」-> 「安裝 EPPlus」-> 「安裝 EPPlus」-> 「安裝 EPPlus」。 在「瀏覽」 標籤中搜索“ EPPlus”,然後安裝 NuGet 包。 class="content-img-align-center"> class="center-image-wrapper"> 安裝包後,您可以在控制台應用程序「Program.cs」中使用以下代碼。 using OfficeOpenXml; using System; using System.IO; namespace ReadExcelInCsharp { class Program { static void Main(string[] args) { // Provide file path FileInfo existingFile = new FileInfo(@"D:\sample_XLSX.xlsx"); // Use EPPlus using (ExcelPackage package = new ExcelPackage(existingFile)) { // Get the first worksheet in the workbook ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; int colCount = worksheet.Dimension.End.Column; // Get Column Count int rowCount = worksheet.Dimension.End.Row; // Get row count for (int row = 1; row <= rowCount; row++) { for (int col = 1; col <= colCount; col++) { // Print data, based on row and columns position Console.WriteLine("Row:" + row + " Column:" + col + " Value:" + worksheet.Cells[row, col].Value?.ToString().Trim()); } } } } } } using OfficeOpenXml; using System; using System.IO; namespace ReadExcelInCsharp { class Program { static void Main(string[] args) { // Provide file path FileInfo existingFile = new FileInfo(@"D:\sample_XLSX.xlsx"); // Use EPPlus using (ExcelPackage package = new ExcelPackage(existingFile)) { // Get the first worksheet in the workbook ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; int colCount = worksheet.Dimension.End.Column; // Get Column Count int rowCount = worksheet.Dimension.End.Row; // Get row count for (int row = 1; row <= rowCount; row++) { for (int col = 1; col <= colCount; col++) { // Print data, based on row and columns position Console.WriteLine("Row:" + row + " Column:" + col + " Value:" + worksheet.Cells[row, col].Value?.ToString().Trim()); } } } } } } Imports OfficeOpenXml Imports System Imports System.IO Namespace ReadExcelInCsharp Friend Class Program Shared Sub Main(ByVal args() As String) ' Provide file path Dim existingFile As New FileInfo("D:\sample_XLSX.xlsx") ' Use EPPlus Using package As New ExcelPackage(existingFile) ' Get the first worksheet in the workbook Dim worksheet As ExcelWorksheet = package.Workbook.Worksheets(1) Dim colCount As Integer = worksheet.Dimension.End.Column ' Get Column Count Dim rowCount As Integer = worksheet.Dimension.End.Row ' Get row count For row As Integer = 1 To rowCount For col As Integer = 1 To colCount ' Print data, based on row and columns position Console.WriteLine("Row:" & row & " Column:" & col & " Value:" & worksheet.Cells(row, col).Value?.ToString().Trim()) Next col Next row End Using End Sub End Class End Namespace $vbLabelText $csharpLabel 下面是一個控制台應用程序的輸出示例,帶有一個我們正在處理的 Excel 文件 (.xlsx) 樣本。 這裡是一個用於在 C# 中使用 EPPlus 讀取的 xlsx 文件。 class="content-img-align-center"> class="center-image-wrapper"> 下面的方法可以使用“cells”屬性(ExcelRange)加載多個源的數據: 讀取 CSV 文本文件 並使用LoadFromText 和 LoadFromTextAsync 將數據加載到工作表中。 LoadFromDataReaderAsync 和 LoadFromDataReader — 將數據字段從 DataReader 加載到範圍中。 LoadFrom- 用 SaveToText 和 SaveToTextAsync 將範圍寫入 CSV 文件**。 — 從 DataTable 加載數據到範圍中。 它可以從包括 XML(提供了示例)和數據庫在內的多種源中導入數據。 反射性地從一個 IEnumerable 中載入數據到一個範圍中。 使用屬性 LoadFromCollection — 反射性地從 IEnumerable 中載入數據到一個範圍或表中。 用屬性指定樣式、數字格式、公式和其他屬性。 反射性地從一個 IEnumerable 中載入數據到一個範圍中。 屬性指定樣式、數字格式、公式以及其他屬性。 LoadFromDictionaries — 從 IEnumerable 中的 ExpandoObject/dynamic 對象字典(通過 IDictionary<string, object> 介面)載入數據到一個範圍中。 這對於導入 JSON 數據非常有用,並包括一個示例。 - 反射性地從一個 IEnumerable 中讀取數據並加載數據到一個範圍中,每個對象數組對應於工作表中的一行。 使用這些方法時,可以選擇傳遞一個參數以生成 Excel 表。 範例 4 和 5 的 Sample-.NET Framework 或 Sample-.NET Framework 包含了更多的示例。 寫入 Excel 文件 List<UserDetails> persons = new List<UserDetails>() { new UserDetails() {ID="9999", Name="ABCD", City ="City1", Country="USA"}, new UserDetails() {ID="8888", Name="PQRS", City ="City2", Country="INDIA"}, new UserDetails() {ID="7777", Name="XYZZ", City ="City3", Country="CHINA"}, new UserDetails() {ID="6666", Name="LMNO", City ="City4", Country="UK"}, }; List<UserDetails> persons = new List<UserDetails>() { new UserDetails() {ID="9999", Name="ABCD", City ="City1", Country="USA"}, new UserDetails() {ID="8888", Name="PQRS", City ="City2", Country="INDIA"}, new UserDetails() {ID="7777", Name="XYZZ", City ="City3", Country="CHINA"}, new UserDetails() {ID="6666", Name="LMNO", City ="City4", Country="UK"}, }; Dim persons As New List(Of UserDetails)() From { New UserDetails() With { .ID="9999", .Name="ABCD", .City ="City1", .Country="USA" }, New UserDetails() With { .ID="8888", .Name="PQRS", .City ="City2", .Country="INDIA" }, New UserDetails() With { .ID="7777", .Name="XYZZ", .City ="City3", .Country="CHINA" }, New UserDetails() With { .ID="6666", .Name="LMNO", .City ="City4", .Country="UK" } } $vbLabelText $csharpLabel 接下來,我們來看看是否可以將數據導出到新的 Excel 文件中。 這是我們想要作為 Excel 文檔保存的樣本數據/對象。 要創建一個包含基本信息的新 Excel 文件,我們需要利用 ExcelPackage 類。 將數據寫入文件並生成一個新的 Excel 試算表只需幾行代碼。 請注意以下一行,它完成了將 DataTables 加載到 Excel 表中的工作。 class="content-img-align-center"> class="center-image-wrapper"> private static void WriteToExcel(string path) { // Let use below test data for writing it to excel List<UserDetails> persons = new List<UserDetails>() { new UserDetails() {ID="9999", Name="ABCD", City ="City1", Country="USA"}, new UserDetails() {ID="8888", Name="PQRS", City ="City2", Country="INDIA"}, new UserDetails() {ID="7777", Name="XYZZ", City ="City3", Country="CHINA"}, new UserDetails() {ID="6666", Name="LMNO", City ="City4", Country="UK"}, }; // Let's convert our object data to Datatable for a simplified logic. // Datatable is the easiest way to deal with complex datatypes for easy reading and formatting. DataTable table = (DataTable)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(persons), (typeof(DataTable))); FileInfo filePath = new FileInfo(path); using (var excelPack = new ExcelPackage(filePath)) { var ws = excelPack.Workbook.Worksheets.Add("WriteTest"); ws.Cells.LoadFromDataTable(table, true, OfficeOpenXml.Table.TableStyles.Light8); excelPack.Save(); } } private static void WriteToExcel(string path) { // Let use below test data for writing it to excel List<UserDetails> persons = new List<UserDetails>() { new UserDetails() {ID="9999", Name="ABCD", City ="City1", Country="USA"}, new UserDetails() {ID="8888", Name="PQRS", City ="City2", Country="INDIA"}, new UserDetails() {ID="7777", Name="XYZZ", City ="City3", Country="CHINA"}, new UserDetails() {ID="6666", Name="LMNO", City ="City4", Country="UK"}, }; // Let's convert our object data to Datatable for a simplified logic. // Datatable is the easiest way to deal with complex datatypes for easy reading and formatting. DataTable table = (DataTable)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(persons), (typeof(DataTable))); FileInfo filePath = new FileInfo(path); using (var excelPack = new ExcelPackage(filePath)) { var ws = excelPack.Workbook.Worksheets.Add("WriteTest"); ws.Cells.LoadFromDataTable(table, true, OfficeOpenXml.Table.TableStyles.Light8); excelPack.Save(); } } Private Shared Sub WriteToExcel(ByVal path As String) ' Let use below test data for writing it to excel Dim persons As New List(Of UserDetails)() From { New UserDetails() With { .ID="9999", .Name="ABCD", .City ="City1", .Country="USA" }, New UserDetails() With { .ID="8888", .Name="PQRS", .City ="City2", .Country="INDIA" }, New UserDetails() With { .ID="7777", .Name="XYZZ", .City ="City3", .Country="CHINA" }, New UserDetails() With { .ID="6666", .Name="LMNO", .City ="City4", .Country="UK" } } ' Let's convert our object data to Datatable for a simplified logic. ' Datatable is the easiest way to deal with complex datatypes for easy reading and formatting. Dim table As DataTable = CType(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(persons), (GetType(DataTable))), DataTable) Dim filePath As New FileInfo(path) Using excelPack = New ExcelPackage(filePath) Dim ws = excelPack.Workbook.Worksheets.Add("WriteTest") ws.Cells.LoadFromDataTable(table, True, OfficeOpenXml.Table.TableStyles.Light8) excelPack.Save() End Using End Sub $vbLabelText $csharpLabel 為了簡化事情,我生成了一個新的試算表文件在同一個項目文件夾中(Excel 文件將生成在項目的 'bin' 文件夾中)。 源代碼如下: 根據前述的數據驗證 API 調用,會創建一個新的 Excel 文件,將上述自定義對象轉換為相應的 Excel 列和行顯示如下的值。 class="content-img-align-center"> class="center-image-wrapper"> 上述準備好的 API 可以在 .NET Core 控制台、一個測試項目或 ASP.NET Core 應用程序中使用,並且可以根據需要改變邏輯。 這些方法可以使用“cells”屬性(ExcelRange)訪問: ToText 和 ToTextAsync — 從一個範圍生成一個 CSV 字符串。 - 用 SaveToText 和 SaveToTextAsync 將範圍寫入 CSV 文件。 使用 ToDataTable 方法將數據從一個範圍導出到 System。 DataTable GetValue — 顯示具有數據類型選項的值。 - Value — 返回或設置範圍的值。 GetValue 和 SetValue 方法也可以直接用在工作表對象上。 (這將提供比在範圍上讀取/寫入稍好的結果): GetValue — 獲得單個單元格的值,帶有指定數據類型的選項。 SetValue — 更改單個單元格的值。 Linq 可以用來查詢工作表中的數據,因為 cell 屬性實現了 IEnumerable 接口。 用 IronXL 開啟和寫入 Office Open XML 格式 XLSX IronXL 是一個 NET 庫,允許 C# 開發人員快速輕鬆地處理 Excel、樞紐表和其他試算表文件。 不需要 Office Interop。 讀取 Excel 文件 不需要特定依賴或在 Core 或 Azure 上安裝 Microsoft Office。 IronXL 是一個著名的 C# 和 VB.NET xl 試算表庫用於 .NET core 和 .NET framework。 - 要加載的工作表 Excel 工作表由 WorkBook 類表示。 /** Load WorkBook **/ var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx"); /** Load WorkBook **/ var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx"); ''' ''' Load WorkBook ''' * Dim workbook = WorkBook.Load("Spreadsheets\\GDP.xlsx") $vbLabelText $csharpLabel 我們使用 WorkBook 打開 C# 中的 Excel 文件,其中包含甚至是樞紐表。 加載 Excel 文件並選擇其位置(.xlsx)。 WorkSheet 對象可以在多個 WorkBooks 中找到。 var worksheet = workbook.GetWorkSheet("GDPByCountry"); var worksheet = workbook.GetWorkSheet("GDPByCountry"); Dim worksheet = workbook.GetWorkSheet("GDPByCountry") $vbLabelText $csharpLabel 這些是 Excel 文檔的工作表。 如果工作表包含工作表,則使用名稱 WorkBook 借助 GetWorkSheet 找到它們。 /** Create WorkBook **/ var workbook = new WorkBook(ExcelFileFormat.XLSX); /** Create WorkBook **/ var workbook = new WorkBook(ExcelFileFormat.XLSX); ''' ''' Create WorkBook ''' * Dim workbook As New WorkBook(ExcelFileFormat.XLSX) $vbLabelText $csharpLabel 製作自己的工作簿。 用 WorkSheet 類型構造一個新 WorkBook 生成一個新的 WorkBook 在內存中。 對於別緻的 Microsoft Excel 試算表,使用 ExcelFileFormat.XLS(95 和更早版本)。 擁有一個工作表如果您還沒有一個。 在每個 "WorkBook" 中可以有多個 WorkSheets。 "WorkSheet" 是單個數據表,而 "WorkBook" 是一個 WorkSheets 的集合。 在 Excel 中,這是兩個工作表的一本工作簿的樣子。 var worksheet = workbook.CreateWorkSheet("Countries"); var worksheet = workbook.CreateWorkSheet("Countries"); Dim worksheet = workbook.CreateWorkSheet("Countries") $vbLabelText $csharpLabel class="content-img-align-center"> class="center-image-wrapper"> 您可以建立一個名為 WorkBook 的新 WorkSheet。 傳遞工作表的名稱給 CreateWorkSheet。 獲得單元格範圍 “Range” 類表示一個二維的“Cell”對象集合。 var range = worksheet["D2:D101"]; var range = worksheet["D2:D101"]; Dim range = worksheet("D2:D101") $vbLabelText $csharpLabel 表示 Excel 單元格的某個範圍。 在 WorkSheet 對象上使用字符串索引以檢索範圍。 參數文本可以是一個單元格坐標(例如,「A1」)或一個範圍內左至右頂至下的單元(例如,「B2:E5」)。 GetRange 也可以從 WorkSheet 調用。 - 在範圍內編輯單元格的值 可以以不同的方式讀取或編輯範圍內單元格的值。 /** Edit Cell Values in Range **/ // Iterate through the rows for (var y = 2; y <= 101; y++) { var result = new PersonValidationResult { Row = y }; results.Add(result); // Get all cells for the person var cells = worksheet[$"A{y}:E{y}"].ToList(); // Validate the phone number (1 = B) var phoneNumber = cells[1].Value; result.PhoneNumberErrorMessage = ValidatePhoneNumber(phoneNumberUtil, (string)phoneNumber); // Validate the email address (3 = D) result.EmailErrorMessage = ValidateEmailAddress((string)cells[3].Value); // Get the raw date in the format of Month Day [suffix], Year (4 = E) var rawDate = (string) cells[4].Value; result.DateErrorMessage = ValidateDate(rawDate); } /** Edit Cell Values in Range **/ // Iterate through the rows for (var y = 2; y <= 101; y++) { var result = new PersonValidationResult { Row = y }; results.Add(result); // Get all cells for the person var cells = worksheet[$"A{y}:E{y}"].ToList(); // Validate the phone number (1 = B) var phoneNumber = cells[1].Value; result.PhoneNumberErrorMessage = ValidatePhoneNumber(phoneNumberUtil, (string)phoneNumber); // Validate the email address (3 = D) result.EmailErrorMessage = ValidateEmailAddress((string)cells[3].Value); // Get the raw date in the format of Month Day [suffix], Year (4 = E) var rawDate = (string) cells[4].Value; result.DateErrorMessage = ValidateDate(rawDate); } ''' ''' Edit Cell Values in Range ''' * ' Iterate through the rows For y = 2 To 101 Dim result = New PersonValidationResult With {.Row = y} results.Add(result) ' Get all cells for the person Dim cells = worksheet($"A{y}:E{y}").ToList() ' Validate the phone number (1 = B) Dim phoneNumber = cells(1).Value result.PhoneNumberErrorMessage = ValidatePhoneNumber(phoneNumberUtil, CStr(phoneNumber)) ' Validate the email address (3 = D) result.EmailErrorMessage = ValidateEmailAddress(CStr(cells(3).Value)) ' Get the raw date in the format of Month Day [suffix], Year (4 = E) Dim rawDate = CStr(cells(4).Value) result.DateErrorMessage = ValidateDate(rawDate) Next y $vbLabelText $csharpLabel 如果已知計數,請使用 For 循環。 您還可以從這裡進行單元格樣式設計。 在試算表中驗證數據 /** Validate Spreadsheet Data **/ // Iterate through the rows for (var i = 2; i <= 101; i++) { var result = new PersonValidationResult { Row = i }; results.Add(result); // Get all cells for the person var cells = worksheet[$"A{i}:E{i}"].ToList(); // Validate the phone number (1 = B) var phoneNumber = cells[1].Value; result.PhoneNumberErrorMessage = ValidatePhoneNumber(phoneNumberUtil, (string)phoneNumber); // Validate the email address (3 = D) result.EmailErrorMessage = ValidateEmailAddress((string)cells[3].Value); // Get the raw date in the format of Month Day [suffix], Year (4 = E) var rawDate = (string)cells[4].Value; result.DateErrorMessage = ValidateDate(rawDate); } /** Validate Spreadsheet Data **/ // Iterate through the rows for (var i = 2; i <= 101; i++) { var result = new PersonValidationResult { Row = i }; results.Add(result); // Get all cells for the person var cells = worksheet[$"A{i}:E{i}"].ToList(); // Validate the phone number (1 = B) var phoneNumber = cells[1].Value; result.PhoneNumberErrorMessage = ValidatePhoneNumber(phoneNumberUtil, (string)phoneNumber); // Validate the email address (3 = D) result.EmailErrorMessage = ValidateEmailAddress((string)cells[3].Value); // Get the raw date in the format of Month Day [suffix], Year (4 = E) var rawDate = (string)cells[4].Value; result.DateErrorMessage = ValidateDate(rawDate); } ''' ''' Validate Spreadsheet Data ''' * ' Iterate through the rows For i = 2 To 101 Dim result = New PersonValidationResult With {.Row = i} results.Add(result) ' Get all cells for the person Dim cells = worksheet($"A{i}:E{i}").ToList() ' Validate the phone number (1 = B) Dim phoneNumber = cells(1).Value result.PhoneNumberErrorMessage = ValidatePhoneNumber(phoneNumberUtil, CStr(phoneNumber)) ' Validate the email address (3 = D) result.EmailErrorMessage = ValidateEmailAddress(CStr(cells(3).Value)) ' Get the raw date in the format of Month Day [suffix], Year (4 = E) Dim rawDate = CStr(cells(4).Value) result.DateErrorMessage = ValidateDate(rawDate) Next i $vbLabelText $csharpLabel 為了驗證一份數據表,請使用 IronXL。 DataValidation 範例使用 libphonenumber-CSharp 驗證電話號碼,並使用常規 C# API 驗證電郵地址和日期。 var resultsSheet = workbook.CreateWorkSheet("Results"); resultsSheet["A1"].Value = "Row"; resultsSheet["B1"].Value = "Valid"; resultsSheet["C1"].Value = "Phone Error"; resultsSheet["D1"].Value = "Email Error"; resultsSheet["E1"].Value = "Date Error"; for (var i = 0; i < results.Count; i++) { var result = results[i]; resultsSheet[$"A{i + 2}"].Value = result.Row; resultsSheet[$"B{i + 2}"].Value = result.IsValid ? "Yes" : "No"; resultsSheet[$"C{i + 2}"].Value = result.PhoneNumberErrorMessage; resultsSheet[$"D{i + 2}"].Value = result.EmailErrorMessage; resultsSheet[$"E{i + 2}"].Value = result.DateErrorMessage; } workbook.SaveAs(@"Spreadsheets\\PeopleValidated.xlsx"); var resultsSheet = workbook.CreateWorkSheet("Results"); resultsSheet["A1"].Value = "Row"; resultsSheet["B1"].Value = "Valid"; resultsSheet["C1"].Value = "Phone Error"; resultsSheet["D1"].Value = "Email Error"; resultsSheet["E1"].Value = "Date Error"; for (var i = 0; i < results.Count; i++) { var result = results[i]; resultsSheet[$"A{i + 2}"].Value = result.Row; resultsSheet[$"B{i + 2}"].Value = result.IsValid ? "Yes" : "No"; resultsSheet[$"C{i + 2}"].Value = result.PhoneNumberErrorMessage; resultsSheet[$"D{i + 2}"].Value = result.EmailErrorMessage; resultsSheet[$"E{i + 2}"].Value = result.DateErrorMessage; } workbook.SaveAs(@"Spreadsheets\\PeopleValidated.xlsx"); Dim resultsSheet = workbook.CreateWorkSheet("Results") resultsSheet("A1").Value = "Row" resultsSheet("B1").Value = "Valid" resultsSheet("C1").Value = "Phone Error" resultsSheet("D1").Value = "Email Error" resultsSheet("E1").Value = "Date Error" For i = 0 To results.Count - 1 Dim result = results(i) resultsSheet($"A{i + 2}").Value = result.Row resultsSheet($"B{i + 2}").Value = If(result.IsValid, "Yes", "No") resultsSheet($"C{i + 2}").Value = result.PhoneNumberErrorMessage resultsSheet($"D{i + 2}").Value = result.EmailErrorMessage resultsSheet($"E{i + 2}").Value = result.DateErrorMessage Next i workbook.SaveAs("Spreadsheets\\PeopleValidated.xlsx") $vbLabelText $csharpLabel 上述代碼循環遍歷試算表的行,將單元格作為列表抓取。 每個驗證方法驗證一個單元格的值,並在值不正確時返回錯誤。 這段代碼創建了一個新的工作表,指定了表頭並生成了錯誤信息結果,以便可以保留一個錯誤數據日志。 使用實體框架導出數據 使用 IronXL 將 Excel 試算表轉換為數據庫或導出數據到數據庫。 ExcelToDB 範例讀取包含國家 GDP 的試算表並將它導出到 SQLite。 它使用 EntityFramework 創建數據庫,然後逐行導出數據。 應安裝 SQLite Entity Framework NuGet 包。 public class Country { [Key] public Guid Key { get; set; } public string Name { get; set; } public decimal GDP { get; set; } } public class Country { [Key] public Guid Key { get; set; } public string Name { get; set; } public decimal GDP { get; set; } } Public Class Country <Key> Public Property Key() As Guid Public Property Name() As String Public Property GDP() As Decimal End Class $vbLabelText $csharpLabel class="content-img-align-center"> class="center-image-wrapper"> /** Export Data using Entity Framework **/ public class CountryContext : DbContext { public DbSet<Country> Countries { get; set; } public CountryContext() { // TODO: Make async Database.EnsureCreated(); } /// <summary> /// Configure context to use Sqlite /// </summary> /// <param name="optionsBuilder"></param> protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var connection = new SqliteConnection($"Data Source=Country.db"); connection.Open(); var command = connection.CreateCommand(); // Create the database if it doesn't already exist command.CommandText = $"PRAGMA foreign_keys = ON;"; command.ExecuteNonQuery(); optionsBuilder.UseSqlite(connection); base.OnConfiguring(optionsBuilder); } } /** Export Data using Entity Framework **/ public class CountryContext : DbContext { public DbSet<Country> Countries { get; set; } public CountryContext() { // TODO: Make async Database.EnsureCreated(); } /// <summary> /// Configure context to use Sqlite /// </summary> /// <param name="optionsBuilder"></param> protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var connection = new SqliteConnection($"Data Source=Country.db"); connection.Open(); var command = connection.CreateCommand(); // Create the database if it doesn't already exist command.CommandText = $"PRAGMA foreign_keys = ON;"; command.ExecuteNonQuery(); optionsBuilder.UseSqlite(connection); base.OnConfiguring(optionsBuilder); } } ''' ''' Export Data using Entity Framework ''' * Public Class CountryContext Inherits DbContext Public Property Countries() As DbSet(Of Country) Public Sub New() ' TODO: Make async Database.EnsureCreated() End Sub ''' <summary> ''' Configure context to use Sqlite ''' </summary> ''' <param name="optionsBuilder"></param> Protected Overrides Sub OnConfiguring(ByVal optionsBuilder As DbContextOptionsBuilder) Dim connection = New SqliteConnection($"Data Source=Country.db") connection.Open() Dim command = connection.CreateCommand() ' Create the database if it doesn't already exist command.CommandText = $"PRAGMA foreign_keys = ON;" command.ExecuteNonQuery() optionsBuilder.UseSqlite(connection) MyBase.OnConfiguring(optionsBuilder) End Sub End Class $vbLabelText $csharpLabel 您可以使用 EntityFramework 構建一個模型對象來將數據輸出到數據庫。 public async Task ProcessAsync() { // Get the first worksheet var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx"); var worksheet = workbook.GetWorkSheet("GDPByCountry"); // Create the database connection using (var countryContext = new CountryContext()) { // Iterate through all the cells for (var i = 2; i <= 213; i++) { // Get the range from A-B var range = worksheet[$"A{i}:B{i}"].ToList(); // Create a Country entity to be saved to the database var country = new Country { Name = (string)range[0].Value, GDP = (decimal)(double)range[1].Value }; // Add the entity await countryContext.Countries.AddAsync(country); } // Commit changes to the database await countryContext.SaveChangesAsync(); } } public async Task ProcessAsync() { // Get the first worksheet var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx"); var worksheet = workbook.GetWorkSheet("GDPByCountry"); // Create the database connection using (var countryContext = new CountryContext()) { // Iterate through all the cells for (var i = 2; i <= 213; i++) { // Get the range from A-B var range = worksheet[$"A{i}:B{i}"].ToList(); // Create a Country entity to be saved to the database var country = new Country { Name = (string)range[0].Value, GDP = (decimal)(double)range[1].Value }; // Add the entity await countryContext.Countries.AddAsync(country); } // Commit changes to the database await countryContext.SaveChangesAsync(); } } Public Async Function ProcessAsync() As Task ' Get the first worksheet Dim workbook = WorkBook.Load("Spreadsheets\\GDP.xlsx") Dim worksheet = workbook.GetWorkSheet("GDPByCountry") ' Create the database connection Using countryContext As New CountryContext() ' Iterate through all the cells For i = 2 To 213 ' Get the range from A-B Dim range = worksheet($"A{i}:B{i}").ToList() ' Create a Country entity to be saved to the database Dim country As New Country With { .Name = CStr(range(0).Value), .GDP = CDec(CDbl(range(1).Value)) } ' Add the entity Await countryContext.Countries.AddAsync(country) Next i ' Commit changes to the database Await countryContext.SaveChangesAsync() End Using End Function $vbLabelText $csharpLabel 要使用不同的數據庫,請安裝相應的 NuGet 包,並尋找 UseSQlite 等效的 UseMySQL、UseSqlServer 等。 生成一個 CountryContext,然後遍歷範圍以創建每個條目,並使用 SaveChangesAsync 將數據保存到數據庫中。 // Iterate through all rows with a value for (var y = 2; y < i; y++) { // Get the C cell var cell = sheet[$"C{y}"].First(); // Set the formula for the Percentage of Total column cell.Formula = $"=B{y}/B{i}"; } // Iterate through all rows with a value for (var y = 2; y < i; y++) { // Get the C cell var cell = sheet[$"C{y}"].First(); // Set the formula for the Percentage of Total column cell.Formula = $"=B{y}/B{i}"; } ' Iterate through all rows with a value Dim y = 2 Do While y < i ' Get the C cell Dim cell = sheet($"C{y}").First() ' Set the formula for the Percentage of Total column cell.Formula = $"=B{y}/B{i}" y += 1 Loop $vbLabelText $csharpLabel 將公式融入试算表 Formula 屬性可用來設置單元格的公式。 列 C 中的代碼遍歷每個州並計算總百分比。 可以從 API 下載數據到試算表 /** Data API to Spreadsheet **/ var client = new Client(new Uri("https://restcountries.eu/rest/v2/")); List<RestCountry> countries = await client.GetAsync<List<RestCountry>>(); /** Data API to Spreadsheet **/ var client = new Client(new Uri("https://restcountries.eu/rest/v2/")); List<RestCountry> countries = await client.GetAsync<List<RestCountry>>(); ''' ''' Data API to Spreadsheet ''' * Dim client As New Client(New Uri("https://restcountries.eu/rest/v2/")) Dim countries As List(Of RestCountry) = Await client.GetAsync(Of List(Of RestCountry))() $vbLabelText $csharpLabel 下面的調用使用 RestClient.Net 進行 REST 調用。 它下載 JSON 並將其轉換為 RestCountry 型別的 List。然後,通過遍歷每個國家,可以輕鬆地將數據從 REST API 保存到 Excel 文件中。 API 的 JSON 數據看起來像這樣: for (var i = 2; i < countries.Count; i++) { var country = countries[i]; // Set the basic values worksheet[$"A{i}"].Value = country.name; worksheet[$"B{i}"].Value = country.population; worksheet[$"G{i}"].Value = country.region; worksheet[$"H{i}"].Value = country.numericCode; // Iterate through languages for (var x = 0; x < 3; x++) { if (x > (country.languages.Count - 1)) break; var language = country.languages[x]; // Get the letter for the column var columnLetter = GetColumnLetter(4 + x); // Set the language name worksheet[$"{columnLetter}{i}"].Value = language.name; } } for (var i = 2; i < countries.Count; i++) { var country = countries[i]; // Set the basic values worksheet[$"A{i}"].Value = country.name; worksheet[$"B{i}"].Value = country.population; worksheet[$"G{i}"].Value = country.region; worksheet[$"H{i}"].Value = country.numericCode; // Iterate through languages for (var x = 0; x < 3; x++) { if (x > (country.languages.Count - 1)) break; var language = country.languages[x]; // Get the letter for the column var columnLetter = GetColumnLetter(4 + x); // Set the language name worksheet[$"{columnLetter}{i}"].Value = language.name; } } For i = 2 To countries.Count - 1 Dim country = countries(i) ' Set the basic values worksheet($"A{i}").Value = country.name worksheet($"B{i}").Value = country.population worksheet($"G{i}").Value = country.region worksheet($"H{i}").Value = country.numericCode ' Iterate through languages For x = 0 To 2 If x > (country.languages.Count - 1) Then Exit For End If Dim language = country.languages(x) ' Get the letter for the column Dim columnLetter = GetColumnLetter(4 + x) ' Set the language name worksheet($"{columnLetter}{i}").Value = language.name Next x Next i $vbLabelText $csharpLabel class="content-img-align-center"> class="center-image-wrapper"> 下面的代碼環繞國家並用名字、人口、地區數字代碼和前3種語言填充試算表。 static void Main(string[] args) { var workbook = IronXL.WorkBook.Load($@"{Directory.GetCurrentDirectory()}\Files\HelloWorld.xlsx"); var sheet = workbook.WorkSheets.First(); var cell = sheet["A1"].StringValue; Console.WriteLine(cell); } static void Main(string[] args) { var workbook = IronXL.WorkBook.Load($@"{Directory.GetCurrentDirectory()}\Files\HelloWorld.xlsx"); var sheet = workbook.WorkSheets.First(); var cell = sheet["A1"].StringValue; Console.WriteLine(cell); } Shared Sub Main(ByVal args() As String) Dim workbook = IronXL.WorkBook.Load($"{Directory.GetCurrentDirectory()}\Files\HelloWorld.xlsx") Dim sheet = workbook.WorkSheets.First() Dim cell = sheet("A1").StringValue Console.WriteLine(cell) End Sub $vbLabelText $csharpLabel 用 IronXL 開啟 Excel 文件 /** Create Excel File **/ static void Main(string[] args) { var newXLFile = WorkBook.Create(ExcelFileFormat.XLSX); newXLFile.Metadata.Title = "IronXL New File"; var newWorkSheet = newXLFile.CreateWorkSheet("1stWorkSheet"); newWorkSheet["A1"].Value = "Hello World"; newWorkSheet["A2"].Style.BottomBorder.SetColor("#ff6600"); newWorkSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dashed; } /** Create Excel File **/ static void Main(string[] args) { var newXLFile = WorkBook.Create(ExcelFileFormat.XLSX); newXLFile.Metadata.Title = "IronXL New File"; var newWorkSheet = newXLFile.CreateWorkSheet("1stWorkSheet"); newWorkSheet["A1"].Value = "Hello World"; newWorkSheet["A2"].Style.BottomBorder.SetColor("#ff6600"); newWorkSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dashed; } ''' ''' Create Excel File ''' * Shared Sub Main(ByVal args() As String) Dim newXLFile = WorkBook.Create(ExcelFileFormat.XLSX) newXLFile.Metadata.Title = "IronXL New File" Dim newWorkSheet = newXLFile.CreateWorkSheet("1stWorkSheet") newWorkSheet("A1").Value = "Hello World" newWorkSheet("A2").Style.BottomBorder.SetColor("#ff6600") newWorkSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Dashed End Sub $vbLabelText $csharpLabel 啟動 Excel 文件後,添加前幾行,讀取第一個工作表的第一個單元格,並打印。 使用 IronXL 創建一個新的 Excel 文件。 完成後,您可以使用各自的代碼保存到 CSV、JSON 或 XML。 newXLFile.SaveAsXml($@"{Directory.GetCurrentDirectory()}\Files\HelloWorldXML.XML"); newXLFile.SaveAsXml($@"{Directory.GetCurrentDirectory()}\Files\HelloWorldXML.XML"); newXLFile.SaveAsXml($"{Directory.GetCurrentDirectory()}\Files\HelloWorldXML.XML") $vbLabelText $csharpLabel 例如,要保存到 XML .xml <?xml version="1.0" standalone="yes"?> <_x0031_stWorkSheet> <_x0031_stWorkSheet> <Column1 xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Hello World</Column1> </_x0031_stWorkSheet> <_x0031_stWorkSheet> <Column1 xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /> </_x0031_stWorkSheet> </_x0031_stWorkSheet> <?xml version="1.0" standalone="yes"?> <_x0031_stWorkSheet> <_x0031_stWorkSheet> <Column1 xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Hello World</Column1> </_x0031_stWorkSheet> <_x0031_stWorkSheet> <Column1 xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /> </_x0031_stWorkSheet> </_x0031_stWorkSheet> XML 要保存到 XML,使用 SaveAsXml 如下: 結果如下所示: 讀取 Excel 文件比用 IronXL 更容易用 EPPlus。 IronXL 擁有更精簡的代碼,涵蓋了訪問工作簿中所有列,行和單元格所需的所有內容,而使用 EPPlus 時,則需要的特定代碼行來讀取列和行。 IronXL 在操作 Excel 文檔方面更為智能。 它讓您可以靈活地在任何時候創建更多的工作表,以及從多個工作表和工作簿中讀取數據,而 EPPlus 則一次只處理一個工作表。 另外,使用 IronXL,您還可以使用 Excel 工作簿中的數據填充一個數據庫。 EPPlus 和 IronXL 的授權和定價 EPPlus 授權模型和價格 EPPlus 可在兩個授權模型下使用,即非商業性授權模型和 Polyform 的商業性授權模型。 商業許可證 這些可用於永久性和基於訂閱的格式,期限範圍為 1 個月至 2 年。 在許可期限內,所有許可類別的支持通過支持中心和 NuGet 提供升級。 EPPlus 需要每位開發人員擁有一個許可證。 許可證發行給單個個人,不能共享。 一般指導,即任何直接使用 EPPlus 生產或需要調試代碼的人,都應擁有商業許可證。 如果您的公司將 EPPlus 內部提供為一種服務(例如,通過 API 提供其能力),則您公司需購買涵蓋使用此服務的內部用戶(開發人員)數量的訂閱。 訂閱 持續訂閱可隨時使用最新版本,但開發時必須擁有有效許可證。 許可期結束時,支付完成時許可自動計费并更新。 您可以在許可期結束時取消訂閱,也可以在您需要時啟動新的訂閱。 訂閱可僅在線購買。 EPPlus 可在商業環境中使用。 許可證是由公司內的一位開發人員負責,有不限的部署位置。 每年可購買的許可證數量可增加或減少,並可在每年結束時暫停或取消。 32 天免費試用期作為一種選項提供。 定價:每年自 $299 開始。 您可以按次付費 單個組織內每位開發人員的價格,具有無限的部署位置和Stripe 開戶計費。 每月可增加或降低的許可證數量,並且許可證可在每月結束時暫停或取消。 定價:每月自 $29 開始。 永久性許可證 永久性許可證允許您在特定時間段內升級到新版本並獲得支持。然後,您可以使用在此周期發布的版本發展軟件而不需續期許可證。 許可證是由公司內的一位開發人員負責,有不限的部署位置。 相同公司中每位開發人員的價格不受限於部署地點。 在支持/升級期限內,無限期地使用所有 EPPlus 版本。 定價:每年自 $599 開始。 套裝 帶有初始升級和支援期限的永久性許可證選項可用。 然後,可以在此時期內不需續期許可證的情況下繼續使用發布的版本進行軟件開發。 定價:每年自 $4,295 開始。 Polyform 的非商業許可證 EPPlus 自版本 5 開始授權為 Polyform 非商業授權,意味著代碼是開源的,並可用於非商業用途。 您可以在其網站上查看更多詳細信息。 IronXL 授權模型和價格 可以随时购买扩展。 可以查看扩展。 永久性授權:每個許可證的購買僅需一次,不需續期。 免費支持和產品更新:每個許可證都隨附一年免費產品更新和來自產品背後團隊的支持。 所有许可证为永久许可证,并适用于开发、测试和生产环境。 即時許可證:註冊的許可證金鑰一旦收到付款便會發出。 Iron Software 可用于一个网络应用程序、内部网应用程序或桌面软件。 如有任何有關IronXL for .NET 授權的問題,請聯繫我們的 Iron Software 授權專家。 輕量級許可證 - 允許組織中的一名軟件開發人員在單一地點使用 Iron Software。 許可證不可轉讓,亦不能與組織外或代理/客戶關係共享。此許可證類型,與所有其他許可證類型一樣,明確排除未經協議授權明確授予的所有權利,包括 OEM 重新分發和在未購買額外涵蓋範圍的情況下,將 Iron Software 作為 SaaS 使用。 The Iron Software can be used in as many websites, intranet applications, or desktop software applications as you like. 如有任何有關IronXL for .NET 授權的問題,請聯繫我們的 Iron Software 授權專家。 定價:每年自 $489 開始。 專業許可證 - 允許組織中最高達 10 位軟件開發人員在單一地點使用 Iron Software。 The Iron Software can be used in as many websites, intranet applications, or desktop software applications as you like. 如有任何有關IronXL for .NET 授權的問題,請聯繫我們的 Iron Software 授權專家。 定價:每年自 $976 開始。 無限許可證 - 允許組織中無限數量的軟件開發人員在無限數量的地點使用 Iron Software。 免版稅重新分發 - 允許您將 Iron Software 作為不同的套裝商業產品的一部分進行分發(不需支付版稅),基於基礎許可證涵蓋的項目數量。 結論 允許在 SaaS 軟件服務中部署 Iron Software,基於基礎許可證涵蓋的項目數量。 定價:每年自 $2939 開始。 總結而言,IronXL 比 EPPlus 更實際,因為它允許您按照需要在 Excel 表中進行導航,代碼行較短,並有更多導出 XML、HTML 和 JSON 的機會。 IronXL 還可以讓您將您的工作簿數據整合入數據庫中。 此外,它擁有一個直觀系統,這每次編輯文檔時重新計算公式,並提供一個與 WorkSheet[“A1:B10”] 語法的直觀範圍設置。 工作表功能包含與 Excel 一同工作的公式,並在每次編輯工作表時重新計算。 單元格數據格式擁有多種文本、數字、公式、日期、貨幣、百分比、科學符號和時間。他們的自定義格式有不同的排序方法,例如範圍、列和行。 其單元格樣式包括多種字體、大小、背景圖案、邊框和對齊方式。 所有產品名稱、徽標和品牌均為其各自所有者的財產。 比較僅供參考,反映撰寫時公開可用的信息。 常見問題解答 如何在不使用 Microsoft Office 的情況下建立 Excel 文件? 您可以使用 IronXL 來創建 Excel 文件而不需要 Microsoft Office。IronXL 提供一個簡單的 API,用於在 C# 和 VB.NET 中讀取、編輯和創建 Excel 試算表。 使用 IronXL 相較於 EPPlus 有什麼優勢? IronXL 提供一個更直覺的 API,支持多種格式如 XML、HTML 和 JSON,並允許進行高級樣式和公式重新計算。這使得對於開發人員來說,它比 EPPlus 更加實用和靈活。 是否可以使用 IronXL 操作 Excel 數據並將其導出為不同格式? 是的,IronXL 支持將 Excel 數據導出為多種格式,如 XML、HTML 和 JSON,這使得與數據庫和其他應用程序的集成變得容易。 如何使用 IronXL 處理 Excel 公式? IronXL 支持直覺的公式重新計算,這意味著每次編輯文檔時,公式會自動更新,提供了一個效率高的系統來管理 Excel 公式。 IronXL 提供哪些授權選項? IronXL 提供開發、登台和生產環境的永久授權,價格從每年 489 美元起,提供一年的免費更新和支援。 EPPlus 可以用於創建樞紐分析表和應用條件格式嗎? 可以,EPPlus 支持創建樞紐分析表和應用條件格式,但通常需要比 IronXL 更複雜的代碼。 IronXL 如何支持跨平台開發? IronXL 支持包括 .NET Core、.NET Framework、Xamarin、Mobile、Linux、macOS以及Azure在內的多種平臺,使其對於跨平台開發非常靈活。 IronXL 是否需要在服務器或客戶端機器上安裝 Microsoft Office? 不,IronXL 不需要安裝 Microsoft Office。它被設計為獨立於 Office 工作,提供讀取、編輯和創建 Excel 文件的能力。 EPPlus 在處理 Excel 文件方面的關鍵功能是什麼? EPPlus 因其對 Office OpenXML 的支持而聞名,易於熟悉 Excel 的開發人員使用,並具備創建樞紐分析表和應用條件格式等功能。它是雙重授權模型下提供的。 如何在我的 .NET 專案中安裝 IronXL? 您可以通過 NuGet 套件管理控制台使用命令 Install-Package IronXL.Excel 安裝 IronXL,或使用 .NET CLI 指令 dotnet add package IronXL.Excel。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新日期 6月 22, 2025 IronXL 使用 C# 開發人員的 Zip 壓縮教程 在本教程中,我們將探索如何在 C# 中使用相對路徑創建 ZIP 文件、從壓縮文件中提取數據及操作 ZIP 文件。 閱讀更多 更新日期 7月 28, 2025 比較三個開放源代碼的 C# Excel 函式庫 本文將探討三個 C# 開源 Excel 程式庫,旨在簡化 .NET 環境中的 Excel 檔案操作 閱讀更多 更新日期 8月 4, 2025 EPPlus 讀取 Excel 到 DataTable C#(IronXL 教程) EPPlus 是一個功能強大的開源庫,用於在 C# 中創建和操作 Excel 文件。它提供了一個簡單直觀的 API,允許開發人員以程式的方式生成、讀取和修改 Excel 試算表。 閱讀更多 IronXL 與 Aspose Cells 的比較IronXL 與 Microsoft Office Interop...
更新日期 6月 22, 2025 IronXL 使用 C# 開發人員的 Zip 壓縮教程 在本教程中,我們將探索如何在 C# 中使用相對路徑創建 ZIP 文件、從壓縮文件中提取數據及操作 ZIP 文件。 閱讀更多
更新日期 8月 4, 2025 EPPlus 讀取 Excel 到 DataTable C#(IronXL 教程) EPPlus 是一個功能強大的開源庫,用於在 C# 中創建和操作 Excel 文件。它提供了一個簡單直觀的 API,允許開發人員以程式的方式生成、讀取和修改 Excel 試算表。 閱讀更多