Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 836
Christian A. Rasmussen Avatar asked Oct 18 '17 11:10

Christian A. Rasmussen


People also ask

Why wont Excel let me save?

If you cannot save a workbook when you run Microsoft Excel in Windows Safe mode, the problem may be caused by a third-party add-in or by a file from one of the Excel startup locations. By default, startup files are loaded when you start Excel.

How do I update a pivot table after adding data?

Click Analyze > Refresh, or press Alt+F5. Tip: You can also refresh the PivotTable by right-clicking on the PivotTable, and then selecting Refresh. To update all PivotTables in your workbook at once, click Analyze > Refresh arrow > Refresh All.

Can you add data to a pivot table after it is created?

Next to the pivot table, click Edit to open the pivot table editor. Add data—Depending on where you want to add data, under Rows, Columns, or Values, click Add. Change row or column names—Double-click a Row or Column name and enter a new name. under Order or Sort by and select the option or item.

How do I save a pivot table in Excel without underlying data?

First, select and copy the entire pivot table. You can use Control + A to select the whole table. Next, in a new worksheet, use Paste Special, then Values. This will strip away all the formatting and leave you with just the data.

How do I save a sheet in a pivot table?

Right-click a cell in the pivot table, and click PivotTable Options. On the Data tab, in the PivotTable Data section, add or remove the check mark from Save Source Data with File. Click OK.


1 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.

like image 91
IITC Avatar answered Sep 17 '22 13:09

IITC