asp.net Linq把數(shù)據(jù)導(dǎo)出到Excel的代碼
更新時(shí)間: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í)間有朋友為了完成此功能,就硬把數(shù)據(jù)導(dǎo)入DataGrid再導(dǎo)出到Excel。這實(shí)在是多此一舉。
解決辦法:
通過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中兩列之間的分隔符號
解決辦法:
通過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中通過ALinq讓Mysql操作變得如此簡單
- asp.net Linq TO Sql 分頁方法
- asp.net下Linq To Sql注意事項(xiàng)小結(jié)
- asp.net中一個linq分頁實(shí)現(xiàn)代碼
- asp.net 根據(jù)漢字的拼音首字母搜索數(shù)據(jù)庫(附 LINQ 調(diào)用方法)
- asp.net Linq to Xml學(xué)習(xí)筆記
- asp.net LINQ中數(shù)據(jù)庫連接字符串的問題
- asp.net Linq To Xml上手Descendants、Elements遍歷節(jié)點(diǎn)
- asp.net使用LINQ to SQL連接數(shù)據(jù)庫及SQL操作語句用法分析
相關(guān)文章
ASP.NET數(shù)據(jù)庫編程之Access連接失敗
ASP.NET數(shù)據(jù)庫編程之Access連接失敗...2006-09-09
asp.net listbox實(shí)現(xiàn)單選全選取消
這篇文章主要介紹了asp.net listbox單選全選取消的應(yīng)用,需要的朋友可以參考下2014-02-02
asp.net 數(shù)據(jù)綁定 使用eval 時(shí)候報(bào) 字符文本中的字符太多 問題的解決方法
asp.net 數(shù)據(jù)綁定 使用eval 時(shí)候報(bào) 字符文本中的字符太多 問題解決,需要的朋友可以參考下。2010-09-09
asp.net用Zxing庫實(shí)現(xiàn)條形碼輸出的具體實(shí)現(xiàn)
這篇文章主要介紹了asp.net用Zxing庫實(shí)現(xiàn)條形碼輸出的具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-12-12

