110,534
社区成员
发帖
与我相关
我的任务
分享
protected void btnExcel_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "盘点结果表.xls");//调用导出方法
}
///导出Excel方法
private void Export(string FileType, string FileName)
{
//清楚分页
gridView.AllowPaging = false;
BindData() ;
Response.Clear();
Response.Buffer = true;
//设定输出的字符集
Response.Charset = "GB2312";
//解决导出到Excel2007乱码问题
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");
//假定导出的文件名为盘点结果表.xls
Response.AddHeader("Content-Disposition", "attachment; filename=" +DateTime.Now.ToString("_yyyyMMdd_HHmmss") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
//解决导出到Excel2007乱码问题
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//设置导出文件的格式
Response.ContentType = FileType;
//关闭ViewState
this.EnableViewState = false;
StringWriter stringWriter = new StringWriter();
HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);
gridView.RenderControl(textWriter);
//把HTML写回浏览器
Response.Write(stringWriter.ToString());
Response.Flush();
Response.End();
gridView.AllowPaging = true;//恢复分页
//为GridView重新绑定数据源
BindData();
}