日期:2014-05-18  浏览次数:20461 次

GridView导出到Excel 为什么有时候导出的是<Div>?
System.Web.HttpContext.Current.Response.Clear()
  System.Web.HttpContext.Current.Response.Buffer = True
  System.Web.HttpContext.Current.Response.Charset = "GB2312"
  Dim s As String
  s = "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8)
  Dim aType As String
  Select Case aFileType
  Case 0 'excle
  s = s + ".xls"
  aType = "application/ms-excel"
  Case 1 'word
  s = s + ".doc"
  aType = "application/ms-word"
  Case 2 'TXT 暂时不用
  s = s + ".txt"
  aType = "application/ms-txt"
  Case 3 'html
  s = s + ".html"
  aType = "application/ms-html"
  End Select
  System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", Trim(s))
  System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
 application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
  System.Web.HttpContext.Current.Response.ContentType = aType
  Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter
  Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
  aGridView.AllowPaging = False
  aGridView.AllowSorting = False
  aGridView.DataBind()
  aGridView.RenderControl(oHtmlTextWriter)
  System.Web.HttpContext.Current.Response.Output.Write(oStringWriter.ToString)
  System.Web.HttpContext.Current.Response.Flush()
  System.Web.HttpContext.Current.Response.End()

----------------
用上述代码导出有按钮列的GridView,有的页面成功,有的页面导出后却是<div> <div>
为什么呢?
GridView的数据源是用的SqlDataSource,但SqlDataSource.SelectCommand在Page_Load中给的。
有遇到同样问题的人吗?

------解决方案--------------------
你的问题是出现空值了吧

会不会是取值方法不对呢,

我用C#实现和你一样的工作的时候,

是直接从Gridview的dataSource去取值

我直接用DataSource的View

没有遇到过这种问题