将多个网格视图导出到多个Excel表单

我在我的网站有3个gridviews我需要出口到Excel,但我需要每个gridview出现在不同的工作表。

此链接导出GridView到多个Excel工作表使用非常相似的东西导出多个网格视图到多个Excel标签(工作表)

为了导出多个gridviews到多个工作表,我使用.NET,我已经下载了ClosedXML和DocumentFormat.OpenXml.dll 。

在Web应用程序中,我没有错误,但是XLS输出是空的。

任何人都知道如何解决这个问题?

你能提出其他方法吗?

先谢谢你。

请检查下面的代码。

protected void btnExportBoth_Click(object sender, EventArgs e) { XLWorkbook wb = new XLWorkbook(); GridView[] gvExcel = new GridView[] { gv1, gv2, gv3 }; string[] name = new string[] { "gv1", "gv2", "gv3" }; for (int i = 0; i < gvExcel.Length; i++) { if (gvExcel[i].Visible) { gvExcel[i].AllowPaging = false; gvExcel[i].DataBind(); DataTable dt = new DataTable(name[i].ToString()); for (int z = 0; z < gvExcel[i].Columns.Count; z++) { dt.Columns.Add(gvExcel[i].Columns[z].HeaderText); } foreach (GridViewRow row in gvExcel[i].Rows) { dt.Rows.Add(); for (int c = 0; c < row.Cells.Count; c++) { dt.Rows[dt.Rows.Count - 1][c] = row.Cells[c].Text; } } wb.Worksheets.Add(dt); gvExcel[i].AllowPaging = true; } } Response.Clear(); Response.Buffer = true; Response.Charset = ""; Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment;filename=Workbook_Name.xlsx"); using (MemoryStream MyMemoryStream = new MemoryStream()) { wb.SaveAs(MyMemoryStream); MyMemoryStream.WriteTo(Response.OutputStream); Response.Flush(); Response.End(); } } 

你确定已经填充btnExportBoth_Click方法的gridview?

  gvExcel[i].AllowPaging = false; gvExcel[i].DataBind(); 

btnExportBoth_Click方法中,您必须调用方法来在网页上填充gridview。