Export Excel from grid not working on Ajax Model PopUp - asp.net

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

Related

Exporting gridview to microsoft powerpoint in c#

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.

Export page content to PDF using a iTextSharp (include buttons and grids)

How to export my aspx page (include buttons and Grids) to PDF?
Searching the web I found iTextSharp, but it works only with normal html.
If my page has Grids or Buttons, these do not appear in PDF.
My current code for export to PDF.
This code exports only basic html (no buttons or grids).
string attachment = "attachment; filename=AllPage.pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(htextw);
Document document = new Document();
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
StringReader str = new StringReader(stw.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(str);
document.Close();
Response.Write(document);
Response.End();
Just use wkhtmltopdf. It'll handle anything short of an ActiveX control.

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

saving dataset in excel and allow user to download it in the client machine

I am developing an application where i want i am displaying a dataset in the datagrid view for the user.Now the user wants to download the data in the datagridview in an excel format.How can i do it ?
1) should i write the dataset in the excel and save it the server before the user download the file ?
2) Can i use a hyper link and set the path of the file that is saved in the server to the hyper link hRef property , so that the user can click and download the file ?
I am using C# ASP.net 2.0
Please help !
no need to save the file to the sever use the following code, will create the file on the fly:
Public Shared Sub DataTableToExcel(ByVal dt As DataTable, ByVal FileName As String
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Write(Environment.NewLine)
For Each row As DataRow In dt.Rows
For i As Integer = 0 To dt.Columns.Count - 1
HttpContext.Current.Response.Write(row(i).ToString().Replace(";", String.Empty) + ";")
Next
HttpContext.Current.Response.Write(Environment.NewLine)
Next
HttpContext.Current.Response.ContentType = "application/ms-excel"
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls")
HttpContext.Current.Response.[End]()
End Sub
you can use this simple code to accomplish
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
string attachment = "attachment; filename=SummaryReport" + DateTime.Now.ToString() + ".xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
grd.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
SpreadsheetGear for .NET will do it.
You can see ASP.NET (C# and VB) samples here and download a free trial here.
Disclaimer: I own SpreadsheetGear LLC

Export Gridview with image to Excel - Problem

I have a Gridview in my ASP.NET C# Page. It has a couple of columns and also one image column. I can export the entire columns to an Excel file, but the image column is blank. By the way, I use the full path.
Any Idea?
Yes in default.aspx (For local Export)
ImageUrl="http://localhost:4056/CSharp/banner.gif"
instead of ImageUrl="~/banner.gif"
or (For remote Export)
http://your ip address here :port/foldername/filename
Response.Clear(); //this clears the Response of any headers or previous output
Response.Buffer = true; //make sure that the entire output is rendered simultaneously
Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter(); //System.IO namespace should be used
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
GridView2.GridLines = GridLines.Both;
GridView2.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();

Resources