Hi,
My requirment is to open the excel sheet with pivot table even before saving it.
Other words I should be able to open the file when file download prompts to open or save the file.
When I open the file directly without saving it, throws an error saying cannot open pivotTable source file C:\.....test[1].xls
One more thing. The below line gives the sum but not the count
pivotTable.DataFields[0].AddCalculatedItem("CountSurveyID", "=COUNT('SurveyID')");
Please see the code below
public void CreateStaticReport()
{
Workbook wBook;
Worksheet wSheet;
string path = System.Web.HttpContext.Current.Server.MapPath("~");
path += @"\ExcelTemplates\Test.xls";
System.IO.StreamReader s = new System.IO.StreamReader(path);
wBook = new Workbook(s.BaseStream);
wSheet = wBook.Worksheets[4];
LocalBuilder.Reset();
LocalBuilder.StoredProcedureName = "abc";
DataTable dt = LocalBuilder.GetDataTable();
int rowCount = 1;
int colCount = 0;
foreach (DataRow dr in dt.Rows)
{
colCount = 0;
wSheet.Cells[rowCount, colCount++].PutValue(dr["SurveyID"]);
wSheet.Cells[rowCount, colCount++].PutValue(dr["Stage"]);
wSheet.Cells[rowCount, colCount++].PutValue(dr["ARD"].ToString());
wSheet.Cells[rowCount, colCount++].PutValue(dr["CNPartner"].ToString());
wSheet.Cells[rowCount, colCount++].PutValue(dr["PML"]);
rowCount++;
}
Worksheet wSheet1 = wBook.Worksheets[1];
Aspose.Cells.Pivot.PivotTableCollection pivotTables = wSheet1.PivotTables;
int index = pivotTables.Add("=Detail!A1:Q"+rowCount, "B3", "PivotTable1");
Aspose.Cells.Pivot.PivotTable pivotTable = pivotTables[index];
pivotTable.RowGrand = true;
pivotTable.ColumnGrand = true;
pivotTable.IsAutoFormat = true;
pivotTable.AutoFormatType = Aspose.Cells.Pivot.PivotTableAutoFormatType.Report6;
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, "PML");
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, "ARD");
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Column, "Stage");
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data,"SurveyID");
pivotTable.DataFields[0].AddCalculatedItem("CountSurveyID", "=COUNT('SurveyID')");
this.Response.Clear();
this.Response.AddHeader("content-disposition", "attachment;filename=Test.xls");
this.Response.ContentType = "application/excel";
wBook.Save(this.Response.OutputStream, SaveFormat.Excel97To2003);
}
protected void lbl_Click(object sender, EventArgs e)
{
CreateStaticReport();
}