跳過到頁腳內容
使用 IRONXL

使用IronXL在C#中將DataSet匯出到Excel | 不需要Office。

在 C# 中將 DataSet 或 DataTable 匯出到 Excel,無需在目標電腦上安裝 Microsoft Office,即可可靠地將結構化的記憶體資料轉換為專業的電子表格檔案。無論您是從 SQL 資料庫中提取記錄、以程式設計方式建立 DataSet,還是使用現有的 DataGrid, IronXL都能透過簡潔的程式碼優先 API 處理整個流程。 本指南將逐步介紹所有主要場景——從單表匯出到多工作表工作簿,再到即時資料庫查詢結果——使用頂級 C# 語句和.NET 10。

只需安裝一次IronXL ,編寫幾行程式碼,您的資料就會保存在 XLSX 檔案中,而無需任何 Office 依賴項。 以下各節涵蓋安裝、核心模式、高級格式設定和資料庫集成,您可以直接複製並貼上到自己的專案中。

如何安裝IronXL以匯出 Excel 檔案?

在編寫任何匯出程式碼之前,請透過NuGet套件管理器將IronXL新增到您的專案中。 在專案根目錄下開啟終端機並執行.NET CLI 命令,或在 Visual Studio 中使用套件管理器控制台並執行 Install-Package 命令:

dotnet add package IronXl.Excel
# Or in Visual Studio Package Manager Console:
# Install-Package IronXl.Excel
dotnet add package IronXl.Excel
# Or in Visual Studio Package Manager Console:
# Install-Package IronXl.Excel
SHELL

安裝完成後,新增一個頂層 using 語句來匯入IronXL命名空間:

using IronXL;
using System.Data;
using IronXL;
using System.Data;
$vbLabelText   $csharpLabel

IronXL 的目標架構包括.NET 8、 .NET 9 和.NET 10,以及較舊的.NET Framework版本(4.6.2+)。 無需在任何機器(伺服器、桌上型電腦或雲端)上安裝 Office。 安裝軟體套件後,您就可以完全透過託管程式碼建立、讀取和寫入 XLSX、XLS 和 CSV 檔案。

有關詳細的安裝選項,請參閱IronXL安裝指南NuGet套件頁面以取得版本歷史記錄。

如何將單一資料表匯出到 Excel 文件?

最常見的情況是將一個資料表匯出到單一工作表中。 IronXL 的 WorkBookWorkSheet 類別可以自然地對應到 Excel 物件模型。 您建立一個工作簿,新增一個工作表,然後遍歷資料表的列和行。

以下範例建立一個記憶體中的 Employees 資料表,並將其寫入 EmployeeData.xlsx:

using IronXL;
using System.Data;

// Build an in-memory DataTable
DataTable dt = new DataTable("Employees");
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Department", typeof(string));

dt.Rows.Add(1, "John Smith", "Engineering");
dt.Rows.Add(2, "Sarah Jones", "Marketing");
dt.Rows.Add(3, "Mike Wilson", "Sales");

// Create a workbook and a named worksheet
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet worksheet = workbook.CreateWorkSheet("Employees");

// Write column headers in row 0
for (int col = 0; col < dt.Columns.Count; col++)
{
    worksheet.SetCellValue(0, col, dt.Columns[col].ColumnName);
}

// Write data rows starting at row 1
for (int row = 0; row < dt.Rows.Count; row++)
{
    for (int col = 0; col < dt.Columns.Count; col++)
    {
        worksheet.SetCellValue(row + 1, col, dt.Rows[row][col].ToString());
    }
}

workbook.SaveAs("EmployeeData.xlsx");
using IronXL;
using System.Data;

// Build an in-memory DataTable
DataTable dt = new DataTable("Employees");
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Department", typeof(string));

dt.Rows.Add(1, "John Smith", "Engineering");
dt.Rows.Add(2, "Sarah Jones", "Marketing");
dt.Rows.Add(3, "Mike Wilson", "Sales");

// Create a workbook and a named worksheet
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet worksheet = workbook.CreateWorkSheet("Employees");

