ClosedXML / ClosedXML/ClosedXML
Corrupted pivot tables
- Dominant language
- C#
- Stars
- 5.7k
- Forks
- 933
- Avg merge
- 12h 14m
- Merged PRs (30d)
- 1
Description
## Read and complete the full issue template
**Do you want to request a *feature* or report a *bug*?**
- [x] Bug
- [ ] Feature
If you are logging a possible bug or feature request, please test with [the latest development build](https://ci.appveyor.com/project/ClosedXML/ClosedXML/branch/develop/artifacts) first.
**Version of ClosedXML**
Starting from 0.93
**What is the current behavior?**
When pre created pivot table sources, the file becomes corrupt and excel removes the pivot tables completely. This still works in v0.92.1. When opening the file, there is an error 👍
Removed Feature: PivotTable report from /xl/pivotCache/pivotCacheDefinition1.xml part (PivotTable cache)
## Reproducibility
**This is an important section. Read it carefully. Failure to do so will cause a 'RTFM' comment.**
**Code to reproduce problem:**
I am using an excel sheet with pivot tables referencing sheets with tables, using ClosedXML I want to populate the source tables with data and update the pivot. The code is very simple
```csharp
using (var workbook = new XLWorkbook(fileTemplate, XLEventTracking.Enabled))
{
var metricsSheet = workbook.Worksheet("Metrics");
var metricTable = metricsSheet.Tables.First();
//metricTable.ReplaceData(metricsArray);
metricTable.AppendData(metricsArray);
metricsSheet.Columns().AdjustToContents();
var metricsRange = metricTable.AsRange();
//var outputsSheet = workbook.Worksheet("Output");
//var outputTable = outputsSheet.Tables.First();
//outputTable.AppendData(assignments);
//outputsSheet.Columns().AdjustToContents();
//foreach (var ws in workbook.Worksheets)
//{
// if (ws.PivotTables == null || !ws.PivotTables.Any()) continue;
// var pivot = ws.PivotTables.First();
// pivot.SourceRange = metricsRange;
//}
Stream fs = new MemoryStream();
workbook.SaveAs(fs);
fs.Position = 0;
return await Task.FromResult(fs);
}
```
[auto_pivot_pre_open.xlsx](https://github.com/ClosedXML/ClosedXML/files/4079975/auto_pivot_pre_open.xlsx)
- [x] I attached a sample spreadsheet. (You can drag files on to this issue)
Contributor guide
Research direction
Reproduce the corruption with auto_pivot_pre_open.xlsx using the shown XLWorkbook, AppendData, and SaveAs flow; first compare the result with ClosedXML 0.92.1 and the newer version. Inspect how the existing pivot table and pivot cache are preserved when the source table is populated. Done means Excel opens the saved workbook without removing the pivot tables.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- data, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100