Asp.net 将网格传递给用于导出到excel的函数
我有一个方法用于将数据导出到excel。到目前为止,我一直在将表传递给该方法。但现在我希望传递网格数据,这样我就不必调用不同实例的过程来获得不同的过滤数据集。 有没有办法做到这一点Asp.net 将网格传递给用于导出到excel的函数,asp.net,grid,Asp.net,Grid,我有一个方法用于将数据导出到excel。到目前为止,我一直在将表传递给该方法。但现在我希望传递网格数据,这样我就不必调用不同实例的过程来获得不同的过滤数据集。 有没有办法做到这一点 public void ExportToExcel(DataSet ds) { if (ds.Tables[0].Rows.Count > 0) { string filename = ds.Tables[1].Rows[0]["file
public void ExportToExcel(DataSet ds)
{
if (ds.Tables[0].Rows.Count > 0)
{
string filename = ds.Tables[1].Rows[0]["filename"].ToString() + ".xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = ds.Tables[0];
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.Clear();
Response.ClearHeaders();
Response.Charset = "";
Response.AddHeader("content-disposition", String.Concat("attachment;filename=", filename));
Response.AddHeader("Cache-Control", "max-age=0");
Response.ContentType = "application/vnd.xls";
// this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
这是我用于导出到excel的方法。是否要将数据集导出到excel文件?如果你能给你看代码,我想要的是我用网格中的任何东西填充一个数据表(已经填充了另一个数据表,我必须为它调用一个方法)。我想避免调用方法,因为在不同的条件下,我需要调用不同的方法(不同的表).我想简化..当我填充网格时..我想传递给单击即可工作的exportToExcel方法的内容相同..我甚至不能在每次填充网格时调用exportToExcel方法。所以问题就来了,为什么不直接将网格导出到excel中呢?