|
解决办法:
通过Linq将数据读出,并直接写入数据流中
代码如下:
复制代码 代码如下:
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"默认做为Excel中两列之间的分隔符号
AspNet技术:asp.net Linq把数据导出到Excel的代码,转载需保留来源!
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。