ASP.NET 導(dǎo)出到Excel時(shí)保留換行的代碼
完整代碼:
<%@ 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用于導(dǎo)出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
// 設(shè)置編碼和附件格式
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 = "";
// 導(dǎo)出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
htmlWriter.WriteLine("標(biāo)題");
// 返回客戶端
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("學(xué)生班級", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("學(xué)生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("語文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("數(shù)學(xué)", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英語", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("計(jì)算機(jī)", 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] = "學(xué)生姓名:孟子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>無標(biāo)題頁</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HtmlEncode="false" DataField="學(xué)生姓名" 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
- asp.net生成Excel并導(dǎo)出下載五種實(shí)現(xiàn)方法
- ASP.NET操作EXCEL的總結(jié)篇
- ASP.NET(C#)讀取Excel的文件內(nèi)容
- Asp.Net使用Npoi導(dǎo)入導(dǎo)出Excel的方法
- asp.net使用npoi讀取excel模板并導(dǎo)出下載詳解
- asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法
- ASP.NET導(dǎo)出Excel打開時(shí)提示:與文件擴(kuò)展名指定文件不一致解決方法
- asp.net 讀取并顯示excel數(shù)據(jù)的實(shí)現(xiàn)代碼
- asp.net導(dǎo)出EXCEL的功能代碼
- Asp.net MVC實(shí)現(xiàn)生成Excel并下載功能
相關(guān)文章
ASP.NET三層架構(gòu)詳解 如何實(shí)現(xiàn)三層架構(gòu)
這篇文章主要為大家詳細(xì)介紹了ASP.NET三層架構(gòu),如何實(shí)現(xiàn)三層架構(gòu),本文為大家揭曉,感興趣的小伙伴們可以參考一下2016-05-05
asp.net使用for循環(huán)實(shí)現(xiàn)Datalist的分列顯示功能
工程業(yè)績--用for循環(huán)代替了DataList多列顯示,得到2行四列的表格,需要內(nèi)存表的8行數(shù)據(jù)2009-12-12
WPF實(shí)現(xiàn)ScrollViewer滾動到指定控件處
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)ScrollViewer滾動到指定控件處,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
ASP.NET MVC運(yùn)行出現(xiàn)Uncaught TypeError: Cannot set property __MVC
同一相站點(diǎn),有些頁面的客戶端驗(yàn)證能工作,而有些死活不行。打開頁面就出現(xiàn)Uncaught TypeError: Cannot set property __MVC_FormValidation of null錯誤2010-04-04
asp.net 光棒效應(yīng)實(shí)現(xiàn)代碼
asp.net 光棒效應(yīng)(今天剛剛學(xué)到的)2009-12-12
ASP.NET 恢復(fù)備份Sqlserver實(shí)現(xiàn)代碼
在線恢復(fù)和備份SQL Server的代碼,需要的朋友可以參考下。2010-04-04