// Write column headers in row 0
for (int col = 0; col < dt.Columns.Count; col++)
{
    worksheet.SetCellValue(0, col, dt.Columns[col].ColumnName);
}

// Write data rows starting at row 1
for (int row = 0; row < dt.Rows.Count; row++)
{
    for (int col = 0; col < dt.Columns.Count; col++)
    {
        worksheet.SetCellValue(row + 1, col, dt.Rows[row][col].ToString());
    }
}

workbook.SaveAs("EmployeeData.xlsx");
$vbLabelText   $csharpLabel

輸出

如何使用.NET將資料集匯出至 Excel: IronXL完整指南:圖 1 - .NET中將簡單 DataTable 匯出至 Excel 的輸出

SetCellValue 接受 int、double、string 和 bool 類型 -- 因此您可以獲得原生的 Excel 儲存格類型,而無需額外的類型轉換。 循環模式適用於任何大小的表,因為IronXL直接寫入內存,只有在調用 SaveAs 時才刷新到磁碟,即使對於數千行數據,也能保持高效的處理速度。

要更全面地了解讀寫技術, IronXL C# 教學涵蓋了所有檔案操作以及這種匯出模式。

如何將資料集匯出到多個Excel工作表中?

當一個資料集包含多個相關表(例如產品表和訂單表)時,您可以將每個資料表對應到一個工作簿中的對應工作表。 這樣,收件人無需打開多個文檔,即可在一個文件中查看所有相關資料。

下面的循環遍歷 ds.Tables,並為每個表格建立工作表:

using IronXL;
using System.Data;

// Build a DataSet with two related tables
DataSet ds = new DataSet("CompanyData");

DataTable dtProducts = new DataTable("Products");
dtProducts.Columns.Add("ProductID", typeof(int));
dtProducts.Columns.Add("ProductName", typeof(string));
dtProducts.Columns.Add("Price", typeof(decimal));
dtProducts.Rows.Add(101, "Widget A", 29.99m);
dtProducts.Rows.Add(102, "Widget B", 49.99m);
ds.Tables.Add(dtProducts);

DataTable dtOrders = new DataTable("Orders");
dtOrders.Columns.Add("OrderID", typeof(int));
dtOrders.Columns.Add("Customer", typeof(string));
dtOrders.Columns.Add("Total", typeof(decimal));
dtOrders.Rows.Add(1001, "Acme Corp", 599.90m);
dtOrders.Rows.Add(1002, "Tech Inc", 299.95m);
ds.Tables.Add(dtOrders);

// Create one workbook and iterate each DataTable
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);

foreach (DataTable table in ds.Tables)
{
    WorkSheet sheet = workbook.CreateWorkSheet(table.TableName);

    // Write headers
    for (int col = 0; col < table.Columns.Count; col++)
    {
        sheet.SetCellValue(0, col, table.Columns[col].ColumnName);
    }

    // Write rows
    for (int row = 0; row < table.Rows.Count; row++)
    {
        for (int col = 0; col < table.Columns.Count; col++)
        {
            sheet.SetCellValue(row + 1, col, table.Rows[row][col].ToString());
        }
    }
}

workbook.SaveAs("CompanyReport.xlsx");
using IronXL;
using System.Data;

// Build a DataSet with two related tables
DataSet ds = new DataSet("CompanyData");

DataTable dtProducts = new DataTable("Products");
dtProducts.Columns.Add("ProductID", typeof(int));
dtProducts.Columns.Add("ProductName", typeof(string));
dtProducts.Columns.Add("Price", typeof(decimal));
dtProducts.Rows.Add(101, "Widget A", 29.99m);
dtProducts.Rows.Add(102, "Widget B", 49.99m);
ds.Tables.Add(dtProducts);

DataTable dtOrders = new DataTable("Orders");
dtOrders.Columns.Add("OrderID", typeof(int));
dtOrders.Columns.Add("Customer", typeof(string));
dtOrders.Columns.Add("Total", typeof(decimal));
dtOrders.Rows.Add(1001, "Acme Corp", 599.90m);
dtOrders.Rows.Add(1002, "Tech Inc", 299.95m);
ds.Tables.Add(dtOrders);

