使用C#生成Excel PivotTables怎么设置格式?在线等大神
//得到Excel
Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
workbook.LoadFromFile(@"C:\CommodityBlueInfo\Contrast SRVInfo_0617.xlsx");
Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
sheet.Name = "Contrast-1";
Spire.Xls.Worksheet sheet2 = workbook.CreateEmptySheet();
sheet2.Name = "Pivot Table";
//得到Excel中的数据
CellRange dataRange = sheet.Range["A1:F6"];
PivotCache cache = workbook.PivotCaches.Add(dataRange);
PivotTable pt = sheet2.PivotTables.Add("Pivot Table", sheet.Range["A1"], cache);
var r1 = pt.PivotFields["Subsystem"];
r1.Axis = AxisTypes.Row;
pt.Options.RowHeaderCaption = "Subsystem";
var r2 = pt.PivotFields["HW Desc"];
r2.Axis = AxisTypes.Row;
//r2.Formula = "";
pt.BuiltInStyle = PivotBuiltInStyles.PivotStyleMedium12;
workbook.SaveToFile(@"Contrast SRVInfo_0121.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start(@"Contrast SRVInfo_0121.xlsx");
调用了spire.xls_hotfix_7.0.18版本中的Spire.Xls.dll
到这里为止其实数据透视表已经出来了,但是格式太原始了.上面代码的效果如下图
我想将每一个行标签的字段设置都设置为 分类汇总(无) 以表格形式显示项目标签.如下图
C# Excel PivotTables
补充:.NET技术 , 图表区