
如何在 C# 中開啟 Excel 文件和撰寫資料
本文將探索IronXL程式庫,以展示如何在C#主控台應用程式中打開Microsoft Excel文件並寫入資料。
IronXL - 一個Excel程式庫
IronXL 是一個.NET Excel程式庫,便於在C#應用程式中建立、讀取和編輯Excel文件。 它提供卓越的性能和精確的輸出。 該程式庫支持所有Excel工作簿文件格式,包括XLS、XLSX、XLSM、CSV和TSV。此外,它允許將資料以JSON、HTML、二進位、字節陣列、DataSet或DataTable格式儲存或導出。
使用IronXL,開發人員可以無縫地處理工作表和單元格範圍。 它提供了編輯公式並在工作表中輕鬆重新計算的能力。 基於範圍、列或行對資料排序非常簡單。 該程式庫提供修改佈局的功能,例如凍結窗格,自動調整行/列大小和新增/刪除行/列。
IronXL還啟用保護Excel文件,使用者密碼和編輯權限。 另一個值得注意的功能是能夠從Excel工作表中新增、移除和提取圖像。 該程式庫提供了廣泛的Excel功能,支持各種單元格資料格式。 這些功能使IronXL成為操作Excel文件最友好的API之一。
IronXL的一個顯著優勢是不需要在機器上安裝Microsoft Excel,消除對Office Interop或任何其他依賴的需求。 它相容多平台,支持.NET 7、6和5,還相容.NET Core 2和3,以及.NET Framework 4.5及更高版本用於處理Excel試算表。
建立控制台應用程式
建議使用最新版本的Visual Studio IDE來建立應用程式。 Visual Studio是C#開發的官方IDE,假定您已經安裝它。 如果您尚未安裝Visual Studio,可以從官方Microsoft Visual Studio網站下載。
按照以下步驟建立一個名為"DemoApp"的新專案。
-
開啟Visual Studio並點擊建立新專案
新專案 -
選擇主控台應用程式並點擊下一步
** 新專案型別 ** -
輸入項目的名稱
新專案名稱 -
選擇.NET版本。 選擇穩定版本.NET 6.0。
新專案其它資訊
安裝IronXL程式庫
專案建立後,需要在專案中安裝IronXL程式庫以便使用。 按照以下步驟安裝。
-
打開管理NuGet套件方案,可以從方案總管或工具中進行操作。
NuGet套件管理器 -
瀏覽IronXL程式庫並選擇當前專案。 點擊安裝。
在NuGet包管理器UI中搜尋和安裝IronXL包
在Program.cs文件的頂部新增以下命名空間
using IronXL;Imports IronXLOpen an Existing Excel File in C#
IronXL提供了打開現有Excel文件或建立新Excel文件的功能。此範例將使用IronXL在C#中打開一個現有文件。
// Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");' Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")現在,讓我們選擇其第一個工作表。 您可以按索引號或按名稱選擇工作表。 DefaultWorkSheet屬性可以幫助獲取第一張工作表。
// Select worksheet at index 0
WorkSheet workSheet = workBook.WorkSheets[0];
// Select worksheet by name
WorkSheet ws = workBook.GetWorkSheet("Sheet1");
// Get any existing worksheet
WorkSheet firstSheet = workBook.DefaultWorkSheet;' Select worksheet at index 0
Dim workSheet As WorkSheet = workBook.WorkSheets(0)
' Select worksheet by name
Dim ws As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Get any existing worksheet
Dim firstSheet As WorkSheet = workBook.DefaultWorkSheet上述程式碼從Excel工作簿中獲得第一張工作表。 要建立包含資料的新Excel文件,請查看此程式碼範例頁面。
現在,讓我們使用IronXL物件程式庫將資料寫入Excel文件。
Write Data to Excel File in C#
使用IronXL將資料寫入Excel文件非常簡單。 有多種方法可以實現,但最簡單的方法是使用Excel的單元格引用。
// Access A1 cell and write the value
workSheet["A1"].Value = "Value using cell reference";' Access A1 cell and write the value
workSheet("A1").Value = "Value using cell reference"還可以將資料寫入一個單元格範圍。 以下程式碼將資料從單元格B1寫入到B5。
// Write the same value to cells from B1 to B5
workSheet["B1:B5"].Value = "Range value";' Write the same value to cells from B1 to B5
workSheet("B1:B5").Value = "Range value"我們還可以使用for迴圈來填充範圍,讓它變得動態化。 程式碼如下:
// Specify range in which we want to write the values
for (int i = 1; i <= 5; i++)
{
// Write the Dynamic value in column C
workSheet["C" + i].Value = "Value: " + i;
// Write the Dynamic value in column D
workSheet["D" + i].Value = "Value: " + i;
}' Specify range in which we want to write the values
For i As Integer = 1 To 5
' Write the Dynamic value in column C
workSheet("C" & i).Value = "Value: " & i
' Write the Dynamic value in column D
workSheet("D" & i).Value = "Value: " & i
Next i寫入Excel文件的另一種方法是使用Replace方法。
// Replace the value in cell D5
workSheet["D5"].Replace("Value: 5", "Replaced Value");' Replace the value in cell D5
workSheet("D5").Replace("Value: 5", "Replaced Value")Save an Excel File in C#
本節說明如何將新寫入的內容儲存到Excel文件中。
// Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx");' Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx")完整程式碼如下:
using System;
using IronXL;
class Program
{
static void Main(string[] args)
{
// Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Select worksheet at index 0
WorkSheet workSheet = workBook.WorkSheets[0];
// Access A1 cell and write the value
workSheet["A1"].Value = "Value using cell reference";
// Write the same value to cells from B1 to B5
workSheet["B1:B5"].Value = "Range value";
// Specify range in which we want to write the values
for (int i = 1; i <= 5; i++)
{
// Write the Dynamic value in column C
workSheet["C" + i].Value = "Value: " + i;
// Write the Dynamic value in column D
workSheet["D" + i].Value = "Value: " + i;
}
// Replace the value in cell D5
workSheet["D5"].Replace("Value: 5", "Replaced Value");
// Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx");
Console.WriteLine("Successfully written to Excel File");
}
}Imports System
Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Select worksheet at index 0
Dim workSheet As WorkSheet = workBook.WorkSheets(0)
' Access A1 cell and write the value
workSheet("A1").Value = "Value using cell reference"
' Write the same value to cells from B1 to B5
workSheet("B1:B5").Value = "Range value"
' Specify range in which we want to write the values
For i As Integer = 1 To 5
' Write the Dynamic value in column C
workSheet("C" & i).Value = "Value: " & i
' Write the Dynamic value in column D
workSheet("D" & i).Value = "Value: " & i
Next i
' Replace the value in cell D5
workSheet("D5").Replace("Value: 5", "Replaced Value")
' Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx")
Console.WriteLine("Successfully written to Excel File")
End Sub
End Class關於如何在C#中讀取Excel文件資料的詳細資訊,請查看此範例。
輸出
文件的輸出是:
輸出Excel文件
總結
本文展示了如何在C#中使用IronXL將資料寫入Excel文件。 IronXL提供了便捷的功能來處理現有的Excel文件。 它還允許您建立新的Excel文件並使用簡單語法寫入資料。 IronXL還可以在不安裝Microsoft Excel應用程式的情況下用於讀取Excel文件。 要從Excel文件中讀取資料,您可以查看此程式碼範例頁面。
IronXL對開發免費,商業用途可以授權。 您也可以嘗試使用IronXL免費試用進行商業用途。

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
相關文章



