export html file to excel using asp.net - c#

Can anybody help me I am a beginner and have no concept how the conversion can be made.
Here is my code:
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Response.ContentEncoding = System.Text.Encoding.Unicode;
fuFile.RenderControl(hw);
Response.Output.Write(sw.ToString().Replace(" ", "").Replace("&", "&"));
Response.End();
Is this not enough to export an HTML file to Excel?

I have found this works:
String content = "<html><body><table><tr><td>your table</td></tr></table></body></html>";
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
Response.ContentType = "application/vnd.xls";
Response.Cache.SetCacheability(HttpCacheability.NoCache); // not necessarily required
Response.Charset = "";
Response.Output.Write(content);
Response.End();
As long as your content is well-formed HTML, this should work.
Also note that comments, VBA code/macros, and multiple work-sheets do not work with this method. Additionally any styling that is not inline will also not render.
And some other considerations on this matter:
How can I export tables to excel from a webpage
http://www.c-sharpcorner.com/UploadFile/ankurmalik123/export-html-to-excel-and-more/

Related

Binary to File download - But the downloaded file has no file extension?

protected void getFileAttachment(byte[] bytes)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = fileType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + "test101");
Response.OutputStream.Write(bytes,0,bytes.Length);
Response.BinaryWrite(bytes);
Response.Flush();
}
I'm trying to convert varbinary that is saved in SQL Server and I'm having trouble because the output file has no extension
Response.AppendHeader("Content-Disposition", "attachment; filename=" + "test101.filetype");
My fault - I did not include the file extension in my database
Thanks for the comments!

C# Response.ContentType Creating multiple tabs in Excel

I'm generating an Excel report in C# and need to add a second tab to the Excel file that will contain reference data. How do I achieve this without using ClosedXML ?
Response.AddHeader("content-disposition", "attachment; filename=" + fname);
Response.ContentType = "application/vnd.ms-excel";
var writer = new System.IO.StringWriter();
var htmlTextWriter = new HtmlTextWriter(writer);
dataGrid.RenderControl(htmlTextWriter);
Response.Write(writer.ToString());
Response.End();

export grid view to excel using C#

I have created a page which will accept SharePoint list name as query string and generate an excel file
I am trying to export to excel using the below code
ListName = ListName.Replace(" ", "");
string attachment = "attachment; filename=" + ListName + ".xls";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel 8.0";
HttpContext.Current.Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htmlwriter = new HtmlTextWriter(sw);
gridview.DataSource = _dtFramedTable;
gridview.DataBind();
htmlwriter.AddAttribute("xmlns:x", "urn:schemas-microsoft-com:office:excel");
htmlwriter.RenderBeginTag(HtmlTextWriterTag.Html);
htmlwriter.RenderBeginTag(HtmlTextWriterTag.Head);
htmlwriter.RenderBeginTag(HtmlTextWriterTag.Style);
htmlwriter.Write("br {mso-data-placement:same-cell;}");
htmlwriter.RenderEndTag();
htmlwriter.RenderEndTag();
htmlwriter.RenderBeginTag(HtmlTextWriterTag.Body);
gridview.RenderControl(htmlwriter);
htmlwriter.RenderEndTag();
htmlwriter.RenderEndTag();
HttpContext.Current.Response.Write(HttpUtility.HtmlDecode(sw.ToString()));
HttpContext.Current.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.Close();
This exports gridview to excel, but when I open the excel file, it says
file format and extension of don't match. the file could be corrupted
or unsafe unless you trust the source, don't open it
How can this be avoided?
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=\""+Export.xls+"\"; filename*=UTF-8''"+Uri.EscapeDataString(Export.xls)); );
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "example.xls"))

Export to excel using asp.net

I am using following function to export my data to excel sheet:
string filename = "Test.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
//Get the H`enter code here`TML for the control.
yourGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
Response.Write(tw.ToString());
but excel sheet is not created. Although it shows in chrome browser bottom that excel is downloading. When it finishs downloading, I click it and it says file cant be opened. Please suggest me what is wrong in it?
[EDIT]
Response.Clear();
Response.Buffer = true;
//use Response.AddHeader
Response.AddHeader("content-disposition", "attachment;filename=Test.xlsx");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvLogs.RenderControl(hw);
gvLogs.AllowPaging = false;
gvLogs.DataBind(); // bind data
Response.Output.Write(sw.ToString());
//need to call Flush and End methods
Response.Flush();
Response.End();
give same error
Epplus is a good library for excel import/export. You can check it out.
You can try this one
private void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0)
{
string filename = "DownloadReport.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
Change the 'Response.ContentType' value in your code as like below.
Response.ContentType = "application/vnd.xls";
Hope this will help you.
check below code
Response.Clear();
Response.Buffer = true;
//use Response.AddHeader
Response.AddHeader("content-disposition", "attachment;filename=Test.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
GridView1.AllowPaging = false;
GridView1.DataBind(); // bind data
Response.Output.Write(sw.ToString());
//need to call Flush and End methods
Response.Flush();
Response.End();

Convert .aspx page to Microsoft Excel Page

HI i want to use such thing in my web application that on a button click .aspx page converted in Xls page.
I did it but result is not good as I needed. I used this code on button click which is given below.
Response.Clear();
Response.Buffer = true;
string filename = "TicketSecretary.xlxs";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
I'm missing the Response.End(); at the end.
http://msdn.microsoft.com/en-us/library/system.web.httpresponse.end.aspx

Categories