Click here to Skip to main content
15,877,966 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DataTable dt = new DataTable();
                dt = _dataSet.Tables[0];
                //Create a dummy GridView

                GridView GridView1 = new GridView();
                GridView1.AllowPaging = false;
                GridView1.DataSource = dt;
                GridView1.DataBind();

                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition",
                 "attachment;filename=SummaryCollection.xls");

                Response.Charset = "";
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter sw = new StringWriter();

                HtmlTextWriter hw = new HtmlTextWriter(sw);
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    GridView1.Rows[i].Attributes.Add("class", "textmode");
                }
                GridView1.RenderControl(hw);

                //style to format numbers to string
                string style = @" .textmode { mso-number-format:\@; } ";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();


What I have tried:

above i write the code which export Data from dataset in excel i want to know

how i give header name and bind shcool name in Header and Also One Column put it on Top of the sheet after School Name

Please give me Code
Posted
Updated 29-Nov-19 1:45am
v2
Comments
F-ES Sitecore 29-Nov-19 5:26am    
You're not exporting anything to excel, you are sending an html table to the browser and asking the browser to use Excel to interpret that table, so what you can do is fairly limited to how excel interprets html tables. If you want more control over how the excel file is formatted you'll probably need to create an actual excel file using something like the Open XML SDK, or there are products like EPPlus etc.

1 solution

As F-ES Sitecore said, use a tool like EPPlus[^] to generate a proper Excel file:
C#
DataTable dt = _dataSet.Tables[0];

using (ExcelPackage package = new ExcelPackage())
{
    var sheet = package.Workbook.Worksheets.Add("Your Sheet Name");
    sheet.Cells["A1"].LoadFromDataTable(dt, PrintHeaders: true, TableStyle: TableStyles.Medium9);
    
    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment;filename=SummaryCollection.xlsx");
    package.SaveAs(Response.OutputStream);
    
    Response.Flush();
    Response.End();
}
 
Share this answer
 
v2
Comments
Member 12183079 2-Dec-19 0:36am    
getting exception in this line
expected;

sheet.Cells["A1"].LoadFromDataTable(dt, PrintHeaders: true, TableStyle: TableStyles.Medium9);
Richard Deeming 3-Dec-19 6:26am    
And the exception is?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900