Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export HTML Table to Excel using ASP.NET

I have an html table (Not Gridview) and that has no proper Header and rows. Instead it has customized structure and data. I want to export this table to Excel. How can I do using ASP.NET? enter image description here

The labels are fixed text and the integer values are coming from database. So the table structure is fixed only the integer/decimal values change.

like image 756
hotcoder Avatar asked Feb 20 '12 15:02

hotcoder


People also ask

How do I export data from ASP net to excel?

Add a link button in the View and name it DownloadExcel. Now, create a Method in Controller and add the following code to Export the data in Excel. Now, run the project and click on the "Download Excel" button. The data is downloaded in Excel format.

How do I export a table from web to excel?

Just right click on the webpage, and then select "Export to Microsoft Excel" on the shortcut menu. This should display the New Web Query dialog box in Excel.

How do I export a table from excel to ASP NET core?

using FileSaver. js plugin and TableExport plugin to export html table to excel. Right click the wwwroot folder, then click Add and Client-Side Library..., enter FileSaver. js and click the Install button to install the library.


3 Answers

You want Export HTML table (Not Gridview) customized structure and data to Excel using ASP.NET.

Try the following Approach

  1. Provide the ID and add runat="server" attribute

    <table id="tbl" runat="server" >

  2. Add the following code

    Response.ContentType = "application/x-msexcel"; 
    Response.AddHeader("Content-Disposition", "attachment;
    filename=ExcelFile.xls");
    Response.ContentEncoding = Encoding.UTF8; 
    StringWriter tw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    tbl.RenderControl(hw);
    Response.Write(tw.ToString());
    Response.End();
    
like image 196
Arun Singh Avatar answered Oct 14 '22 05:10

Arun Singh


You can use below code:

Response.ContentType = "application/force-download";
Response.AddHeader("content-disposition", "attachment; filename=Print.xls");
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>Report Data</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]--> ");
StringWriter tw = new StringWriter(); 
HtmlTextWriter hw = new HtmlTextWriter(tw);      
tbl.RenderControl(hw);
Response.Write(tw.ToString());
Response.Write("</head>");
Response.flush();

Also it is advicable to give inline css if you want the exported output look exactly same as your UI. If you apply css classes to the table then it would not be displayed in exported excel.

like image 32
user3215480 Avatar answered Oct 14 '22 05:10

user3215480


If dtReport contains the table(i.e data to be exported) then we can export the table to excel by using the following LOC and also we can format the header

    if (dtReports != null && dtReports.Rows.Count > 0 && !string.IsNullOrEmpty(formName))
            {
                string filename = formName.ToUpper() + ParsConstant.XLS_EXTENSION;
                StringWriter tw = new StringWriter();

                using (HtmlTextWriter hw = new HtmlTextWriter(tw))
                {

                    //Binding Datatable to DataGrid.
                    DataGrid dgGrid = new DataGrid();
                    dgGrid.DataSource = dtReports;
                    dgGrid.DataBind();

                    //Some Properties for the Header
                    dgGrid.HeaderStyle.Font.Bold = true;
                    dgGrid.HeaderStyle.Font.Size = 13;

                    //Get the HTML for the control.
                    dgGrid.RenderControl(hw);


                    Response.ContentType = "application/vnd.ms-excel";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
                    //Response.Write("<style> TD { mso-number-format:\\@; } </style>");


                    Response.Write(tw.ToString());
                    Response.End();
                }
            }

using MSO Format will not be avoiding leading zero's,but it will convert the text to string which is not advisable for doing operations.

like image 1
Deepak Kothari Avatar answered Oct 14 '22 06:10

Deepak Kothari