Exporting gridview to microsoft powerpoint in c# - asp.net

I want to export gridview to powerpoint.I have the following code.But I have the following problems:
when I am using it for ms office 2010.its giving error:
Powerpoint cannot read c:\users\filename.ppt. The presentation cannot
be opened. Your antivirus program may prevent you from opening the
presentation. To fix this problem, make sure your antivirus program is
current and working correctly. If the problem persists and the
presentation is from someone that you trust, turn off your antivirus
program, and then try to open the presentation again. If you do this,
make sure you turn on your antivirus program again after you open the
presentation.
when I am using it for ms office 2007,its opening but there is no gridview
CODE:
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=FileName.ppt");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Charset = "";
// this.Response.ContentType = "application/vnd.openxmlformats- officedocument.presentationml.presentation";
this.Response.ContentType = "application/vnd.ms-powerpoint.presentation.macroEnabled.12";
this.Response.ContentType = "application/vnd.ms-powerpoint";
//Response.ContentType = "application/vnd.ppt";
GridView2.AllowPaging = false;
GridView2.DataBind();
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView2.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
Response.Flush();

Try to use an external library to generate your ppt or pptx.
Like aspose.net or Open xml.

Related

Export Excel from grid not working on Ajax Model PopUp

Hi i am trying to export grid data to excel but it is not working in IE8.I am using a Ajax Modal PopUp dialog box containing 2 buttons 'ok' and 'close'.On Ok click i want to download excel file.it works fine in Mozilla but in IE its not working.I am using below code. please suggest me how to do that?Also when i open file first it show warning before opening file how to handle that?
Response.Clear();
Response.Buffer = true;
string filename = "Checkout";
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".adt");
Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("<head>");
Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
Response.Write("<!--[if gte mso 9]><xml>");
Response.Write("<x:ExcelWorkbook>");
Response.Write("<x:ExcelWorksheets>");
Response.Write("<x:ExcelWorksheet>");
Response.Write("<x:Name>Sheet1</x:Name>");
Response.Write("<x:WorksheetOptions>");
Response.Write("<x:Print>");
Response.Write("<x:ValidPrinterInfo/>");
Response.Write("</x:Print>");
Response.Write("</x:WorksheetOptions>");
Response.Write("</x:ExcelWorksheet>");
Response.Write("</x:ExcelWorksheets>");
Response.Write("</x:ExcelWorkbook>");
Response.Write("</xml>");
Response.Write("<![endif]--> ");
Response.Write("</head>");
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView gv = new GridView();
gv.AutoGenerateColumns = true;
gv.DataSource = dt;
gv.DataBind();
gv.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
You get warning because tehnically speaking this is not export to excel, but you send a html with a wrong headers to trick browser to open this content with excel.
Better use native Excel library and generate real Excel file on the server, for example EPPlus, it's open source and doesn't require excel on server.
Here is the example of ashx handler that generates excel file from DataTable
https://stackoverflow.com/a/9569827/351383

Export pie chart to powerpoint using c#

i am trying to Export a pie chart to powerpoint slide uisng c# but not found any similar article which can help me...
please provide me some example or provide some useful link which can help me in achieving this functionality easily...
thanks,
Aman
we are using hipdf library but this is paid and we are converting html to image first and then showing this image on ppt slide. You can also do it by using inkscape exe file
In button_click event write following code.
GridView1.AllowPaging = false; //write this code only if paging is enabled.
GridView1.DataBind(); //write this code only if paging is enabled.
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.ppt");//for text file write FileName.txt
Response.Charset = "";
// If you want the option to open the Excel file without saving than comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ppt";\\for text file write vnd.txt
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
GridView1.AllowPaging = true; //write this code only if paging is enabled.
GridView1.DataBind();//write this code only if paging is enabled.
You will have to override function of VerifyRenderingInServerForm like this
public override void VerifyRenderingInServerForm(Control control)
{
//Keep it empty
}
And to avoid exception you have to set property of
<%# Page Language="C#" EnableEventValidation="false" %>

exporting to excel over https(SSL) using IE is not working in asp.net website

i am trying to export an excel file from an https site, it is working great with
firefox and other browsers but in IE its is giving me error that the site is not available or the file is not found.
i have research the problem and i have found it is cache related so i have change the headers in this page so it will allow caching but it is still not working.
this is a snap of the code
Response.Cache.SetAllowResponseInBrowserHistory(true);
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0));
Response.Cache.SetAllowResponseInBrowserHistory(true);
Response.CacheControl = "Public";
any help would be great thank you
#region For Download to work on HTTPS
Response.ClearHeaders();
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.Buffer = true;
Response.AddHeader("Content-Transfer-Encoding", "binary");
#endregion
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Temp.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter strWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(strWriter);
string heading = "<h4 align='left'>Temp Excel Cube</h4>";
strWriter.Write(heading);
gvTemp.RenderControl(htmlWrite);
Response.Write(strWriter.ToString());
Response.End();
I guess this is what you are looking for. It works for me on HTTPS, I've exported a grid to an excel sheet, you can use it to pickup a file from a physical location too. :)

Export Repeater to excel

I am trying to export my repeater to excel and here is my code...
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
string attachment = "attachment; filename=file.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
rpt.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
When I am trying to open file getting this error
The file you are trying to open, 'file.xls', is in a different format than specified
by the file extension. Verify that the file is not Corrupted and is from a trusted
source before opening the file. Do you want to open the file now?
Yes No Help option are available
What's wrong in my code or What I have to do resolve this issue.
Thanks
You are setting the content type to application/vnd.ms-excel but you are sending HTML contents in the response stream when you call the RenderContents method. You might need a library to generate Excel files.
Try wrapping your content into this:
StringBuilder sb = new StringBuilder("");
sb.Append("<HTML xmlns:x=\"urn:schemas-microsoft-com:office:excel\"><HEAD>");
sb.Append("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
sb.Append("<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>");
sb.Append(title);
sb.Append("</x:Name><x:WorksheetOptions><x:Print><x:ValidPrinterInfo/></x:Print></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--> </HEAD><BODY>");
sb.Append(content);
sb.Append("</BODY></HTML>");

exporting sql query result to excel

In my asp.net application i wanted to export the SQL query result to Excel and that excel should be avaliable to the user as a download.
Please help
I find working with excel to be a real headache, but it's possible to do just about anything if you want to. What features do you need to leverage, because offering a csv file instead is much easier!
Do you have SSRS, you could offer the query as an SSRS report and it is automatically available as an excel download!
If your on MS SQL Server, reporting services are available for free and can do this for you easily, you can also export to PDF and Word.
If not, or if its just a one off try here
you can show your result in a datagrid. after that you can export this grid to excel file.
do like this :
I pass grid by session.
Control grdList;
GridView grdList1 = Session["GridView"] as GridView;
if (grdList1 == null)
{
grdList = (DataGrid)Session["GridView"];
}
else
{
grdList = (GridView)Session["GridView"];
}
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=ExportList.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving than
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
Response.ContentEncoding = System.Text.Encoding.UTF8;// GetEncoding(1256);// UTF8;
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
grdList.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

Resources