I have a template excel file that I fill data into from SQL Server OLEDB connector. The file contains some pivot tables that refer to the dataset being filled in by the database.
Currently what I do is that I select all rows in sheet by using "Data!$A:$K" range. This brings a problem with blank values being shown in the pivot table.
What I wanted to do is to create a named table over the dataset and refer the pivot tables to that (plus I gain some other advantages that names tables bring).
The number of rows is naturally not set so I wanted to find a way to set the named range scope to the actual values only.
I'm using Excel Interop and C# for that and I can't find a way to change the range. I only got as far as:
oRng = oSheet.get_Range("Transactions");
Which selects the named range. But how do I change which cells belong to it?
Or is there perhaps a better solution I should pursue?
EDIT
Dynamic ranges are the answer!
I solved this thanks to @TimWilliams reply:
"Use a dynamic range in your template: http://ozgrid.com/Excel/DynamicRanges.htm"
I feel the dynamic ranges are better described here: http://www.contextures.com/xlpivot01.html
I encountered a slight issue that I couldn't use the range in pivot table because it demanded it needs at least 2 rows to operate - the template file only has column headings. I added a random string to 1st cell of 2nd row and pivot table accepted that.
Afterwards I had to remove that row using c# code.
Thank you guys for help.