问题描述
我有以下示例原始数据表:
并希望使用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();
但是,我的数据透视表在数据透视表标题中有一个额外的行,其中带有单词“ Data”:
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)