The line feed code is retained when ASP. NET is exported to excel.

Source: Internet
Author: User

<Br style = 'mso-data-placement: Same-cell; '/>
Complete Code : Copy code The Code is as follows: <% @ page Language = "C #" trace = "false" autoeventwireup = "true" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<SCRIPT runat = "server">
Protected void button#click (Object sender, eventargs E)
{
System. Web. httpcontext curcontext = system. Web. httpcontext. Current;
// Io is used to export and return an Excel file
System. Io. stringwriter strwriter = NULL;
System. Web. UI. htmltextwriter htmlwriter = NULL;
// Set the encoding and attachment format
Httpcontext. Current. response. Clear ();
Httpcontext. Current. response. Buffer = true;
Httpcontext. Current. response. addheader ("content-disposition", String. Format ("attachment; filename = {0}", "aaa.xls "));
Curcontext. response. contenttype = "application/vnd. MS-excel ";
Curcontext. response. contentencoding = system. Text. encoding. getencoding ("gb18030 ");
Curcontext. response. charset = "";
// Export an Excel file
Strwriter = new system. Io. stringwriter ();
Htmlwriter = new system. Web. UI. htmltextwriter (strwriter );
Htmlwriter. writeline ("title ");
// Return to the Client
Gridview1.rendercontrol (htmlwriter );
Curcontext. response. write (strwriter. tostring (). replace ("<br/>", "<br style = 'mso-data-placement: Same-cell; '/> "));
Curcontext. response. End ();
}
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Gridview1.datasource = createperformancebyxianhuimeng ();
Gridview1.databind ();
}
}
System. Data. dataview createperformancebyxianhuimeng ()
{
System. Data. datatable dt = new system. Data. datatable ();
System. Data. datarow Dr;
DT. Columns. Add (new system. Data. datacolumn ("Student Class", typeof (system. String )));
DT. Columns. Add (new system. Data. datacolumn ("Student name", typeof (system. String )));
DT. Columns. Add (new system. Data. datacolumn ("", typeof (system. decimal )));
DT. Columns. Add (new system. Data. datacolumn ("Mathematics", typeof (system. decimal )));
DT. Columns. Add (new system. Data. datacolumn ("English", typeof (system. decimal )));
DT. Columns. Add (new system. Data. datacolumn ("computer", typeof (system. decimal )));
For (INT I = 0; I <8; I ++)
{
System. Random RD = new system. Random (environment. tickcount * I );;
Dr = DT. newrow ();
Dr [0] = "class" + I. tostring ();
Dr [1] = "Student name: Chapter E of Mencius" + I. tostring () + "<br/> class:" + "class" + I. tostring ();
Dr [2] = system. Math. Round (RD. nextdouble () * 100, 2 );
Dr [3] = system. Math. Round (RD. nextdouble () * 100, 2 );
Dr [4] = system. Math. Round (RD. nextdouble () * 100, 2 );
Dr [5] = system. Math. Round (RD. nextdouble () * 100, 2 );
DT. Rows. Add (DR );
}
System. Data. dataview DV = new system. Data. dataview (DT );
Return DV;
}
Public override void verifyrenderinginserverform (Control)
{}
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false">
<Columns>
<Asp: boundfield htmlencode = "false" datafield = "Student name" headertext = "test field"/>
</Columns>
</ASP: gridview>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/>
</Form>
</Body>
</Html>

for more information about the style specifications used in Excel, see Microsoft Office HTML and XML reference.
: http://download.microsoft.com/download/a/c/1/ac18e8a2-ce20-41b5-8407-c4cec4a17f19/ofhtml9.exe

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.