Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Commented Unassigned: Index of RowLabel and ColumnLabel in PivotTable [9326]

$
0
0
File XLWorkbook_Save.cs
Line 2222
Creation of RowLabels
```
var f = new Field {Index = pt.Fields.IndexOf(xlpf)};
```
Line 2240
```
var f = new Field {Index = pt.Fields.IndexOf(xlpf)};
```
The index of RowLabels and ColumnLabels is not correct because it is based on the order of Fields Collection

Is it correct changing the code in this way?
```
var f = new Field {Index = pt.RowLabels.IndexOf(pt.RowLabels.FirstOrDefault(p => p.SourceName == xlpf.SourceName))};
```
```
var f = new Field { Index = pt.ColumnLabels.IndexOf(pt.ColumnLabels.FirstOrDefault(p => p.SourceName == xlpf.SourceName)) };
```
Comments: ``` var dt = new System.Data.DataTable(); dt.Columns.Add("col1", typeof(string)); dt.Columns.Add("col2", typeof(string)); dt.Columns.Add("col3", typeof(double)); var col1 = new string[] { "col1_val1", "col1_val2", "col1_val3" }; var col2 = new string[] { "col2_val1", "col2_val2", "col2_val3" }; var rnd = new Random(); for (int i = 0; i < 10; i++) { var row = dt.NewRow(); row["col1"] = col1[rnd.Next(0, 3)]; row["col2"] = col2[rnd.Next(0, 3)]; row["col3"] = rnd.NextDouble() * rnd.Next(10, 100); dt.Rows.Add(row); } var workbook = new XLWorkbook(); var sheet = workbook.Worksheets.Add("Sheet1"); var table = sheet.Cell(1, 1).InsertTable(dt, "Table1", true); var range = table.DataRange; var header = sheet.Range(1, 1, 1, dt.Columns.Count); var dataRange = sheet.Range(header.FirstCell(), range.LastCell()); var ptSheet = workbook.Worksheets.Add("Sheet2"); var pt = ptSheet.PivotTables.AddNew("TablePivot", ptSheet.Cell(1, 1), dataRange); // COL2 then COL1 pt.RowLabels.Add("col2"); pt.RowLabels.Add("col1"); pt.Values.Add("col3"); workbook.SaveAs("workbook.xlsx"); ``` In the output excel file, the rowfields order in the pivot table is col1 first and then col2

Viewing all articles
Browse latest Browse all 1877

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>