//得到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
到这里为止其实数据透视表已经出来了,但是格式太原始了.上面代码的效果如下图
我想将每一个行标签的字段设置都设置为 分类汇总(无) 以表格形式显示项目标签.如下图
我不常逛园子,所以园豆不多,希望有知道的大神帮帮忙.
后来研究了很久,还是调用不到了spire.xls_hotfix_7.0.18版本中的Spire.Xls.dll里面更核心的代码了,毕竟是人家商业开发的,后来这个部分没做了.