asp.net 如何将datatable作为数据源导出excel文件

我后台返回一个DataTable

public static DataTable GetTable()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["JYCMS"].ConnectionString);
string sql = "select NewsID,NewsTitle,CreatTime from jy_news";
con.Open();
SqlCommand cmd=new SqlCommand(sql,con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);

con.Close();
return ds.Tables[0];
}

这个里的数据作为数据源导出到ExceL文件中
而且要把标题NewsID,NewsTItle,CreateTime在EXCEL显示为相应的中文标题 新闻ID,新闻标题,发表时间
这样的 求教哪位会的

第1个回答  推荐于2016-03-17
你绑定的时候 直接把“NewsID,NewsTItle,CreateTime在EXCEL显示为相应的中文标题 新闻ID,新闻标题,发表时间 ” 设定好 直接导出到EXCEL就行

参考代码
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=file.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.这里写你的表格控件名.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();追问

导出为0B..报错

本回答被提问者采纳
第2个回答  2014-06-09
卓正软件的pageoffice还是跨浏览器的,你参考下吧
相似回答