`
wsql
  • 浏览: 11776587 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

ASP.NET 把DataGrid数据导入到Excel中

 
阅读更多

private void Btn_ExportReport_Click(object sender, System.EventArgs e)

{

Response.Clear();

Response.Buffer = true;

Response.Charset = "GB2312";

Response.AppendHeader("Content-Disposition", "attachment;filename=CommuterReportExcel.xls");

// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!

Response.ContentEncoding = System.Text.Encoding.UTF8;

Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。

//Response.ContentType = "application/vnd.ms-excel";//输出类型

//Response.Charset = "";

//关闭 ViewState

EnableViewState = false;

System.IO.StringWriter tw = new System.IO.StringWriter();//将信息写入字符串

System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);//WEB窗体页上写出一系列连续的HTML特定字符和文本。

//此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能

//获取controlHTML

dgVidicon.RenderControl(hw);//table中的内容输出到HtmlTextWriter对象中

// HTML写回浏览器

Response.Write(tw.ToString());

Response.Flush();

Response.End();

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics