HI WELCOME TO KANSIRIS

Creating Powerpoint Slide with charts in ASP.NET(C#)

Leave a Comment

using System;

using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Graph;
using Microsoft.Office.Core;

public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

bool bAssistantOn;
Microsoft.Office.Interop.PowerPoint.Application objApp;
Presentations objPresSet;
_Presentation objPres;
Slides objSlides;
_Slide objSlide;
TextRange objTextRng;
Microsoft.Office.Interop.PowerPoint.Shapes objShapes;
Microsoft.Office.Interop.PowerPoint.Shape objShape;
SlideShowWindows objSSWs;
SlideShowTransition objSST;
SlideShowSettings objSSS;
SlideRange objSldRng;
Microsoft.Office.Interop.Graph.Chart objChart;
Microsoft.Office.Interop.Graph.DataSheet dataSheet;

//Create a new presentation based on a template.
objApp = new Microsoft.Office.Interop.PowerPoint.Application();
objApp.Visible = MsoTriState.msoTrue;
objPresSet = objApp.Presentations;
objPres = objPresSet.Open(@"D:\xyz\Template.potx", MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
//.potx file is the template file of powerpoint.
//Take a template file remove all it's slides and save it.
//Then give it's physical path instead of it.
objSlides = objPres.Slides;

//Build Slide #1
//Add text to the slide title, format the text. Also add a chart to the
//slide and change the chart type to a 3D pie chart.
objSlide = objSlides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
objTextRng.Text = "My Chart";
objTextRng.Font.Name = "Comic Sans MS";
objTextRng.Font.Size = 48;

objChart = (Microsoft.Office.Interop.Graph.Chart)objSlide.Shapes.AddOLEObject(150, 150, 480, 320, "MSGraph.Chart.8", "", MsoTriState.msoFalse,"", 0, "", MsoTriState.msoFalse).OLEFormat.Object;
dataSheet = objChart.Application.DataSheet;
// objChart.Legend.LegendEntries();
dataSheet.Cells[1, 2] = "1"; //x-axis value 1
dataSheet.Cells[1, 3] = "2"; //x-axis value 2
dataSheet.Cells[1, 4] = "3"; //x-axis value 3
dataSheet.Cells[1, 5] = "4"; //x-axis value 4
dataSheet.Cells[1, 6] = "5"; //x-axis value 5
dataSheet.Cells[2, 1] = "Blue"; //Legend 1
dataSheet.Cells[2, 2] = "22";
dataSheet.Cells[2, 3] = "23";
dataSheet.Cells[2, 4] = "24";
dataSheet.Cells[2, 5] = "25";
dataSheet.Cells[2, 6] = "26";
dataSheet.Cells[3, 1] = "Red"; //Legend 2
dataSheet.Cells[3, 2] = "32";
dataSheet.Cells[3, 3] = "33";
dataSheet.Cells[3, 4] = "34";
dataSheet.Cells[3, 5] = "35";
dataSheet.Cells[3, 6] = "36";
dataSheet.Cells[4, 1] = "Orange"; //Legend 3
dataSheet.Cells[4, 2] = "42";
dataSheet.Cells[4, 3] = "43";
dataSheet.Cells[4, 4] = "44";
dataSheet.Cells[4, 5] = "45";
dataSheet.Cells[4, 6] = "46";
dataSheet.Cells[5, 1] = "Light Blue"; //Legend 4
dataSheet.Cells[5, 2] = "52";
dataSheet.Cells[5, 3] = "53";
dataSheet.Cells[5, 4] = "54";
dataSheet.Cells[5, 5] = "55";
dataSheet.Cells[5, 6] = "56";
objChart.Application.Update();
//objChart.ChartType = Microsoft.Office.Interop.Graph.XlChartType.xlLine; //changing the graph type : by default pie chart
objChart.Legend.Position = Microsoft.Office.Interop.Graph.XlLegendPosition.xlLegendPositionBottom;
objChart.HasTitle = true;
objChart.ChartTitle.Text = "Chart";

//Build Slide #2:
//Change the background color of this slide only. Add a text effect to the slide
//and apply various color schemes and shadows to the text effect.
objSlide = objSlides.Add(2, PpSlideLayout.ppLayoutBlank);
objSlide.FollowMasterBackground = MsoTriState.msoFalse;
objShapes = objSlide.Shapes;
objShape = objShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27,"The End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200);


//Modify the slide show transition settings for all slides in
//the presentation.
int[] SlideIdx = new int[2];
for (int i = 0; i < 2; i++) SlideIdx[i] = i + 1;
objSldRng = objSlides.Range(SlideIdx);
objSST = objSldRng.SlideShowTransition;
objSST.AdvanceOnTime = MsoTriState.msoTrue;
objSST.AdvanceTime = 3;
objSST.EntryEffect = PpEntryEffect.ppEffectBoxOut;

//Prevent Office Assistant from displaying alert messages:
bAssistantOn = objApp.Assistant.On;
objApp.Assistant.On = false;

//Run the Slide show
objSSS = objPres.SlideShowSettings;
objSSS.StartingSlide = 1;
objSSS.EndingSlide = 2;
objSSS.Run();

//Wait for the slide show to end.
objSSWs = objApp.SlideShowWindows;
while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(100);

//Reenable Office Assisant, if it was on:
if (bAssistantOn)
{
objApp.Assistant.On = true;
objApp.Assistant.Visible = false;
}
//Close the presentation without saving changes and quit PowerPoint.
objPres.SaveAs(@"D:\Sample", PpSaveAsFileType.ppSaveAsPresentation, MsoTriState.msoTrue);
//"D:\Sample" is the location where the ppt will be saved.
objPres.Close();
objApp.Quit();
}

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.