多个gridview作为单个excel文件的多个工作表

时间:2014-06-09 06:17:37

标签: c# asp.net-mvc-4 gridview datatable

我有两个数据表。我想为每个人制作两个网格视图。然后我想在一个excel文件中渲染这些网格视图,但是作为两个单独的工作表。我搜索过这个,但没有得到任何我想要的东西。任何人都可以指路吗?

以下是使用一个gridview的单个工作表的示例代码:

DataTable dt = createDataTbl(); // creating a datatable
grid.DataSource = dt;
grid.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("content-disposition", "attachment; filename=sample.xls");

Response.Charset = "";
StringWriter sw = new StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

grid.HeaderRow.Style.Add("background-color", "#007ACC");
grid.RenderControl(htw);

Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
byte[] fileContents = System.Text.Encoding.UTF8.GetBytes(Response.ToString());
return new FileContentResult(fileContents, "application/vnd.ms-excel");

0 个答案:

没有答案