欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net Linq把數(shù)據(jù)導(dǎo)出到Excel的代碼

 更新時間:2008年10月13日 23:37:24   作者:  
最近有需要通過WEB把數(shù)據(jù)導(dǎo)出到Excel的功能, 關(guān)于導(dǎo)出數(shù)據(jù)到Excel并無什么新奇可言,網(wǎng)絡(luò)上到處都是,但基本上都是一種模式,通過DataGrid 把數(shù)據(jù)導(dǎo)出到Excel的方式。
前些時間有朋友為了完成此功能,就硬把數(shù)據(jù)導(dǎo)入DataGrid再導(dǎo)出到Excel。這實在是多此一舉。
解決辦法:
通過Linq將數(shù)據(jù)讀出,并直接寫入數(shù)據(jù)流中
代碼如下:
復(fù)制代碼 代碼如下:

public partial class DataToExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataAccess.DataClassesDataContext db = new DataClassesDataContext();
var qu = from t in db.TXLInfos
select t;
Response.AppendHeader("Content-Disposition", "attachment;filename=result.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "gb2312";
Response.ContentEncoding = Encoding.GetEncoding("gb2312");
System.IO.StringWriter writer = new System.IO.StringWriter();
foreach(TXLInfo item in qu)
{
writer.Write(item.GQName);
writer.Write("\t");
writer.Write(item.GQID);
writer.WriteLine();
}
Response.Write(writer.ToString());
Response.End();
}
}
注:"\t"默認(rèn)做為Excel中兩列之間的分隔符號

相關(guān)文章

最新評論