我正在尝试导出我的列表视图,但不仅仅是可见的列表视图,我想导出 sqldatasource 的全部内容。该查询返回 20 列,列表视图上仅显示一小部分 (5)。有没有办法可以将整个 20 列导出到 excel 文件?
到目前为止我有这个:
protected void ExportToExcel(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=SearchResults.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
this.EnableViewState = false;
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
//I want to chagne this:
this.ResultsListView.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
而不是这一行:
this.ResultsListView.RenderControl(htmlTextWriter);
我想做类似的事情:
this.sqldatasource.RenderControl(htmlTextWriter);
但显然 SqlDataSource 没有 RenderControl 方法。有没有一种干净的方法来解决这个问题?