// Create one workbook and iterate each DataTable
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);

foreach (DataTable table in ds.Tables)
{
    WorkSheet sheet = workbook.CreateWorkSheet(table.TableName);

    // Write headers
    for (int col = 0; col < table.Columns.Count; col++)
    {
        sheet.SetCellValue(0, col, table.Columns[col].ColumnName);
    }

    // Write rows
    for (int row = 0; row < table.Rows.Count; row++)
    {
        for (int col = 0; col < table.Columns.Count; col++)
        {
            sheet.SetCellValue(row + 1, col, table.Rows[row][col].ToString());
        }
    }
}

workbook.SaveAs("CompanyReport.xlsx");
$vbLabelText   $csharpLabel

輸出

如何將資料集匯出至 Excel VB .NET: IronXL完整指南:圖 2 - 產生的 Excel 文件,其中包含匯出的資料集作為單獨的工作表

每個工作表都以來源資料表命名,使得在 Excel 中導覽變得非常簡單。 因為 WorkBook.Create 在記憶體中分配工作簿,所以您可以在循環中不斷添加工作表而無需重新開啟檔案 -- IronXL在寫入磁碟之前建立整個 XLSX 結構。

IronXL WorkBook 文件列出了 WorkBook 物件上的所有可用方法,包括工作表重新排序、密碼保護以及儲存為 XLSX 以外的格式。

如何在Excel匯出檔案中新增標題格式?

純文字資料匯出功能正常,但新增粗體標題和列寬後,輸出結果即可立即在業務環境中使用。 IronXL透過附加到任何 WorkSheet 範圍的 Style 物件公開儲存格和範圍樣式。

以下程式碼片段將標題行的字體格式設定為粗體,並在寫入資料後自動調整列寬:

using IronXL;
using System.Data;

DataTable dt = new DataTable("Sales");
dt.Columns.Add("Region", typeof(string));
dt.Columns.Add("Q1", typeof(decimal));
dt.Columns.Add("Q2", typeof(decimal));
dt.Columns.Add("Q3", typeof(decimal));
dt.Columns.Add("Q4", typeof(decimal));

dt.Rows.Add("不rth", 120000m, 135000m, 142000m, 158000m);
dt.Rows.Add("South", 98000m, 104000m, 112000m, 121000m);
dt.Rows.Add("East",  87000m, 93000m,  99000m,  108000m);
dt.Rows.Add("West",  145000m, 152000m, 161000m, 174000m);

WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet sheet = workbook.CreateWorkSheet("Sales");

// Write and style headers
for (int col = 0; col < dt.Columns.Count; col++)
{
    sheet.SetCellValue(0, col, dt.Columns[col].ColumnName);
    sheet["A1:E1"].Style.Font.Bold = true;
    sheet["A1:E1"].Style.Font.Height = 12;
}

// Write data rows
for (int row = 0; row < dt.Rows.Count; row++)
{
    for (int col = 0; col < dt.Columns.Count; col++)
    {
        sheet.SetCellValue(row + 1, col, dt.Rows[row][col]);
    }
}

workbook.SaveAs("SalesReport.xlsx");
using IronXL;
using System.Data;

DataTable dt = new DataTable("Sales");
dt.Columns.Add("Region", typeof(string));
dt.Columns.Add("Q1", typeof(decimal));
dt.Columns.Add("Q2", typeof(decimal));
dt.Columns.Add("Q3", typeof(decimal));
dt.Columns.Add("Q4", typeof(decimal));

dt.Rows.Add("不rth", 120000m, 135000m, 142000m, 158000m);
dt.Rows.Add("South", 98000m, 104000m, 112000m, 121000m);
dt.Rows.Add("East",  87000m, 93000m,  99000m,  108000m);
dt.Rows.Add("West",  145000m, 152000m, 161000m, 174000m);

WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet sheet = workbook.CreateWorkSheet("Sales");

