Hi,
Here I have created sample that will help you out.You can modify code as per your requirement.alos you need to create pot file(i.e. 1.pot).just create blank textfile and save it with extension .pot
HTML
<div>
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ImageUrl='<%#Eval("Image") %>' runat="server" Style="width: 150px; height: 150px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button Text="Export" runat="server" OnClick="Export" />
</div>
C#
Namespaces
using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Graph = Microsoft.Office.Interop.Graph;
using System.Runtime.InteropServices;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id", typeof(int)),
new DataColumn("Image", typeof(string))});
dt.Rows.Add(1, "~/Images/Desert.jpg");
dt.Rows.Add(2, "~/Images/Tulips.jpg");
ViewState["Data"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Export(object sender, EventArgs e)
{
ExportToPPT();
}
private void ExportToPPT()
{
if (ViewState["Data"] != null)
{
DataTable dt = (DataTable)ViewState["Data"];
String strTemplate = @"D:\1.pot";
PowerPoint.Application objApp;
PowerPoint.Presentations objPresSet;
PowerPoint._Presentation objPres;
PowerPoint.Slides objSlides;
PowerPoint._Slide objSlide;
PowerPoint.TextRange objTextRng;
PowerPoint.Shapes objShapes;
PowerPoint.Shape objShape;
PowerPoint.SlideShowWindows objSSWs;
PowerPoint.SlideShowTransition objSST;
PowerPoint.SlideRange objSldRng;
//Create a new presentation based on a template.
objApp = new PowerPoint.Application();
objApp.Visible = MsoTriState.msoTrue;
objPresSet = objApp.Presentations;
objPres = objPresSet.Open(strTemplate,
MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
objSlides = objPres.Slides;
int slideCount = 1;
objSlide = objSlides.Add(slideCount, PowerPoint.PpSlideLayout.ppLayoutBlank);
objSlide.FollowMasterBackground = MsoTriState.msoFalse;
objShapes = objSlide.Shapes;
objShape = objShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27,
"Begining", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200);
slideCount++;
foreach (DataRow row in dt.Rows)
{
objSlide = objSlides.Add(slideCount, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
objTextRng.Text = "Id :" + row["Id"].ToString();
objTextRng.Font.Name = "Comic Sans MS";
objTextRng.Font.Size = 48;
objSlide.Shapes.AddPicture(Server.MapPath(row["Image"].ToString()), MsoTriState.msoFalse, MsoTriState.msoTrue,
150, 150, 500, 350);
slideCount++;
}
//Build Slide End Slide:
objSlide = objSlides.Add(slideCount, PowerPoint.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 3 slides in
int[] SlideIdx = new int[slideCount];
for (int i = 0; i < slideCount; i++) SlideIdx[i] = i + 1;
objSldRng = objSlides.Range(SlideIdx);
objSST = objSldRng.SlideShowTransition;
objSST.AdvanceOnTime = MsoTriState.msoTrue;
objSST.AdvanceTime = 3;
objSST.EntryEffect = PowerPoint.PpEntryEffect.ppEffectBoxOut;
//Wait for the slide show to end.
objSSWs = objApp.SlideShowWindows;
objPres.SaveAs(Server.MapPath("~/PPTs/" + DateTime.Today.ToShortDateString().Replace("/", "_") + ".ppt"));
objPres.Close();
objApp.Quit();
}
}
VB
Namespaces
Imports Microsoft.Office.Core
Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
Imports Graph = Microsoft.Office.Interop.Graph
Imports System.Runtime.InteropServices
Code
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Id", GetType(Integer)), New DataColumn("Image", GetType(String))})
dt.Rows.Add(1, "~/Images/Desert.jpg")
dt.Rows.Add(2, "~/Images/Tulips.jpg")
ViewState("Data") = dt
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub Export(sender As Object, e As EventArgs)
ExportToPPT()
End Sub
Private Sub ExportToPPT()
If ViewState("Data") IsNot Nothing Then
Dim dt As DataTable = DirectCast(ViewState("Data"), DataTable)
Dim strTemplate As [String] = "D:\1.pot"
Dim objApp As PowerPoint.Application
Dim objPresSet As PowerPoint.Presentations
Dim objPres As PowerPoint._Presentation
Dim objSlides As PowerPoint.Slides
Dim objSlide As PowerPoint._Slide
Dim objTextRng As PowerPoint.TextRange
Dim objShapes As PowerPoint.Shapes
Dim objShape As PowerPoint.Shape
Dim objSSWs As PowerPoint.SlideShowWindows
Dim objSST As PowerPoint.SlideShowTransition
Dim objSldRng As PowerPoint.SlideRange
'Create a new presentation based on a template.
objApp = New PowerPoint.Application()
objApp.Visible = MsoTriState.msoTrue
objPresSet = objApp.Presentations
objPres = objPresSet.Open(strTemplate, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue)
objSlides = objPres.Slides
Dim slideCount As Integer = 1
objSlide = objSlides.Add(slideCount, PowerPoint.PpSlideLayout.ppLayoutBlank)
objSlide.FollowMasterBackground = MsoTriState.msoFalse
objShapes = objSlide.Shapes
objShape = objShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27, "Begining", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, _
230, 200)
slideCount += 1
For Each row As DataRow In dt.Rows
objSlide = objSlides.Add(slideCount, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)
objTextRng = objSlide.Shapes(1).TextFrame.TextRange
objTextRng.Text = "Id :" + row("Id").ToString()
objTextRng.Font.Name = "Comic Sans MS"
objTextRng.Font.Size = 48
objSlide.Shapes.AddPicture(Server.MapPath(row("Image").ToString()), MsoTriState.msoFalse, MsoTriState.msoTrue, 150, 150, 500, _
350)
slideCount += 1
Next
'Build Slide End Slide:
objSlide = objSlides.Add(slideCount, PowerPoint.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 3 slides in
Dim SlideIdx As Integer() = New Integer(slideCount - 1) {}
For i As Integer = 0 To slideCount - 1
SlideIdx(i) = i + 1
Next
objSldRng = objSlides.Range(SlideIdx)
objSST = objSldRng.SlideShowTransition
objSST.AdvanceOnTime = MsoTriState.msoTrue
objSST.AdvanceTime = 3
objSST.EntryEffect = PowerPoint.PpEntryEffect.ppEffectBoxOut
'Wait for the slide show to end.
objSSWs = objApp.SlideShowWindows
objPres.SaveAs(Server.MapPath("~/PPTs/" + DateTime.Today.ToShortDateString().Replace("/", "_") + ".ppt"))
objPres.Close()
objApp.Quit()
End If
End Sub
Screenshot
1)
2)