Hi, I am trying to update the chart data using following code, I don't know what I am missing. When I open the saved presentation, i do not see the updated data. Please help, following is code
PresentationEx objPre = new PresentationEx (@"C:\v1.pptx") ;
//PresentationEx objPre = new PresentationEx(sPath);
try{
if (objPre.Slides.Count > 0){
SlideEx chartSlide = objPre.Slides[3];
foreach (Aspose.Slides.Pptx.ShapeEx chart in chartSlide.Shapes){
if (chart.GetType().FullName == "Aspose.Slides.Pptx.OleObjectFrameEx"){
((Aspose.Slides.Pptx.OleObjectFrameEx)chart).ObjectData = GetChartData();
}
}
//objPre.Slides.InsertClone(4, chartSlide);
//objPre.Slides.RemoveAt(3);
}
}
catch (Exception ex)
{
}
finally
{
objPre.Save(@"C:\v1.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
//Get Chart data function is as
static byte[] GetChartData()
{
//Create a workbook
Workbook wb = new Workbook();
//Add an excel chart
int chartRows = int.Parse("55");// 55 this.txtRows.Text;
int chartCols = int.Parse("25");//25 this.txtCols.Text;
int chartSheetIndex = AddExcelChartInWorkbook(wb, chartRows, chartCols);
//Set the OLE size of the chart
wb.Worksheets.SetOleSize(0, chartRows, 0, chartCols);
//Save the workbook to stream
MemoryStream wbStream1 = wb.SaveToStream();
byte[] chartOleData = new byte[wbStream1.Length];
wbStream1.Position = 0;
wbStream1.Read(chartOleData, 0, chartOleData.Length);
return chartOleData;
}
and AddExcelChartInExcelWorkbook is as follows
static int AddExcelChartInWorkbook(Workbook wb, int chartRows, int chartCols)
{
//Add a new worksheet to populate cells with data
int dataSheetIdx = wb.Worksheets.Add();
Worksheet dataSheet = wb.Worksheets[dataSheetIdx];
string sheetName = "DataSheet";
dataSheet.Name = sheetName;
string[][] strData = new string[2][];
strData[0] = new string[] { "Dec-08", "Jan-09", "Feb-09", "Mar-09", "Apr-09", "May-09", "Jun-09", "Jul-09", "Aug-09", "Sep-09", "Oct-09", "Nov-09", "Dec-09", "Jan-10" };
strData[1] = new string[] { "4500", "9000", "5020", "5040", "5060", "5080", "5100", "5120", "5140", "5160", "6000", "6500", "7000", "7200" };
for (int i = 0; i < 2; i++)
for (int j = 0; j < 14; j++)
{
try
{
dataSheet.Cells[i, j].PutValue(int.Parse(strData[i][j]));
}
catch (Exception ex)
{
//string str = ex.Message;
dataSheet.Cells[i, j].PutValue(strData[i][j]);
}
//Response.Write(strData[i][j] + "
");
}
//Add a chart sheet
int chartSheetIdx = wb.Worksheets.Add(SheetType.Chart);
Worksheet chartSheet = wb.Worksheets[chartSheetIdx];
chartSheet.Name = "ChartSheet";
//Add a chart in ChartSheet with data series from DataSheet
int chartIdx = 0;
foreach (string s in Enum.GetNames(typeof(ChartType)))
if (s.ToLower().CompareTo("column") == 0)
chartIdx = chartSheet.Charts.Add((ChartType)Enum.Parse(typeof(ChartType), s), 0, chartRows, 0, chartCols);
Chart chart = chartSheet.Charts[chartIdx];
//for (int rows = 1; rows <= this.GrdViewExcel.RowCount; rows++)
for (int rows = 1; rows <= 4; rows++)
chart.NSeries.Add(sheetName + "!A" + rows.ToString() + ":E" + rows.ToString(), false);
//Set ChartSheet an active sheet
wb.Worksheets.ActiveSheetIndex = chartSheetIdx;
//wb.Save("");
return chartSheetIdx;
}