// Write and style headers
for (int col = 0; col < dt.Columns.Count; col++)
{
    sheet.SetCellValue(0, col, dt.Columns[col].ColumnName);
    sheet["A1:E1"].Style.Font.Bold = true;
    sheet["A1:E1"].Style.Font.Height = 12;
}

// Write data rows
for (int row = 0; row < dt.Rows.Count; row++)
{
    for (int col = 0; col < dt.Columns.Count; col++)
    {
        sheet.SetCellValue(row + 1, col, dt.Rows[row][col]);
    }
}

workbook.SaveAs("SalesReport.xlsx");
$vbLabelText   $csharpLabel

Style.Font.Bold 屬性適用於單一語句中的整個標頭範圍。 IronXL也支援背景顏色(Style.HorizontalAlignment)。 有關完整的樣式參考,請參閱IronXL單元格格式指南

如何將資料庫查詢結果匯出到 Excel?

連接到 SQL 資料庫並將結果直接匯出到 Excel 涵蓋了一個重要的實際應用場景——定期報告、資料快照和管理匯出。 此方法使用 SqlDataAdapter 填入 DataSet,然後將產生的 DataTable 對應到IronXL工作表。

using IronXL;
using System.Data;
using System.Data.SqlClient;

string connectionString = "Server=localhost;Database=SalesDB;Integrated Security=True;";
string query = "SELECT CustomerID, CompanyName, ContactName, Country FROM Customers";

// Fill DataSet from SQL query
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
    adapter.Fill(ds, "Customers");
}

DataTable dt = ds.Tables["Customers"]!;

// Create workbook and export
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet worksheet = workbook.CreateWorkSheet("CustomerData");

// Bold header row
for (int col = 0; col < dt.Columns.Count; col++)
{
    worksheet.SetCellValue(0, col, dt.Columns[col].ColumnName);
}
worksheet["A1:D1"].Style.Font.Bold = true;

// Write rows, handling DBNull values safely
for (int row = 0; row < dt.Rows.Count; row++)
{
    for (int col = 0; col < dt.Columns.Count; col++)
    {
        object cellValue = dt.Rows[row][col];
        worksheet.SetCellValue(row + 1, col,
            cellValue == DBNull.Value ? "" : cellValue.ToString()!);
    }
}

workbook.SaveAs("CustomerExport.xlsx");
Console.WriteLine("Export complete.");
using IronXL;
using System.Data;
using System.Data.SqlClient;

string connectionString = "Server=localhost;Database=SalesDB;Integrated Security=True;";
string query = "SELECT CustomerID, CompanyName, ContactName, Country FROM Customers";

// Fill DataSet from SQL query
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
    adapter.Fill(ds, "Customers");
}

DataTable dt = ds.Tables["Customers"]!;

// Create workbook and export
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet worksheet = workbook.CreateWorkSheet("CustomerData");

// Bold header row
for (int col = 0; col < dt.Columns.Count; col++)
{
    worksheet.SetCellValue(0, col, dt.Columns[col].ColumnName);
}
worksheet["A1:D1"].Style.Font.Bold = true;

// Write rows, handling DBNull values safely
for (int row = 0; row < dt.Rows.Count; row++)
{
    for (int col = 0; col < dt.Columns.Count; col++)
    {
        object cellValue = dt.Rows[row][col];
        worksheet.SetCellValue(row + 1, col,
            cellValue == DBNull.Value ? "" : cellValue.ToString()!);
    }
}

workbook.SaveAs("CustomerExport.xlsx");
Console.WriteLine("Export complete.");
$vbLabelText   $csharpLabel

資料庫匯出要點

空值守衛(cellValue == DBNull.Value ? "" : cellValue.ToString()) prevents exceptions when the database returns NULL 值 -- 可選列中常見的情況。 標題行採用粗體格式,可以讓報告接收者立即區分標題和資料。

有關如何將資料從 Excel 讀回 C# 物件的更多信息,請參閱IronXL DataTable 匯出教程,了解如何逆轉此工作流程。

