Home » excel » c# – Can't save Excel sheet after adding data if the workbook contains a Pivot table

c# – Can't save Excel sheet after adding data if the workbook contains a Pivot table

Posted by: admin March 7, 2020 Leave a comment


We’re trying to build a default Excel dashboard which our users can download. When downloading the Excel sheet, we want to populate a sheet in the file with their data.

We’re using EPPlus for the manipulation of the Excel sheet.

In short, the structure of the sheet is as follows:

  1. Sheet A with reporting elements (pivot tables, pivot charts and slicers)
  2. Sheet B which contains the entire data set
  3. A named range defined as =OFFSET(Data!$A$1;0;0;COUNTA(Data!$A:$A);COUNTA(Data!$1:$1)) which simply adapts to the size of the dataset we insert into that sheet
  4. Whatever the named range contains i loaded into Power Query and added to the workbooks Data Model
  5. All the reporting elements from Sheet A is configured to load data from the Data Model

Overall, this works great as long as we’re manually stuffing Sheet B with our data. When we try to use EPPlus for filling in the data we get an error when trying to save the file:

The cachesource is not a worksheet

By trial and error, we’ve stripped the sheet of it’s parts to isolate the cause of the problem. We suspected that it might have been the slicers, the use of Power Query/Data Model or the trick with the named range. However, none of these looks to be the issue – if we remove all Pivot Tables from the sheet then we’re able save the Workbook just fine. Surprising to me, we’re able to use the Pivot Charts just fine, it’s only the tables which are causing the issue.

Any suggestions as to how to avoid this problem with EPPlus? For now, we’ve continued without the use of Pivot Tables would we would like to have them return at some point 🙂

How to&Answers:

Error – “The cachesource is not a worksheet” can be due to recognition of source data in range by Excel as range in worksheet.

You can change the dumped data in the range into Sheet B as a table, using ws.ListObjects.Add. Whenever pivot table is refreshed, it should automatically takes all the data within the table.

Please also check another alternative available from a related question -Defining a table rather than a range as a PivotTable 'cacheSource', if that helps.


There are many issues related with cache when manipulating Pivot data. Once I had a look at it for this other bounty here.

In must be the same with EPPlus. You have to handle the Pivot data cache manually, like done here.

And here the guy gives complete solution for handling filters with EPPlus.

Hope it helps.