Export HTML Table to Excel using ASP.NET

前端 未结 5 1573
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 13:39

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

5条回答
  •  孤城傲影
    2020-11-28 14:24

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

提交回复
热议问题