我有以下示例原始数据表:
并希望使用 C# 和 Microsoft.Office.Interop.Excel 库制作以下数据透视表:
我编写了以下代码来生成原始表和数据透视表:
string fileTest = "test.xlsx";
Excel.Application app;
Excel.Worksheet sheet;
Excel.Workbook book;
app = new Excel.Application();
book = app.Workbooks.Add();
sheet = (Excel.Worksheet)book.Worksheets.get_Item(1);
sheet.Cells[1, 1] = "Name";
sheet.Cells[1, 2] = "Salary";
sheet.Cells[1, 3] = "Hours";
sheet.Cells[1, 4] = "Department";
sheet.Cells[2, 1] = "Bill";
sheet.Cells[2, 2] = 140000;
sheet.Cells[2, 3] = 40;
sheet.Cells[2, 4] = "IT";
sheet.Cells[3, 1] = "Ann";
sheet.Cells[3, 2] = 310000;
sheet.Cells[3, 3] = 60;
sheet.Cells[3, 4] = "Payroll";
sheet.Cells[4, 1] = "Bob";
sheet.Cells[4, 2] = 200000;
sheet.Cells[4, 3] = 50;
sheet.Cells[4, 4] = "IT";
sheet.Cells[5, 1] = "Sam";
sheet.Cells[5, 2] = 50000;
sheet.Cells[5, 3] = 20;
sheet.Cells[5, 4] = "Payroll";
Excel.Range range = sheet.Range["A1", "D5"];
Excel.Worksheet pivotSheet = (Excel.Worksheet)book.Worksheets.Add();
pivotSheet.Name = "Pivot Table";
Excel.Range range2 = pivotSheet.Cells[1, 1];
Excel.PivotCache pivotCache = (Excel.PivotCache)book.PivotCaches().Add(Excel.XlPivotTableSourceType.xlDatabase, range);
Excel.PivotTable pivotTable = (Excel.PivotTable)pivotSheet.PivotTables().Add(PivotCache: pivotCache, TableDestination: range2, TableName: "Summary");
Excel.PivotField pivotField = (Excel.PivotField)pivotTable.PivotFields("Salary");
pivotField.Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pivotField.Function = Excel.XlConsolidationFunction.xlSum;
Excel.PivotField pivotField3 = (Excel.PivotField)pivotTable.PivotFields("Hours");
pivotField3.Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pivotField3.Function = Excel.XlConsolidationFunction.xlSum;
Excel.PivotField pivotField2 = (Excel.PivotField)pivotTable.PivotFields("Department");
pivotField2.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
Excel.PivotField dataField = pivotTable.DataPivotField;
dataField.Orientation = Excel.XlPivotFieldOrientation.xlColumnField;
pivotTable.TableStyle2 = "PivotStyleLight16";
book.SaveAs(fileTest);
book.Close();
app.Quit();
但是,我的数据透视表在数据透视表标题中多了一行,其中包含“数据”一词:
有没有办法删除这个额外的标题?感谢您的任何建议。