1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Aspose.Cells;
using Aspose.Cells.Charts;
using Aspose.Slides;
using Aspose.Slides.Pptx;
namespace ExcelPowerPoint
{
public partial class Excel_Chart_Ole_PowerPoint : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strTemp = DateTime.Now.ToString("ddmmyyhhmmss");
if (!Page.IsPostBack)
{
popListChartType();
getChartImage().Save(Server.MapPath("images") + "\\output" + lstChartType.Text + strTemp + ".jpg", ImageFormat.Jpeg);
imgExcel.ImageUrl = "~/images/output" + lstChartType.Text + strTemp + ".jpg";
RunPPT().GetSlideByPosition(1).GetThumbnail(1f, 1f).Save(Server.MapPath("images") + "\\outputPres" + lstChartType.Text + strTemp + ".jpg", ImageFormat.Jpeg);
imgPowerPoint.ImageUrl = "~/images/outputPres" + lstChartType.Text + strTemp + ".jpg";
}
else
{
try
{
switch (GetPostBackControl(this))
{
case "btnPreviewExcel":
getChartImage().Save(Server.MapPath("images") + "\\output" + lstChartType.Text + strTemp + ".jpg", ImageFormat.Jpeg);
imgExcel.ImageUrl = "~/images/output" + lstChartType.Text + strTemp + ".jpg";
break;
case "lstChartType":
getChartImage().Save(Server.MapPath("images") + "\\output" + lstChartType.Text + strTemp + ".jpg", ImageFormat.Jpeg);
imgExcel.ImageUrl = "~/images/output" + lstChartType.Text + strTemp + ".jpg";
RunPPT().GetSlideByPosition(1).GetThumbnail(1f, 1f).Save(Server.MapPath("images") + "\\outputPres" + lstChartType.Text + strTemp + ".jpg", ImageFormat.Jpeg);
imgPowerPoint.ImageUrl = "~/images/outputPres" + lstChartType.Text + strTemp + ".jpg";
break;
case "btnPreviewPres":
if (this.lstPowerPointFmt.Text == "ppt")
{
RunPPT().GetSlideByPosition(1).GetThumbnail(1f, 1f).Save(Server.MapPath("images") + "\\outputPres" + lstChartType.Text + strTemp + ".jpg", ImageFormat.Jpeg);
imgPowerPoint.ImageUrl = "~/images/outputPres" + lstChartType.Text + strTemp + ".jpg";
}
else
{
RunPPTX().Slides[0].GetThumbnail(1f, 1f).Save(Server.MapPath("images") + "\\outputPres" + lstChartType.Text + strTemp + ".jpg", ImageFormat.Jpeg);
imgPowerPoint.ImageUrl = "~/images/outputPres" + lstChartType.Text + strTemp + ".jpg";
}
break;
case "lstPowerPointFmt":
if (this.lstPowerPointFmt.Text == "ppt")
{
RunPPT().GetSlideByPosition(1).GetThumbnail(1f, 1f).Save(Server.MapPath("images") + "\\outputPres" + lstChartType.Text + strTemp + ".jpg", ImageFormat.Jpeg);
imgPowerPoint.ImageUrl = "~/images/outputPres" + lstChartType.Text + strTemp + ".jpg";
}
else
{
RunPPTX().Slides[0].GetThumbnail(1f, 1f).Save(Server.MapPath("images") + "\\outputPres" + lstChartType.Text + strTemp + ".jpg", ImageFormat.Jpeg);
imgPowerPoint.ImageUrl = "~/images/outputPres" + lstChartType.Text + strTemp + ".jpg";
}
break;
}
}
catch (Exception ex)
{
this.imgExcel.ImageUrl = "~/images/error.jpg";
this.imgPowerPoint.ImageUrl = "~/images/error.jpg";
}
}
}
public static string GetPostBackControl(Page page)
{
Control control = null;
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if (ctrlname != null && ctrlname != string.Empty)
{
control = page.FindControl(ctrlname);
}
else
{
foreach (string ctl in page.Request.Form)
{
Control c = page.FindControl(ctl);
if (c is System.Web.UI.WebControls.Button)
{
control = c;
break;
}
}
}
return control.ID;
}
private void popListChartType()
{
foreach (string s in Enum.GetNames(typeof(ChartType)))
this.lstChartType.Items.Add(s);
this.lstChartType.Text = "Column";
}
private Bitmap getChartImage()
{
//Create a workbook
Workbook wb = new Workbook();
//Add an excel chart
int chartRows = int.Parse(this.txtRows.Text);// 55;
int chartCols = int.Parse(this.txtCols.Text);//25;
int chartSheetIndex = AddExcelChartInWorkbook(wb, chartRows, chartCols);
//Set the OLE size of the chart
wb.Worksheets.SetOleSize(0, chartRows, 0, chartCols);
//wb.Save("d:\\presex\\chk\\images\\wb1.xls");
//Get the image of the chart
//Bitmap imgChart = wb.Worksheets[chartSheetIndex].Charts[0].ToImage();
return wb.Worksheets[chartSheetIndex].Charts[0].ToImage();
}
public Presentation RunPPT()
{
//Create a workbook
Workbook wb = new Workbook();
//Add an excel chart
int chartRows = int.Parse(this.txtRows.Text);// 55;
int chartCols = int.Parse(this.txtCols.Text);//25;
int chartSheetIndex = AddExcelChartInWorkbook(wb, chartRows, chartCols);
//Set the OLE size of the chart
wb.Worksheets.SetOleSize(0, chartRows, 0, chartCols);
//Get the image of the chart
Bitmap imgChart = wb.Worksheets[chartSheetIndex].Charts[0].ToImage();
//imgChart.Save(Server.MapPath("images") + "\\output" + this.lstChartType.Text + ".jpg", ImageFormat.Jpeg);
//Save the workbook to stream
MemoryStream wbStream = wb.SaveToStream();
//Create a presentation
Presentation pres = new Presentation();
//Set the Slide Size
int intSldHt = (int)(float.Parse(this.txtHeightSld.Text) * 576);
int intSldWd = (int)(float.Parse(this.txtWidthSld.Text) * 576);
Size sz = new Size(intSldWd, intSldHt);
pres.SlideSize = sz;
Slide sld = pres.GetSlideByPosition(1);
//Add the workbook on slide
AddExcelChartInPresentation(pres, sld, wbStream, imgChart);
//Write the output presentation to the disk
return pres;
//pres.Write("d:\\presex\\output" + this.lstChartType.Text + ".ppt");
}
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;
//Populate DataSheet with data
//for (int i = 0; i < this.GrdViewExcel.RowCount; i++)
// for (int j = 0; j < this.GrdViewExcel.ColumnCount; j++)
string[][] strData = new string[4][];
strData[0] = new string[] { this.A1.Text, this.B1.Text, this.C1.Text, this.D1.Text, this.E1.Text };
strData[1] = new string[] { this.A2.Text, this.B2.Text, this.C2.Text, this.D2.Text, this.E2.Text };
strData[2] = new string[] { this.A3.Text, this.B3.Text, this.C3.Text, this.D3.Text, this.E3.Text };
strData[3] = new string[] { this.A4.Text, this.B4.Text, this.C4.Text, this.D4.Text, this.E4.Text };
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; 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] + "<br>");
}
//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.CompareTo(this.lstChartType.Text) == 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;
}
void AddExcelChartInPresentation(Presentation pres, Slide sld, Stream wbStream, Bitmap imgChart)
{
Aspose.Slides.Picture pic = new Aspose.Slides.Picture(pres, imgChart);
int picId = pres.Pictures.Add(pic);
int oleWidth = (int)(float.Parse(this.txtWidthOLE.Text) * 576);
int oleHeight = (int)(float.Parse(this.txtHeightOLE.Text) * 576);
int x = 0;
byte[] chartOleData = new byte[wbStream.Length];
wbStream.Position = 0;
wbStream.Read(chartOleData, 0, chartOleData.Length);
OleObjectFrame oof = sld.Shapes.AddOleObjectFrame(x, 0, oleWidth, oleHeight,
"Excel.Sheet.8", chartOleData);
oof.PictureId = picId;
}
public PresentationEx RunPPTX()
{
//Create a workbook
Workbook wb = new Workbook();
//Add an excel chart
int chartRows = int.Parse(this.txtRows.Text);
int chartCols = int.Parse(this.txtCols.Text);
int chartSheetIndex = AddExcelChartInWorkbookPPTX(wb, chartRows, chartCols);
//Set the OLE size of the chart
wb.Worksheets.SetOleSize(0, chartRows, 0, chartCols);
//Get the image of the chart
Bitmap imgChart = wb.Worksheets[chartSheetIndex].Charts[0].ToImage();
//Save the workbook to stream
MemoryStream wbStream = new MemoryStream();
if (this.lstExcelFmt.Text == "XLSX")
wb.Save(wbStream, FileFormatType.Excel2007Xlsx);
else
wb.Save(wbStream, FileFormatType.Excel2003);
//Create a presentation
PresentationEx pres = new PresentationEx();
//Set the Slide Size
float fltSldHt = float.Parse(this.txtHeightSld.Text) * 72;
float fltSldWd = float.Parse(this.txtWidthSld.Text) * 72;
SizeF sz = new SizeF(fltSldWd, fltSldHt);
pres.SlideSize.Size = sz;
SlideEx sld = pres.Slides[0];
//Add the workbook on slide
AddExcelChartInPresentationPPTX(pres, sld, wbStream, imgChart);
return pres;
//Write the output presentation to the disk
//pres.Write("d:\\output.pptx");
}
int AddExcelChartInWorkbookPPTX(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[4][];
strData[0] = new string[] { this.A1.Text, this.B1.Text, this.C1.Text, this.D1.Text, this.E1.Text };
strData[1] = new string[] { this.A2.Text, this.B2.Text, this.C2.Text, this.D2.Text, this.E2.Text };
strData[2] = new string[] { this.A3.Text, this.B3.Text, this.C3.Text, this.D3.Text, this.E3.Text };
strData[3] = new string[] { this.A4.Text, this.B4.Text, this.C4.Text, this.D4.Text, this.E4.Text };
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; 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] + "<br>");
}
//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.CompareTo(this.lstChartType.Text) == 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 <= 4; rows++)
chart.NSeries.Add(sheetName + "!A" + rows.ToString() + ":E" + rows.ToString(), false);
//Set ChartSheet an active sheet
wb.Worksheets.ActiveSheetIndex = chartSheetIdx;
return chartSheetIdx;
}
void AddExcelChartInPresentationPPTX(PresentationEx pres, SlideEx sld, Stream wbStream, Bitmap imgChart)
{
float oleWidth = float.Parse(this.txtWidthOLE.Text) * 72;
float oleHeight = float.Parse(this.txtHeightOLE.Text) * 72;
int x = 0;
byte[] chartOleData = new byte[wbStream.Length];
wbStream.Position = 0;
wbStream.Read(chartOleData, 0, chartOleData.Length);
OleObjectFrameEx oof = null;
if (this.lstExcelFmt.Text == "XLSX")
oof = sld.Shapes.AddOleObjectFrame(x, 0, oleWidth, oleHeight,
"Excel.Sheet.12", chartOleData);
else
oof = sld.Shapes.AddOleObjectFrame(x, 0, oleWidth, oleHeight,
"Excel.Sheet.8", chartOleData);
oof.Image = pres.Images.AddImage((System.Drawing.Image)imgChart);
}
protected void btnDownload_Click(object sender, EventArgs e)
{
if (this.lstPowerPointFmt.Text == "ppt")
RunPPT().Save("ExcelPowerPoint." + this.lstPowerPointFmt.Text,Aspose.Slides.Export.SaveFormat.Ppt,Response,false);
if (this.lstPowerPointFmt.Text == "pptx")
RunPPTX().Save("ExcelPowerPoint." + this.lstPowerPointFmt.Text, Aspose.Slides.Export.SaveFormat.Pptx, Response, false);
this.Response.End();
}
}
}
|