IronXL與其他 Excel 匯出方法相比有何優勢?

在選擇庫之前,了解主要選項在資料集匯出場景中最重要方面的優劣對比很有幫助。

.NET資料集場景的 Excel 匯出程式庫比較
方法 辦公室要求 XLSX 支援 樣式 API 多頁 效能(大數據)
IronXL 是的 滿的 是的 高的
Microsoft Office Interop 是的 是的 滿的 是的 低的
EPPlus 是的 部分的 是的 高的
封閉式 XML 是的 部分的 是的 中等的
CSV(手動) 沒有任何 非常高

Microsoft Office Interop 僅適用於安裝了 Office 的 Windows 機器,因此無法用於伺服器部署和雲端功能。 IronXL、EPPlus 和 封閉式 XML 都支援伺服器端產生——關鍵區別在於樣式 API 的廣度和許可模式。 IronXL 的許可涵蓋商業用途,並且透過單一軟體包即可覆蓋所有.NET目標框架。

對於第三方基準測試和比較, Microsoft 關於 Excel 文件格式的文檔EPPlus 專案頁面提供了更多背景信息,以便為您的專案做出正確的選擇。

如何處理大型資料集而不出現記憶體問題?

在匯出包含數萬行資料的資料表時,保持記憶體使用量可預測非常重要。 以下模式直接從 IDataReader 串流數據,而不是先將整個結果集載入到 DataTable 中。這樣可以避免同時保存讀取緩衝區和 DataTable 而造成的雙倍記憶體開銷。

using IronXL;
using System.Data;
using System.Data.SqlClient;

string connectionString = "Server=localhost;Database=InventoryDB;Integrated Security=True;";
string query = "SELECT ProductID, SKU, Description, StockLevel, UnitCost FROM Products";

WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet sheet = workbook.CreateWorkSheet("Inventory");

using SqlConnection conn = new SqlConnection(connectionString);
conn.Open();

using SqlCommand cmd = new SqlCommand(query, conn);
using SqlDataReader reader = cmd.ExecuteReader();

// Write schema-derived headers
for (int col = 0; col < reader.FieldCount; col++)
{
    sheet.SetCellValue(0, col, reader.GetName(col));
}

// Stream rows directly into IronXL without buffering a DataTable
int excelRow = 1;
while (reader.Read())
{
    for (int col = 0; col < reader.FieldCount; col++)
    {
        sheet.SetCellValue(excelRow, col,
            reader.IsDBNull(col) ? "" : reader.GetValue(col).ToString()!);
    }
    excelRow++;
}

workbook.SaveAs("InventoryExport.xlsx");
Console.WriteLine($"Exported {excelRow - 1} rows.");
using IronXL;
using System.Data;
using System.Data.SqlClient;

string connectionString = "Server=localhost;Database=InventoryDB;Integrated Security=True;";
string query = "SELECT ProductID, SKU, Description, StockLevel, UnitCost FROM Products";

WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet sheet = workbook.CreateWorkSheet("Inventory");

using SqlConnection conn = new SqlConnection(connectionString);
conn.Open();

using SqlCommand cmd = new SqlCommand(query, conn);
using SqlDataReader reader = cmd.ExecuteReader();

// Write schema-derived headers
for (int col = 0; col < reader.FieldCount; col++)
{
    sheet.SetCellValue(0, col, reader.GetName(col));
}

// Stream rows directly into IronXL without buffering a DataTable
int excelRow = 1;
while (reader.Read())
{
    for (int col = 0; col < reader.FieldCount; col++)
    {
        sheet.SetCellValue(excelRow, col,
            reader.IsDBNull(col) ? "" : reader.GetValue(col).ToString()!);
    }
    excelRow++;
}

workbook.SaveAs("InventoryExport.xlsx");
Console.WriteLine($"Exported {excelRow - 1} rows.");
$vbLabelText   $csharpLabel

當結果集可能超過 50,000 行或在記憶體受限的服務中運行時,建議使用此模式。 IronXL IronXL文件涵蓋了針對大容量匯出任務的其他記憶體和吞吐量調優選項。

