如何使用 C# Interop 與 IronXL 在 Excel 中建立資料透視表
在 Excel 中以程式設計方式產生資料透視表需要使用 C# Interop 及其 Office 依賴項,或使用像IronXL這樣可以獨立工作的現代函式庫。 本教學示範了這兩種方法,重點介紹了為什麼開發人員越來越多地選擇 IronXL 而不是傳統的互通方法。
本文將介紹如何編輯、建立、設計和計算資料透視表和資料透視組,以及如何進行自動分析和錯誤處理。
什麼是Excel資料透視表?
資料透視表是Excel中最強大的工具之一。 這是一種簡單易行的匯總大型資料集的方法。 因此,資料透視表可以幫助您輕鬆顯示、理解和分析數值資料。 資料透視表不僅在 Excel 中可用,而且在其他程式中也可用,例如 Google Sheets、Apple Numbers 和 CSV 匯出。 它提供了一種查看數據概覽的解決方案——充當數據控制台,讓人們能夠以有意義的方式查看他們的資訊——並與現實聯繫起來。
讓我們先用錯誤的方法建立一個資料透視表,然後再學習如何用 C# 正確地建立它:
如何使用 C# Interop 在 Excel 表格中建立資料透視表?
C# Excel Interop 透過 COM 自動化提供對 Excel 資料透視表功能的直接存取。 以下是許多開發人員在尋找用於在 C# 中產生透視表的工具時遇到的傳統方法:(已棄用)
如何在 .NET 中以傳統方式建立資料透視表
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
// Create Excel application instance
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Data\SalesData.xlsx");
Excel.Worksheet xlSheet = (Excel.Worksheet)xlWorkbook.Sheets[1];
Excel.Worksheet xlPivotSheet = (Excel.Worksheet)xlWorkbook.Sheets.Add();
// Define data range for pivot table
Excel.Range dataRange = xlSheet.UsedRange;
// Create pivot cache and pivot table
Excel.PivotCache pivotCache = xlWorkbook.PivotCaches().Create(
Excel.XlPivotTableSourceType.xlDatabase,
dataRange,
Type.Missing);
Excel.PivotTable pivotTable = pivotCache.CreatePivotTable(
xlPivotSheet.Cells[3, 1],
"SalesPivot",
Type.Missing,
Type.Missing);
// Configure pivot table fields
Excel.PivotField productField = (Excel.PivotField)pivotTable.PivotFields("Product");
productField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
productField.Position = 1;
Excel.PivotField regionField = (Excel.PivotField)pivotTable.PivotFields("Region");
regionField.Orientation = Excel.XlPivotFieldOrientation.xlColumnField;
regionField.Position = 1;
Excel.PivotField salesField = (Excel.PivotField)pivotTable.PivotFields("Sales");
pivotTable.AddDataField(salesField, "Sum of Sales", Excel.XlConsolidationFunction.xlSum);
// Save and cleanup
xlWorkbook.SaveAs(@"C:\Data\PivotReport.xlsx");
xlWorkbook.Close();
xlApp.Quit();
// Release COM objects to prevent memory leaks
Marshal.ReleaseComObject(pivotTable);
Marshal.ReleaseComObject(pivotCache);
Marshal.ReleaseComObject(xlPivotSheet);
Marshal.ReleaseComObject(xlSheet);
Marshal.ReleaseComObject(xlWorkbook);
Marshal.ReleaseComObject(xlApp);using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
// Create Excel application instance
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Data\SalesData.xlsx");
Excel.Worksheet xlSheet = (Excel.Worksheet)xlWorkbook.Sheets[1];
Excel.Worksheet xlPivotSheet = (Excel.Worksheet)xlWorkbook.Sheets.Add();
// Define data range for pivot table
Excel.Range dataRange = xlSheet.UsedRange;
// Create pivot cache and pivot table
Excel.PivotCache pivotCache = xlWorkbook.PivotCaches().Create(
Excel.XlPivotTableSourceType.xlDatabase,
dataRange,
Type.Missing);
Excel.PivotTable pivotTable = pivotCache.CreatePivotTable(
xlPivotSheet.Cells[3, 1],
"SalesPivot",
Type.Missing,
Type.Missing);
// Configure pivot table fields
Excel.PivotField productField = (Excel.PivotField)pivotTable.PivotFields("Product");
productField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
productField.Position = 1;
Excel.PivotField regionField = (Excel.PivotField)pivotTable.PivotFields("Region");
regionField.Orientation = Excel.XlPivotFieldOrientation.xlColumnField;
regionField.Position = 1;
Excel.PivotField salesField = (Excel.PivotField)pivotTable.PivotFields("Sales");
pivotTable.AddDataField(salesField, "Sum of Sales", Excel.XlConsolidationFunction.xlSum);
// Save and cleanup
xlWorkbook.SaveAs(@"C:\Data\PivotReport.xlsx");
xlWorkbook.Close();
xlApp.Quit();
// Release COM objects to prevent memory leaks
Marshal.ReleaseComObject(pivotTable);
Marshal.ReleaseComObject(pivotCache);
Marshal.ReleaseComObject(xlPivotSheet);
Marshal.ReleaseComObject(xlSheet);
Marshal.ReleaseComObject(xlWorkbook);
Marshal.ReleaseComObject(xlApp);IRON VB CONVERTER ERROR developers@ironsoftware.com此 Interop 範例建立了一個原生 Excel 資料透視表,其中產品為行,地區為列,銷售額匯總在資料區中。 雖然這種方法可行,但需要安裝 Microsoft Office 並進行仔細的 COM 物件管理。 請參閱微軟文檔,了解為什麼不應在現代開發中使用此方法。
C# 互通會帶來哪些問題?
互通性方法面臨幾個重大挑戰:
遺憾的是,Stack Overflow 和其他程式網站仍然推薦它,因為它們的討論主題仍然停留在 2000 年代初期的想法上。
部署相依性:需要在執行原始碼的每台機器上安裝 Microsoft Office,包括生產伺服器。 這會增加許可成本和部署複雜性。
記憶體管理:必須使用Marshal.ReleaseComObject()明確釋放 COM 物件。 即使缺少一個物件也會導致 Excel 進程在記憶體中掛起, Stack Overflow 上對此有大量記錄。
平台限制詳情:這個老式的解決方案僅適用於安裝了 Office 的 Windows 系統,速度可能非常慢,令使用者感到困惑,並可能導致記憶體洩漏。 不支援 Linux、macOS、Docker 容器或 Azure Functions 等雲端平台。
效能問題:啟動 Excel 應用程式執行個體速度慢且資源消耗大,尤其是伺服器端處理。
版本相容性:不同 Office 版本可能具有不同的 COM 接口,從而導致跨環境的相容性問題。
IronXL 如何在不使用互通的情況下以程式設計方式建立資料透視表?
IronXL使用託管程式碼建立資料透視表,沒有 COM 依賴項,其建立方式有所不同。 雖然它不能建立 Excel 原生資料透視表,但它提供了強大的聚合功能。
如何以現代方式透過程式設計建立 XLSX 或 XLS 資料透視表
using IronXL;
using System.Linq;
using System.Data; // Keep this namespace
using static System.Data.DataTableExtensions; // Use 'using static' for DataTableExtensions
class Program
{
static void Main(string[] args)
{
// Load Excel file - no Office required
WorkBook workbook = WorkBook.Load("SalesData.xlsx");
WorkSheet dataSheet = workbook.WorkSheets[0];
// Convert to DataTable for powerful manipulation
var dataTable = dataSheet.ToDataTable(true); // true = use first row as column headers
// Create pivot-style aggregation using LINQ
var pivotData = dataTable.AsEnumerable()
.GroupBy(row => new {
Product = row["Product"].ToString(),
Region = row["Region"].ToString()
}) //range
.Select(g => new {
Product = g.Key.Product,
Region = g.Key.Region,
TotalSales = g.Sum(row => Convert.ToDecimal(row["Sales"])),
AverageSale = g.Average(row => Convert.ToDecimal(row["Sales"])),
Count = g.Count()
});
// Create pivot report worksheet
WorkSheet pivotSheet = workbook.CreateWorkSheet("PivotReport");
// Build cross-tabulation structure
var products = pivotData.Select(p => p.Product).Distinct().OrderBy(p => p);
var regions = pivotData.Select(p => p.Region).Distinct().OrderBy(r => r);
// Create headers
pivotSheet["A1"].Value = "Product/Region";
int col = 2;
foreach (var region in regions)
{
pivotSheet[$"{(char)('A' + col - 1)}1"].Value = region; // string
col++;
}
// Populate pivot data
int row = 2;
foreach (var product in products)
{
pivotSheet[$"A{row}"].Value = product;
col = 2;
foreach (var region in regions)
{
var sales = pivotData
.Where(p => p.Product == product && p.Region == region)
.Select(p => p.TotalSales)
.FirstOrDefault();
pivotSheet[$"{(char)('A' + col - 1)}{row}"].Value = sales;
col++;
}
row++;
}
// Add totals using Excel formulas
pivotSheet[$"A{row}"].Value = "Total"; // grand totals
for (int c = 2; c <= regions.Count() + 1; c++)
{
pivotSheet[$"{(char)('A' + c - 1)}{row}"].Formula = $"=SUM({(char)('A' + c - 1)}2:{(char)('A' + c - 1)}{row - 1})";
}
// Proceeding to apply formatting
var dataRange = pivotSheet[$"B2:{(char)('A' + regions.Count())}{row}"];
dataRange.FormatString = "$#,##0.00";
workbook.SaveAs("PivotReport.xlsx");
}
}using IronXL;
using System.Linq;
using System.Data; // Keep this namespace
using static System.Data.DataTableExtensions; // Use 'using static' for DataTableExtensions
class Program
{
static void Main(string[] args)
{
// Load Excel file - no Office required
WorkBook workbook = WorkBook.Load("SalesData.xlsx");
WorkSheet dataSheet = workbook.WorkSheets[0];
// Convert to DataTable for powerful manipulation
var dataTable = dataSheet.ToDataTable(true); // true = use first row as column headers
// Create pivot-style aggregation using LINQ
var pivotData = dataTable.AsEnumerable()
.GroupBy(row => new {
Product = row["Product"].ToString(),
Region = row["Region"].ToString()
}) //range
.Select(g => new {
Product = g.Key.Product,
Region = g.Key.Region,
TotalSales = g.Sum(row => Convert.ToDecimal(row["Sales"])),
AverageSale = g.Average(row => Convert.ToDecimal(row["Sales"])),
Count = g.Count()
});
// Create pivot report worksheet
WorkSheet pivotSheet = workbook.CreateWorkSheet("PivotReport");
// Build cross-tabulation structure
var products = pivotData.Select(p => p.Product).Distinct().OrderBy(p => p);
var regions = pivotData.Select(p => p.Region).Distinct().OrderBy(r => r);
// Create headers
pivotSheet["A1"].Value = "Product/Region";
int col = 2;
foreach (var region in regions)
{
pivotSheet[$"{(char)('A' + col - 1)}1"].Value = region; // string
col++;
}
// Populate pivot data
int row = 2;
foreach (var product in products)
{
pivotSheet[$"A{row}"].Value = product;
col = 2;
foreach (var region in regions)
{
var sales = pivotData
.Where(p => p.Product == product && p.Region == region)
.Select(p => p.TotalSales)
.FirstOrDefault();
pivotSheet[$"{(char)('A' + col - 1)}{row}"].Value = sales;
col++;
}
row++;
}
// Add totals using Excel formulas
pivotSheet[$"A{row}"].Value = "Total"; // grand totals
for (int c = 2; c <= regions.Count() + 1; c++)
{
pivotSheet[$"{(char)('A' + c - 1)}{row}"].Formula = $"=SUM({(char)('A' + c - 1)}2:{(char)('A' + c - 1)}{row - 1})";
}
// Proceeding to apply formatting
var dataRange = pivotSheet[$"B2:{(char)('A' + regions.Count())}{row}"];
dataRange.FormatString = "$#,##0.00";
workbook.SaveAs("PivotReport.xlsx");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.com所以,這就是2025年創建資料透視表的方法。
輸出
如何使用 C# Interop 與 IronXL 在 Excel 中建立資料透視表:圖 1 - 原始 Excel 檔案與我們建立的資料透視表,顯示了儲存格。
如何使用 IronXL 公式建立動態匯總?
對於需要動態更新(類似於建立資料透視表)的場景,IronXL 可以利用 Excel 的內建公式來計算資料透視表刷新功能,從而獲得答案。 一般來說,這比之前的答案更好——您的記錄將以更現代、更優雅的方式處理,使用的程式碼也更容易理解。 設定簡單,無需聯絡客服或閱讀手冊。
// Load the workbook
WorkBook workbook = WorkBook.Load(inputPath);
// Rename the first worksheet so formulas reference correctly
WorkSheet dataSheet = workbook.WorkSheets[0];
dataSheet.Name = "DataSheet";
// Convert worksheet to DataTable
DataTable dataTable = dataSheet.ToDataTable(true);
// Create new summary worksheet
WorkSheet summarySheet = workbook.CreateWorkSheet("DynamicSummary");
// Get unique product-region combinations
var uniqueCombos = dataTable.AsEnumerable()
.Select(row => new {
Product = row["Product"].ToString(),
Region = row["Region"].ToString()
})
.Distinct()
.OrderBy(x => x.Product)
.ThenBy(x => x.Region);
// Add header row
summarySheet["A1"].Value = "Product";
summarySheet["B1"].Value = "Region";
summarySheet["C1"].Value = "Total Sales";
summarySheet["D1"].Value = "Count";
// Populate rows with formulas
int rowIndex = 2;
foreach (var combo in uniqueCombos)
{
summarySheet[$"A{rowIndex}"].Value = combo.Product;
summarySheet[$"B{rowIndex}"].Value = combo.Region;
// Adjust column references if your Sales column is C (not D)
summarySheet[$"C{rowIndex}"].Formula =
$"=SUMIFS(DataSheet!C:C,DataSheet!A:A,\"{combo.Product}\",DataSheet!B:B,\"{combo.Region}\")";
summarySheet[$"D{rowIndex}"].Formula =
$"=COUNTIFS(DataSheet!A:A,\"{combo.Product}\",DataSheet!B:B,\"{combo.Region}\")";
rowIndex++;
}
// Optional: add total row
summarySheet[$"A{rowIndex}"].Value = "Total";
summarySheet[$"C{rowIndex}"].Formula = $"=SUM(C2:C{rowIndex - 1})";
summarySheet[$"D{rowIndex}"].Formula = $"=SUM(D2:D{rowIndex - 1})";
// Save output file
workbook.SaveAs(outputPath); //filename// Load the workbook
WorkBook workbook = WorkBook.Load(inputPath);
// Rename the first worksheet so formulas reference correctly
WorkSheet dataSheet = workbook.WorkSheets[0];
dataSheet.Name = "DataSheet";
// Convert worksheet to DataTable
DataTable dataTable = dataSheet.ToDataTable(true);
// Create new summary worksheet
WorkSheet summarySheet = workbook.CreateWorkSheet("DynamicSummary");
// Get unique product-region combinations
var uniqueCombos = dataTable.AsEnumerable()
.Select(row => new {
Product = row["Product"].ToString(),
Region = row["Region"].ToString()
})
.Distinct()
.OrderBy(x => x.Product)
.ThenBy(x => x.Region);
// Add header row
summarySheet["A1"].Value = "Product";
summarySheet["B1"].Value = "Region";
summarySheet["C1"].Value = "Total Sales";
summarySheet["D1"].Value = "Count";
// Populate rows with formulas
int rowIndex = 2;
foreach (var combo in uniqueCombos)
{
summarySheet[$"A{rowIndex}"].Value = combo.Product;
summarySheet[$"B{rowIndex}"].Value = combo.Region;
// Adjust column references if your Sales column is C (not D)
summarySheet[$"C{rowIndex}"].Formula =
$"=SUMIFS(DataSheet!C:C,DataSheet!A:A,\"{combo.Product}\",DataSheet!B:B,\"{combo.Region}\")";
summarySheet[$"D{rowIndex}"].Formula =
$"=COUNTIFS(DataSheet!A:A,\"{combo.Product}\",DataSheet!B:B,\"{combo.Region}\")";
rowIndex++;
}
// Optional: add total row
summarySheet[$"A{rowIndex}"].Value = "Total";
summarySheet[$"C{rowIndex}"].Formula = $"=SUM(C2:C{rowIndex - 1})";
summarySheet[$"D{rowIndex}"].Formula = $"=SUM(D2:D{rowIndex - 1})";
// Save output file
workbook.SaveAs(outputPath); //filenameIRON VB CONVERTER ERROR developers@ironsoftware.com這些公式與來源資料保持即時連接,當資料表變更時自動更新—類似於資料透視表刷新行為,但沒有互通依賴性。
如果我們將這段程式碼應用到上一個範例中的範例 Excel 檔案(XLS / XLSX),頁面上將顯示以下欄位:
如何在 Excel 中使用 C# Interop 與 IronXL 建立資料透視表:圖 2 - 注意:C# 動態匯總輸出在位置上下文中可見。
比較 C# Interop 和 IronXL 在資料透視表方面的效能
| 方面 | C# 互通 | IronXL |
|---|---|---|
| 辦公室要求 | 是的 - 完全安裝 | 否 - 獨立庫 |
| 平台支援 | 僅限 Windows 系統 | Windows、Linux、macOS、Docker |
| 記憶體管理 | 需要手動清理 COM 對象 | 自動 .NET 垃圾回收 |
| 部署 | 複雜 - 辦公室許可 | 簡單 - 單一 DLL |
| 表現 | Excel進程啟動緩慢 | 快速記憶體計算 |
| 雲端相容 | 否 - Azure 限制 | 是的 - 支援 Azure Functions |
| 原生透視表 | 是的 | 無 - 聚合替代方案 |
| 發展速度 | 速度慢 - COM 複雜性 | 快速直覺的 API |
你該選擇哪一種方法?
選擇 C# 互通的條件:
- 必須使用原生 Excel 資料透視表對象
- 僅在 Windows 系統上開發,且每台目標機器都已安裝 Office 辦公室軟體。
- 僅部署到您管理的桌面系統
- 現有遺留程式碼依賴互通性
- 使用舊版 .NET Framework
選擇 IronXL 的情況:
- 部署到伺服器或雲端環境和服務(Azure、AWS 等) 建構跨平台應用程式
- 要求更高的性能和可靠性
- 避免辦公室軟體授權費用 需要更簡潔、更容易維護的程式碼
- 支援 Mac、iOS、Android 和/或 Linux 系統
- 熟悉現代 .NET Framework Core 和 .NET 5、6、7、8、9、10。
- 您需要在開發控制台/IDE 中擁有完全存取權限,以便根據資料範圍以程式設計方式配置透視表中每個欄位的設定。
結論
雖然 C# Excel Interop 可以開發原生透視表,但其部署限制和複雜性使其在現代應用程式中越來越不實用。 IronXL透過資料聚合和基於公式的匯總提供強大的替代方案,消除了對 Office 的依賴,同時保持了分析能力。 如果本文對您有幫助,請投票、評論或訂閱。
對於正在尋找無需 Interop 即可開發透視表的替代方案的開發人員來說,IronXL 提供了一種更優的途徑,避免了 COM 的複雜性,可在所有平台上運行,並簡化了部署。 雖然沒有原生透視對象,但更大的靈活性、更好的性能以及無需 Office 許可的要求彌補了這一不足。
!{--01001100010010010100001001010010010000010101001001011001010111110100011101000101010101 01000101111101010011010101000100000101010010010101000100010101000100010111110101011101001000110 1010101000100100001011111010100000101001001001111010001000101010101010000110101010100101010101011 10101010001010010010010010010000010100110001011111010000100100110001001111101000011010010111111010000110100101110--
準備好讓您的 Excel 自動化流程現代化,並使用現代 C# 建立您自己的資料透視表程式碼了嗎?
透過 NuGet 套件管理器,您可以在幾秒鐘內將 IronXL 整合到您的 C# 應用程式中。 免費試用或購買 IronXL 授權,以消除生產應用程式中的 Interop 相依性。
常見問題解答
使用 IronXL 在 Excel 中建立資料透視表有何好處?
IronXL 可讓您在 Excel 中建立資料透視表,而不需要依賴 Office,相較於傳統的 C# Interop 方法,IronXL 是更精簡、更有效率的解決方案。
IronXL 如何處理透視表的資料處理?
IronXL.Excel 提供強大的資料處理功能,可建立透視式報表,而不會產生與 Excel Interop 相關的複雜問題。
IronXL 可以獨立於 Excel Interop 使用嗎?
是的,IronXL.Excel 可獨立運作,讓開發人員無需依賴 Excel Interop 及其相關的相依性即可產生資料透視表。
為何開發人員偏好 IronXL,而非 Excel 的傳統 Interop 方法?
開發人員之所以偏愛 IronXL,是因為它省去了傳統 Interop 方法所需的 Office 相依性,簡化了建立資料透視表的流程。
使用 IronXL 需要安裝 Microsoft Office 嗎?
不需要,IronXL 不需要安裝 Microsoft Office,因為它獨立於 Office 運作,不像 C# Interop 需要 Office 的依賴。
IronXL 是否與現代 C# 程式設計相容?
是的,IronXL.Excel 旨在與現代 C# 程式設計無縫整合,提供處理 Excel 資料處理任務的當代方法。









