ASP.net(gridview导出的execl没有数据)求解给分
/// <summary>
/// 显示所有数据到grid表
/// </summary>
private void displayLog()
{
this.gSysLos.DataSource = SysLogManager.GetAllSysLog();
this.gSysLos.DataBind();
}
protected void btnExportLog_Click(object sender, EventArgs e)
{
if (gSysLos.Rows.Count > 0)
{
//调用导出方法
ExportGridViewForUTF8(gSysLos, DateTime.Now.ToShortDateString() + ".xls");
}
}
/// <summary>
/// 导出方法
/// </summary>
/// <param name="GridView"></param>
/// <param name="filename">保存的文件名称</param>
private void ExportGridViewForUTF8(ExtAspNet.Grid GridView, string filename)
{
string attachment = "attachment; filename=" + filename;
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", attachment);
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
------解决方案--------------------GridView.AllowPaging = false;//分页去掉
displayLog();//绑定数据
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView.RenderControl(htw);
------解决方案--------------------Response.Output.Write(sw.ToString());
改为
HttpContext.Current.Response.Write(sw.ToString());试试
------解决方案--------------------
应该不是没有导出数据吧,而是导出了一堆datarow类型的数据
你把gSysLos.Rows[RowNo]
应该改成gSysLos.Rows[RowNo].Cells[CloumnNo].Text。
我试了一下,就可以导出内容了
------解决方案-------------------- private void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
private void toExcelClk()
{
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
DataBindToGridview("order by id desc");
ToExcel(GridView1, "个人通讯录.xls");
GridView1.AllowPaging = true;
GridView1.AllowSorting = true;
DataBindToGridview("order by id desc");
}
protected void Button6_Click(object