批次儲存為 CSV 文件,適用於超大文件匯出

對於超過 50 萬行且記憶體非常緊張的匯出操作,請考慮將輸出拆分到多個工作簿中,或改為匯出為 CSV 格式:

using IronXL;

// IronXL can save any workbook as CSV
workbook.SaveAs("LargeExport.csv");
using IronXL;

// IronXL can save any workbook as CSV
workbook.SaveAs("LargeExport.csv");
$vbLabelText   $csharpLabel

CSV 檔案會遺失格式和多工作表支持,但對於使用原始分隔資料的下游 ETL 流程仍然有效。

如何將Excel檔案儲存為不同的格式和位置?

IronXL支援儲存到檔案路徑,MemoryStreambyte[] -- 這對於傳回 Excel 作為下載回應的ASP.NET應用程式來說很重要。

using IronXL;
using System.IO;

WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet sheet = workbook.CreateWorkSheet("Report");
sheet.SetCellValue(0, 0, "Generated Report");

// Save to file path
workbook.SaveAs("Report.xlsx");

// Save as XLS (legacy format)
workbook.SaveAs("Report.xls");

// Save to MemoryStream for HTTP response
using MemoryStream ms = new MemoryStream();
workbook.SaveAs(ms);
byte[] fileBytes = ms.ToArray();
// fileBytes can be returned from an ASP.NET controller action
using IronXL;
using System.IO;

WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet sheet = workbook.CreateWorkSheet("Report");
sheet.SetCellValue(0, 0, "Generated Report");

// Save to file path
workbook.SaveAs("Report.xlsx");

// Save as XLS (legacy format)
workbook.SaveAs("Report.xls");

// Save to MemoryStream for HTTP response
using MemoryStream ms = new MemoryStream();
workbook.SaveAs(ms);
byte[] fileBytes = ms.ToArray();
// fileBytes can be returned from an ASP.NET controller action
$vbLabelText   $csharpLabel

對於ASP.NET Core,從控制器操作返回 File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx") 以觸發瀏覽器下載。 IronXL ASP.NET Excel 指南展示了完整的控制器整合模式。

對於其他.NET Excel 函式庫, Open XML SDK是許多函式庫建置的開源基礎。

支援的輸出格式

IronXL無需任何其他依賴項即可儲存為以下格式:

  • XLSX -- OpenXML 格式,相容於 Excel 2007 及更高版本
  • XLS -- 傳統二進位格式,相容於 Excel 97-2003
  • CSV-逗號分隔值,全球通用格式
  • TSV -- 製表符分隔值
  • JSON -- 結構化資料匯出

透過更改 SaveAs 路徑中的檔案副檔名或使用接受 ExcelFileFormat 枚舉值的重載方法來切換格式。

如何驗證匯出的Excel檔案是否正確?

匯出後,您可以使用IronXL讀取文件,以驗證行數、抽查值或在測試項目中執行自動斷言:

using IronXL;

WorkBook loaded = WorkBook.Load("EmployeeData.xlsx");
WorkSheet sheet = loaded.WorkSheets[0];

// Verify row count (header row + data rows)
int totalRows = sheet.Rows.Count();
Console.WriteLine($"Rows in file: {totalRows}");

// Spot-check specific cell values
string header = sheet["A1"].StringValue;
string firstEmployee = sheet["A2"].StringValue;

Console.WriteLine($"Header: {header}, First row: {firstEmployee}");
using IronXL;

WorkBook loaded = WorkBook.Load("EmployeeData.xlsx");
WorkSheet sheet = loaded.WorkSheets[0];

// Verify row count (header row + data rows)
int totalRows = sheet.Rows.Count();
Console.WriteLine($"Rows in file: {totalRows}");

// Spot-check specific cell values
string header = sheet["A1"].StringValue;
string firstEmployee = sheet["A2"].StringValue;

Console.WriteLine($"Header: {header}, First row: {firstEmployee}");
$vbLabelText   $csharpLabel

