asp.net Linq把數(shù)據(jù)導(dǎo)出到Excel的代碼
更新時(shí)間:2008年10月13日 23:37:24 作者:
最近有需要通過(guò)WEB把數(shù)據(jù)導(dǎo)出到Excel的功能, 關(guān)于導(dǎo)出數(shù)據(jù)到Excel并無(wú)什么新奇可言,網(wǎng)絡(luò)上到處都是,但基本上都是一種模式,通過(guò)DataGrid 把數(shù)據(jù)導(dǎo)出到Excel的方式。
前些時(shí)間有朋友為了完成此功能,就硬把數(shù)據(jù)導(dǎo)入DataGrid再導(dǎo)出到Excel。這實(shí)在是多此一舉。
解決辦法:
通過(guò)Linq將數(shù)據(jù)讀出,并直接寫入數(shù)據(jù)流中
代碼如下:
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中兩列之間的分隔符號(hào)
解決辦法:
通過(guò)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();
}
}
您可能感興趣的文章:
- asp.net中通過(guò)ALinq讓Mysql操作變得如此簡(jiǎn)單
- asp.net Linq TO Sql 分頁(yè)方法
- asp.net下Linq To Sql注意事項(xiàng)小結(jié)
- asp.net中一個(gè)linq分頁(yè)實(shí)現(xiàn)代碼
- asp.net 根據(jù)漢字的拼音首字母搜索數(shù)據(jù)庫(kù)(附 LINQ 調(diào)用方法)
- asp.net Linq to Xml學(xué)習(xí)筆記
- asp.net LINQ中數(shù)據(jù)庫(kù)連接字符串的問(wèn)題
- asp.net Linq To Xml上手Descendants、Elements遍歷節(jié)點(diǎn)
- asp.net使用LINQ to SQL連接數(shù)據(jù)庫(kù)及SQL操作語(yǔ)句用法分析
相關(guān)文章
ASP.NET數(shù)據(jù)庫(kù)編程之Access連接失敗
ASP.NET數(shù)據(jù)庫(kù)編程之Access連接失敗...2006-09-09
asp.net 頁(yè)面之間傳遞參數(shù)的幾種方法
因?yàn)樵陧?xiàng)目中需要在兩個(gè)頁(yè)面之間傳遞一些參數(shù),所以總結(jié)出以下幾個(gè)傳遞參數(shù)的方法2009-06-06
asp.net listbox實(shí)現(xiàn)單選全選取消
這篇文章主要介紹了asp.net listbox單選全選取消的應(yīng)用,需要的朋友可以參考下2014-02-02
asp.net 數(shù)據(jù)綁定 使用eval 時(shí)候報(bào) 字符文本中的字符太多 問(wèn)題的解決方法
asp.net 數(shù)據(jù)綁定 使用eval 時(shí)候報(bào) 字符文本中的字符太多 問(wèn)題解決,需要的朋友可以參考下。2010-09-09
asp.net用Zxing庫(kù)實(shí)現(xiàn)條形碼輸出的具體實(shí)現(xiàn)
這篇文章主要介紹了asp.net用Zxing庫(kù)實(shí)現(xiàn)條形碼輸出的具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-12-12
asp.net 頁(yè)面編碼常見(jiàn)問(wèn)題小結(jié)
2010-06-06

