ASP.NET 導出到Excel時保留換行的代碼
更新時間:2008年12月17日 20:23:28 作者:
由于Excel畢竟不是 HTML,它有自己的樣式標準,在Excel 中,實現(xiàn)換行的方法是
<br style='mso-data-placement:same-cell;'/>
完整代碼:
<%@ 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 Button1_Click(object sender, EventArgs e)
{
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于導出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = 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 = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
htmlWriter.WriteLine("標題");
// 返回客戶端
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 = CreateDataSourceByXianhuiMeng();
GridView1.DataBind();
}
}
System.Data.DataView CreateDataSourceByXianhuiMeng()
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("學生班級", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("學生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("語文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("數(shù)學", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英語", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("計算機", typeof(System.Decimal)));
for (int i = 0; i < 8; i++)
{
System.Random rd = new System.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.DataView dv = new System.Data.DataView(dt);
return dv;
}
public override void VerifyRenderingInServerForm(Control control)
{ }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HtmlEncode="false" DataField="學生姓名" HeaderText="測試字段" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>
完整代碼:
復制代碼 代碼如下:
<%@ 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 Button1_Click(object sender, EventArgs e)
{
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于導出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = 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 = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
htmlWriter.WriteLine("標題");
// 返回客戶端
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 = CreateDataSourceByXianhuiMeng();
GridView1.DataBind();
}
}
System.Data.DataView CreateDataSourceByXianhuiMeng()
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("學生班級", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("學生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("語文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("數(shù)學", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英語", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("計算機", typeof(System.Decimal)));
for (int i = 0; i < 8; i++)
{
System.Random rd = new System.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.DataView dv = new System.Data.DataView(dt);
return dv;
}
public override void VerifyRenderingInServerForm(Control control)
{ }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HtmlEncode="false" DataField="學生姓名" HeaderText="測試字段" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>
要查閱 Excel 中使用的樣式規(guī)范,請參考:Microsoft® Office HTML and XML Reference。
下載地址:http://download.microsoft.com/download/a/c/1/ac18e8a2-ce20-41b5-8407-c4cec4a17f19/ofhtml9.exe
相關文章
SQL Server 2008 R2:error 26 開啟遠程連接詳解
本篇文章小編為大家介紹,SQL Server 2008 R2:error 26 開啟遠程連接詳解。需要的朋友參考下2013-04-04ASP.NET Gridview與checkbox全選、全不選實現(xiàn)代碼
ASP.NET Gridview checkbox全選與全不選實現(xiàn)代碼,其實原理就是利用js來實現(xiàn)的,但需要簡單的設置下回傳。2010-04-04asp.net(c#)開發(fā)中的文件上傳組件uploadify的使用方法(帶進度條)
在asp.net開發(fā)中,有很多可以上傳的組件模塊,利用HTML的File控件(uploadify)的上傳也是一種辦法,這里為大家介紹一下(uploadify)的一些使用方法2012-12-12