在ASP.NET / C#中将3个GridView导出到Excel中的同一工作表

时间:2016-12-15 17:31:41

标签: c# asp.net export-to-excel

我试图找出如何将我的GridView1,GridView2和GridView3导出到Excel文档,因此所有这些GridView都将位于同一工作表中。

我有一个代码,允许我只导出一个GridViews:

这是我的C#button_Click事件代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClosedXML;
using ClosedXML.Excel;
using System.IO;


public partial class Page1 : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)
    { 

    }  

    protected void btnExportData_Click(object sender, EventArgs e)
        {

            string style = @"<style> .text { mso-number-format:\@; } </style> ";
            Response.ClearContent();

            Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");

            Response.ContentType = "application/excel";

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridView1.RenderControl(htw);

            // Style is added dynamically

            Response.Write(style);

            Response.Write(sw.ToString());
            Response.End();

        }
  }

如果您对如何将这3个GridView导出到Excel工作表有任何想法,请告诉我。谢谢!

0 个答案:

没有答案