When the datagrid exports data to excel, some values are represented by scientific notation by default.
There are many ways to solve this problem, such as adding single quotes to the Front (directly entering single quotes before a number in excel can make the number display normally, but it won't work if you add it to the program, don't know why), or change the type, etc. In the end, a method is used, that is, adding a space in front.
The following is my code:
Protected void BtnExcel_Click (object sender, EventArgs e)
{
Dt = (DataTable) Session ["dt"];
// Prevent the sn in excel from being a scientific notation
For (int I = 0; I <dt. Rows. Count; I ++)
Dt. rows [I] [1] = "& nbsp;" + dt. rows [I] [1]. toString (); // a space is not an empty space, but an html sign.
Dg2.DataSource = dt. DefaultView;
Dg2.DataBind ();
Response. Clear ();
Response. Buffer = true;
Response. Charset = "GB2312 ";
Response. AppendHeader ("Content-Disposition", "attachment?filename=pandatalist.xls ");
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("UTF-8"); // set the output stream to simplified Chinese
Response. ContentType = "application/ms-excel"; // set the output file type to an excel file.
EnableViewState = false;
System. Globalization. CultureInfo myCItrad = new System. Globalization. CultureInfo ("ZH-CN", true );
System. IO. StringWriter oStringWriter = new System. IO. StringWriter (myCItrad );
System. Web. UI. HtmlTextWriter oHtmlTextWriter = new System. Web. UI. HtmlTextWriter (oStringWriter );
Dg2.RenderControl (oHtmlTextWriter );
Response. Write (oStringWriter. ToString ());
Dg2.ShowHeader = false;
Response. End ();
}
In this way, the exported excel document is normal.
I found this article on the Internet, but it took too little time to experiment with it.
I will try again later. Http://www.cnblogs.com/huomm/articles/982862.html