由于Excel毕竟不是 HTML,它有自己的样式标准,在Excel 中,实现换行的方法是:
- <brstyle='mso-data-placement:same-cell;'/>
完整代码:
- <%@PageLanguage="C#"Trace="false"AutoEventWireup="true"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <scriptrunat="server">
- protectedvoidButton1_Click(objectsender,EventArgse)
- {
- System.Web.HttpContextcurContext=System.Web.HttpContext.Current;
- //IO用于导出并返回excel文件
- System.IO.StringWriterstrWriter=null;
- System.Web.UI.HtmlTextWriterhtmlWriter=null;
- //设置编码和附件格式
- 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="";
- //导出excel文件
- strWriter=newSystem.IO.StringWriter();
- htmlWriter=newSystem.Web.UI.HtmlTextWriter(strWriter);
- htmlWriter.WriteLine("标题");
- //返回客户端
- GridView1.RenderControl(htmlWriter);
- curContext.Response.Write(strWriter.ToString().Replace("<br/>","<brstyle='mso-data-placement:same-cell;'/>"));
- curContext.Response.End();
- }
- protectedvoidPage_Load(objectsender,EventArgse)
- {
- if(!Page.IsPostBack)
- {
- GridView1.DataSource=CreateDataSourceByXianhuiMeng();
- GridView1.DataBind();
- }
- }
- System.Data.DataViewCreateDataSourceByXianhuiMeng()
- {
- System.Data.DataTabledt=newSystem.Data.DataTable();
- System.Data.DataRowdr;
- dt.Columns.Add(newSystem.Data.DataColumn("学生班级",typeof(System.String)));
- dt.Columns.Add(newSystem.Data.DataColumn("学生姓名",typeof(System.String)));
- dt.Columns.Add(newSystem.Data.DataColumn("语文",typeof(System.Decimal)));
- dt.Columns.Add(newSystem.Data.DataColumn("数学",typeof(System.Decimal)));
- dt.Columns.Add(newSystem.Data.DataColumn("英语",typeof(System.Decimal)));
- dt.Columns.Add(newSystem.Data.DataColumn("计算机",typeof(System.Decimal)));
- for(inti=0;i<8;i++)
- {
- System.Randomrd=newSystem.Random(Environment.TickCount*i);;
- dr=dt.NewRow();
- dr[0]="班级"+i.ToString();
- dr[1]="学生姓名:孟子E章"+i.ToString()+"<br/>所在班级:"+"班级"+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.DataViewdv=newSystem.Data.DataView(dt);
- returndv;
- }
- publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
- {}
- </script>
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <headid="Head1"runat="server">
- <title>无标题页</title>
- </head>
- <body>
- <formid="form1"runat="server">
- <asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="false">
- <Columns>
- <asp:BoundFieldHtmlEncode="false"DataField="学生姓名"HeaderText="测试字段"/>
- </Columns>
- </asp:GridView>
- <asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Button"/>
- </form>
- </body>
- </html>
要查阅 Excel 中使用的样式规范,请参考:Microsoft® Office HTML and XML Reference。下载地址:
http://download.microsoft.com/download/a/c/1/ac18e8a2-ce20-41b5-8407-c4cec4a17f19/ofhtml9.exe