简体   繁体   中英

How to update Excel PIvot Table data using C#

let me outline my requirement. I have an excel spreadsheet with multiple pivot tables ( linked to charts / slicers etc ) and 2 worksheets with the data that those pivot tables refer to. Currently I have to manually execute a SQL query, copy the data, paste it over the current data in the spreadsheet and then refresh the pivot tables every day.

This is sub-optimal at best. So what I am trying to achieve is some C# code that I can execute on a schedule.

Using EPPlus, I have managed to load the excel file as a template, create a new one, get the data from SQL, update the 2 datasheets with the new data and then save the file.

using (var templateStream = new MemoryStream(File.ReadAllBytes(@"PATH_TO_TEMPLATE_FILE")))
{
    using (var newStream = new MemoryStream())
    {
        //Create e NEW excel doc from the given template
        using (ExcelPackage excelPackage = new ExcelPackage(newStream, templateStream))
        {
            //load the data from SQL
            DataSet data = LoadDatasetFromQuery(configs, QueueItem);
            //loop over the DataTables inside the DataSet
            for (int i = 1; i <= data.Tables.Count; i++)
            {
                //Resolve the worksheet to put the data on
                var worksheetName = configs.FirstOrDefault(c => c.Name.StartsWith($"Worksheet.{i}."));
                ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[worksheetName.Value];
                //Put the data on the worksheet top/left = B3
                worksheet.Cells["B3"].LoadFromDataTable(data.Tables[i - 1], false);
            }
            //Save the file to the memory stream
            excelPackage.Save();
        }

        //Write the file to the file system
        File.WriteAllBytes(@"PATH_TO_OUTPUT_FILE", newStream.ToArray());
    }
}

The problem is, when I try and open the excel file, it says it is corrupt and tries to repair it, which is does, by removing the pivot tables completely. My template file makes use of named ranges as referred to in this SO post but that has not resolved the issue.

Herewith the excel log of how it completed the "repair"

在此处输入图像描述

I have also dabbled a little bit in using the interop library ( Microsoft.Office.Interop.Excel ) but that is really like a black hole when it comes to debugging / documentation etc. I'm not averse to using it, I just don't know how. ( well nothing I have tried works properly anyways )

Any help with the above will be greatly appreciated. If you need more information, feel free to ask.

Ok, so it seems my above code was correct, but the excel template I was loading was dodgy. In order to correct the issue I had to make sure that all the pivot tables used named ranges to refer to the data ( click anywhere on the pivot table, then click on the Formulas tab in the top ribbon and then click on Name Manager ) source and then use the offset calculation ( to enable a dynamic range ) as suggested in the link in my post above.

=OFFSET(DataSource,$A$1,0,0:COUNTA(DataSource,$A:$A),COUNTA(DataSource!$1:$1))

where DataSource = the name of the worksheet with the data

Finally, I set up the pivots to refresh their data on opening ( right click on the pivot table, go to data tab and tick the "refresh on open" option )

There is a bit of a pain in that when I open the generated doc it is in "Protected mode" so the data + calcs dont refresh, but if I just click "Enable Editing" it all updates and normal service is resumed, happy days!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM