asp.net导excel
public void ExportToExcel(System.Data.DataTable dt, string FileName)
{
DataGrid excel = new DataGrid();
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding( "GB2312 ");
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
if (dt != null)
{
curContext.Response.ContentType = "application/vnd.ms-excel ";
curContext.Response.AppendHeader( "Content-Disposition ", "attachment;filename= " + FileName);
curContext.Response.ContentEncoding = encoding;
curContext.Response.Charset = " ";
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
excel.DataSource = dt.DefaultView;
excel.AllowPaging = false;
excel.DataBind();
excel.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
strWriter.Close();
curContext.Response.End();
}
}
以上代码是将数据库里的数据导出了,现在问题是如何将页面上表格的标题写入EXCEL.来替换数据库里的字段名.
------解决方案--------------------SQL语句定错了.应是:
select ID as ID,StuNUmber as 学号,Class as 班级 from 表名
------解决方案--------------------不清楚你所说的中止线程指的是什么,不过你可以重写一下VerifyRenderingInServerForm
像以下这种方式重写就可以了
public override void VerifyRenderingInServerForm(Control control)
{
}