這種驗證模式在整合測試套件中特別有用,因為在將文件傳送到外部系統之前必須確認匯出正確性。 IronXL讀取教學展示了載入檔案後可用的所有讀取操作。

有關.NET中 DataSet 和 DataTable 模式的更多閱讀資料,請參閱Microsoft DataSet 文檔,其中深入介紹了 ADO .NET物件模型。

下一步計劃是什麼?

現在,您已經擁有了 C# 中所有主要資料集到 Excel 匯出場景的工作模式——從單一 DataTable 到多工作表工作簿、資料庫驅動的匯出、格式化標題和大資料流。 根據您的目標,接下來該如何做:

-免費試用IronXL -- 先取得免費試用許可證,並在投入生產之前使用您自己的資料運行上述範例。 -閱讀完整的 API 參考-- IronXL物件參考涵蓋了 WorkBook、WorkSheet 和儲存格區域的每個方法。 -探索相關的匯出教學-- C# DataTable 到 Excel 指南涵蓋了反向工作流程 -- 將 Excel 讀回 DataTable 物件。 -查看格式設定選項-- IronXL儲存格格式設定文件顯示了數字格式、邊框、背景顏色和對齊方式選項。 -檢查許可證-- 生產部署,請參閱IronXL授權頁面,在開發者、組織和免版稅選項之間進行選擇。 -與其他Iron Software工具比較-- 如果您的專案還需要產生 PDF 或 OCR, Iron Software產品套件可以讓您以更低的價格捆綁多個庫。

基於這裡介紹的模式,您可以完全在託管的 C# 中分層添加計劃任務自動化、 ASP.NET Core下載端點和多格式輸出管道——無需 Office、無需 COM、無需NuGet包之外的運行時相依性。

常見問題解答

如何在 C# 中將 DataSet 匯出至 Excel?

您可以在 C# 中使用 IronXL程式庫將 DataSet 匯出至 Excel,該程式庫提供以程式碼為先的 API,讓您無需 Microsoft Office 即可建立、編輯及處理 Excel 檔案。

我需要安裝 Microsoft Office 才能使用 IronXL 嗎?

不,IronXL 無需在電腦上安裝 Microsoft Office 即可匯出或處理 Excel 檔案。

我可以使用 IronXL 匯出哪些類型的資料?

IronXL 允許您將結構化資料(例如 DataTables 和 DataSets)匯出至 Excel 檔案,同時也能匯出 SqlDataReader 及其他 ADO.NET 來源的資料。

IronXL 能否處理 Excel 檔案的建立與編輯?

是的,IronXL 提供透過 C# 程式化建立、編輯及處理 Excel 檔案的功能。

IronXL 是否適用於伺服器端和雲端應用程式?

是的,IronXL 非常適合需要在不安裝任何 Office 軟體的情況下生成 Excel 檔案的伺服器端應用程式和雲端函式。

使用 IronXL 匯出 DataSet 有什麼好處?

IronXL 無需依賴 Microsoft Office 等外部套件即可處理 Excel 檔案的建立與操作,支援 XLSX、XLS 及 CSV 格式,並適用於所有 .NET Framework 目標框架。

IronXL 能否在 ASP.NET Core 中使用,以返回 Excel 下載檔?

是的,IronXL 可以將工作簿儲存至 MemoryStream,而產生的位元組陣列可透過 ASP.NET Core 控制器動作作為檔案下載回傳。

IronXL 是否支援大型資料集?

是的,IronXL 能夠直接從 SqlDataReader 串流資料,無需緩衝完整的 DataTable,因此對於大型結果集而言,記憶體使用量仍可預測。

IronXL 支援哪些 Excel 格式進行儲存?

IronXL 支援儲存為 XLSX、XLS、CSV、TSV 及 JSON 格式,且無需任何額外依賴項。

匯出至 Excel 時,能否套用粗體標題等格式?

是的,IronXL 提供針對儲存格範圍的樣式 API,可用於設定粗體字型、背景顏色、邊框、數字格式及對齊方式。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担產品测测试,產品開發和研究的责任時,Jordi 為持续的產品改進增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me