Problem Part 2,
I'm replying to my own post but this repro is actually an example of my root problem - that charts are not created correctly in Excel 2007. I think the first part may be my issue as it seems like there are some things Aspose.Cells can't do, like for example finding existing Excel created pivot tables and I also found it was the same issue using the Cells.Ranges collection which does not seem to pick up Excel named ranges.
So this is what I'm trying to do. Creating a pivot table and attached chart using Aspose.Cells. As mentioned it works for Excel 2003 but not for Excel 2007. I have attached my two spreadsheets with only the data part in them. The project code has been revised to create the pivot table as well:
private void btnOpen2007File_Click(object sender, EventArgs e)
{
Workbook wb = new Workbook();
if (opfExample.ShowDialog(this) == DialogResult.OK)
{
wb.Open(opfExample.FileName, FileFormatType.Excel2007Xlsx);
Worksheet wsPivot = wb.Worksheets["Pivot"];
Worksheet wsData = wb.Worksheets["Data"];
Range dataRange = wsData.Cells.CreateRange(0, 0, 9, 6);
dataRange.Name = "DataRange";
int pivotIndex = wsPivot.PivotTables.Add(dataRange.Name,
0,
0,
"PivotTableAuto");
PivotTable pivot = wsPivot.PivotTables[pivotIndex];
for (int i = 0; i < pivot.BaseFields.Count; i++)
{
PivotField field = (PivotField) pivot.BaseFields[i];
field.DisplayName = field.Name;
field.IsAutoSubtotals = false;
field.ShowInOutlineForm = true;
field.ShowCompact = false;
if (pivot.RowFields.Count < 1)
{
pivot.AddFieldToArea(PivotFieldType.Row, field);
continue;
}
pivot.AddFieldToArea(PivotFieldType.Data, field);
}
pivot.AddFieldToArea(PivotFieldType.Column, pivot.DataField);
pivot.IsAutoFormat = true;
pivot.AutoFormatType = PivotTableAutoFormatType.Classic;
pivot.RowGrand = false;
pivot.DataField.ShowInOutlineForm = true;
pivot.DataField.ShowCompact = false;
int chartIx = wsPivot.Charts.Add(ChartType.Column, pivot.TableRange2.EndRow + 2, 0, pivot.TableRange2.EndRow + 20, pivot.TableRange2.EndColumn);
Chart chart = wsPivot.Charts[chartIx];
chart.PivotSource = wsPivot.Name + "!" + pivot.Name;
sfExample.FileName = "GridExport2007WithChart.xlsx";
if (sfExample.ShowDialog(this) == DialogResult.OK)
{
wb.Save(sfExample.FileName, FileFormatType.Excel2007Xlsx);
Process.Start(sfExample.FileName);
}
}
}
private void btnOpen2003File_Click(object sender, EventArgs e)
{
Workbook wb = new Workbook();
if (opfExample.ShowDialog(this) == DialogResult.OK)
{
wb.Open(opfExample.FileName, FileFormatType.Excel2003);
Worksheet wsPivot = wb.Worksheets["Pivot"];
Worksheet wsData = wb.Worksheets["Data"];
Range dataRange = wsData.Cells.CreateRange(0, 0, 9, 6);
dataRange.Name = "DataRange";
int pivotIndex = wsPivot.PivotTables.Add(dataRange.Name,
0,
0,
"PivotTableAuto");
PivotTable pivot = wsPivot.PivotTables[pivotIndex];
for (int i = 0; i < pivot.BaseFields.Count; i++)
{
PivotField field = (PivotField) pivot.BaseFields[i];
field.DisplayName = field.Name;
field.IsAutoSubtotals = false;
field.ShowInOutlineForm = true;
field.ShowCompact = false;
if (pivot.RowFields.Count < 1)
{
pivot.AddFieldToArea(PivotFieldType.Row, field);
continue;
}
pivot.AddFieldToArea(PivotFieldType.Data, field);
}
pivot.AddFieldToArea(PivotFieldType.Column, pivot.DataField);
pivot.IsAutoFormat = true;
pivot.AutoFormatType = PivotTableAutoFormatType.Classic;
pivot.RowGrand = false;
pivot.DataField.ShowInOutlineForm = true;
pivot.DataField.ShowCompact = false;
int chartIx = wsPivot.Charts.Add(ChartType.Column, pivot.TableRange2.EndRow + 2, 0, pivot.TableRange2.EndRow + 20, pivot.TableRange2.EndColumn);
Chart chart = wsPivot.Charts[chartIx];
chart.PivotSource = wsPivot.Name + "!" + pivot.Name;
sfExample.FileName = "GridExport2003WithChart.xls";
if (sfExample.ShowDialog(this) == DialogResult.OK)
{
wb.Save(sfExample.FileName, FileFormatType.Excel2003);
Process.Start(sfExample.FileName);
}
}
}
The end result is that Excel2003 creates what I want but I get an empty chart frame for Excel 2007. It seeems like it's not referencing the pivot table correctly for some reason.
Sorry about the double post here.
Bert