We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Problem while updating slide contents

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;

}

Dear Pravin,

I have worked with the code snippet shared by you and it works fine. Actually, the issue that one need to activate the OLE Frame by double clicking that. When you will enable the OLE Frame, the changes for new chart will take effect. Also, there is no way to programmatically enable the chart and one need to enable that manually by double clicking that.

Thanks and Regards,

Thank you Mudassir for the reply,

I made some changes to the code and it is working fine. My requirement is, I have to update the chart data in the existing presentations. For that I what I tried to do is create a empty slide and put all the contents of the old slide into the new slide with new objectData for the charts, one drawback with this approach is, it is creating the charts in new slide but the charts are old, the new data is not reflecting on the chart and the charts are not editable as well (right click, edit option). The other way I tried is to update the objectData of existing charts on the slide, in this case it allows editing the chart data once presentation is saved, and the data in the data sheet is also the new data, here the problem is the chart is not reflecting the data and when I go to edit it is not keeping the frame in original format, the excel opened is having very small fonts, when u do any modifications to the excel, and save it, it should show the original chart with changes, but the opend excel does not go away, to make it disappear we have to undo the changes.

If you want I can share the code for this too.

-

regards,

pravin

Dear Pravin,

That is really appreciable that you have managed to fix the code snippet to serve the purpose. I will really appreciate, if you could kindly share the code snippet with us for future reference.

Thanks and Regards,

Hi Mudassir,

please see the code below, also the presentations is attached in the attachment

string sPath = Directory.GetCurrentDirectory().Replace("\\bin\\Debug", "");

PresentationEx objPre = new PresentationEx(sPath + "\\Presentation1.pptx");

try {

if (objPre.Slides.Count > 0){

SlideEx chartSlide = objPre.Slides[0];

//objPre.Slides.InsertClone(4, chartSlide);

//int iSlideIndex = objPre.Slides.AddClone(chartSlide);

int iSlideIndex = objPre.Slides.AddEmptySlide(objPre.LayoutSlides[1]);

SlideEx newSlide = objPre.Slides[iSlideIndex];

foreach (Aspose.Slides.Pptx.ShapeEx chart in chartSlide.Shapes){

if (chart.GetType().FullName == "Aspose.Slides.Pptx.OleObjectFrameEx"){

Aspose.Slides.Pptx.OleObjectFrameEx oldChart = ((Aspose.Slides.Pptx.OleObjectFrameEx)chart);

OleObjectFrameEx ooFrame = null;

oldChart.ObjectData = GetChartData();

//ooFrame = newSlide.Shapes.AddOleObjectFrame(oldChart.X, oldChart.Y, oldChart.Width, oldChart.Height, "Excel.Sheet.12", GetChartData()); ;

//ooFrame.Image = objPre.Images.AddImage(((ImageEx)oldChart.Image));

}

}

}

}

catch (Exception ex)

{

}

finally

{

objPre.Save(sPath + "\\Presentation1.pptx", Aspose.Slides.Export.SaveFormat.Pptx);

}

function GetChartData() -

static byte[] GetChartData()

{

//Create a workbook

Workbook wb = new Workbook();

//Add an excel chart

int chartRows = int.Parse("55");

int chartCols = int.Parse("25");

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();

wb.Save(wbStream1, FileFormatType.Excel2007Xlsx);

byte[] chartOleData = new byte[wbStream1.Length];

wbStream1.Position = 0;

wbStream1.Read(chartOleData, 0, chartOleData.Length);

return chartOleData;

}

function AddExcelChartInWorkbook

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];

chart.NSeries.Add(sheetName + "!A2:N2", false);

chart.NSeries.CategoryData = sheetName + "!A1:N1";

chart.ShowLegend = false;

//Set ChartSheet an active sheet

wb.Worksheets.ActiveSheetIndex = chartSheetIdx;

return chartSheetIdx;

}

Attached presentations contains three slides, the first is the source slide and third, is created through above code (uncomment two lines in the main function). The charts in the third slide are not editable, also they are not reflecting the correct data bound to the charts, am I missing some settings or code? The first slide I added for your reference so that you would get the original data if you want to try something. The second slide is the slide I edited using above code. I accessed the charts on the slides and try to update chart data using above code, It allows editing the chart data, but the problem is the workbook is not clearly visible, the fonts are very small, also the chart displayed on the chart sheet is haveing different colors though I assigned the image from original slide. The data sheet in the workbook represents the correct data but is not reflected on the original chart displayed on the slide, the chart in the chart sheet represents correct data. Also double clicking the chart manually is not refreshing the chart.