Scientific notation during Excel Export

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.