C#操作Excel数据透视表需要使用Microsoft.Office.Interop.Excel命名空间,以下是详细攻略:
-
安装Microsoft Excel:在操作Excel之前,必须安装Microsoft Excel软件。如果已经安装则可以跳过该步骤。
-
添加引用:右击项目,选择“添加”->“引用”,在弹出的对话框中选择“COM”标签页,找到Microsoft Excel,勾选并点击“确定”。
-
创建Excel应用程序对象:使用Microsoft.Office.Interop.Excel.Application类,创建Excel应用程序对象,并设置Visible属性为true,以便于调试和查看结果。
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
- 创建Excel工作簿和工作表:使用Application.Workbooks.Add方法创建Excel工作簿,并使用Workbook.Worksheets.Add方法创建Excel工作表。
Excel.Workbook workbook = excelApp.Workbooks.Add();
Excel.Worksheet worksheet = workbook.Worksheets.Add();
- 添加数据:向工作表添加数据,数据可以是一些任意类型的对象。
worksheet.Cells[1, 1] = "Name";
worksheet.Cells[1, 2] = "City";
worksheet.Cells[2, 1] = "Alice";
worksheet.Cells[2, 2] = "New York";
worksheet.Cells[3, 1] = "Bob";
worksheet.Cells[3, 2] = "Chicago";
- 创建数据透视表:使用Worksheet.PivotTableWizard方法创建数据透视表,并设置数据源、行标签、列标签和数据字段。
// 设置数据源
Excel.Range dataRange = worksheet.UsedRange;
Excel.PivotTable pivotTable = worksheet.PivotTableWizard(dataRange, "PivotTable1");
// 设置行标签
Excel.PivotField rowField = pivotTable.PivotFields("City");
rowField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
// 设置列标签
Excel.PivotField columnField = pivotTable.PivotFields("Name");
columnField.Orientation = Excel.XlPivotFieldOrientation.xlColumnField;
// 设置数据字段
Excel.PivotField dataField = pivotTable.PivotFields("Name");
dataField.Orientation = Excel.XlPivotFieldOrientation.xlDataField;
dataField.Function = Excel.XlConsolidationFunction.xlCount;
- 刷新数据透视表:使用PivotTable.RefreshTable方法刷新数据透视表。
pivotTable.RefreshTable();
示例1:生成Excel数据透视表并保存为Excel文件
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = false;
Excel.Workbook workbook = excelApp.Workbooks.Add();
Excel.Worksheet worksheet = workbook.Worksheets.Add();
// 添加数据
worksheet.Cells[1, 1] = "Name";
worksheet.Cells[1, 2] = "City";
worksheet.Cells[2, 1] = "Alice";
worksheet.Cells[2, 2] = "New York";
worksheet.Cells[3, 1] = "Bob";
worksheet.Cells[3, 2] = "Chicago";
// 创建数据透视表
Excel.Range dataRange = worksheet.UsedRange;
Excel.PivotTable pivotTable = worksheet.PivotTableWizard(dataRange, "PivotTable1");
Excel.PivotField rowField = pivotTable.PivotFields("City");
rowField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
Excel.PivotField columnField = pivotTable.PivotFields("Name");
columnField.Orientation = Excel.XlPivotFieldOrientation.xlColumnField;
Excel.PivotField dataField = pivotTable.PivotFields("Name");
dataField.Orientation = Excel.XlPivotFieldOrientation.xlDataField;
dataField.Function = Excel.XlConsolidationFunction.xlCount;
// 刷新数据透视表
pivotTable.RefreshTable();
// 保存Excel文件
workbook.SaveAs("PivotTable.xlsx");
// 关闭Excel应用程序
workbook.Close();
excelApp.Quit();
示例2:获取Excel数据透视表的数据
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = false;
Excel.Workbook workbook = excelApp.Workbooks.Open("PivotTable.xlsx");
Excel.Worksheet worksheet = workbook.Worksheets[1];
Excel.Range pivotDataRange = worksheet.PivotTables("PivotTable1").TableRange1;
// 获取数据透视表的所有数据
object[,] pivotData = pivotDataRange.Value2;
// 遍历数据透视表的所有数据
for (int i = 1; i <= pivotData.GetLength(0); i++)
{
for (int j = 1; j <= pivotData.GetLength(1); j++)
{
Console.Write(pivotData[i, j] + "\t");
}
Console.WriteLine();
}
// 关闭Excel应用程序
workbook.Close();
excelApp.Quit();
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#如何操作Excel数据透视表 - Python技术站