Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting exponential notation for large numbers while exporting gridview binded with a dataset to export. I am using asp.net with c#.

What I have tried:

Code is as below:
DataSet ds = objadmin.getAuditTrailReport(FromDate, ToDate, drdUserId.SelectedValue.ToString());

                DataTable dt = new DataTable();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dt = ds.Tables[0];
                    grdExport.DataSource = dt;
                    grdExport.DataBind();
                    Response.Clear();

                    Response.AddHeader("content-disposition", "attachment;   filename=Audittrail_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");

                    Response.Charset = "";

                    // If you want the option to open the Excel file without saving than

                    // comment out the line below

                    // Response.Cache.SetCacheability(HttpCacheability.NoCache);

                    Response.ContentType = "application/vnd.xls";

                    System.IO.StringWriter stringWrite = new System.IO.StringWriter();

                    System.Web.UI.HtmlTextWriter htmlWrite =
                    new HtmlTextWriter(stringWrite);
                    //for (int i = 0; i < grdExport.Items.Count; i++)
                    //{
                    //    DataGridItem item = grdExport.Items[i];
                    //    //Apply text style to each Row
                    //    item.Cells[i].Attributes.Add("style", "textmode");
                    //}
                    grdExport.RenderControl(htmlWrite);

                    string strHead = strReportHeading + " FROM " + ViewState["FDOB"].ToString() + " TO " + ViewState["TDOB"].ToString();
                    StringBuilder strbn = new StringBuilder();
                    strbn.Append("<div style='text-align:center;font-weight:bold;background-color:White; width:100%;font-size:large'> " + strHead + "</div>");
                    strbn.Append(stringWrite.ToString());

                    Response.Write(strbn);

                    Response.End();

                }
Posted
Updated 2-Dec-21 22:31pm
Comments
Richard MacCutchan 2-Dec-21 12:44pm    
That is a feature of Excel, not how you are exporting. If you want fixed notation then you need to add the formatting to the relevant cells or ranges.

1 solution

You are not exporting the grid to Excel. You are "exporting" it to HTML, whilst lying to the browser to make it think your content is an Excel file.

Excel will do its best to import the HTML. But you will have extremely limited control over the formatting, and you will always get a warning message telling you that the file content doesn't match the expected file type.

If you want to export to Excel, then use a library to generate a real Excel file. For example, ClosedXML[^], or the OpenXML SDK[^].
 
Share this answer
 

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