将3个网格视图导出为3个不同的工作表

时间:2012-07-05 19:56:38

标签: c# asp.net sql excel

在我的网站上,我有一个调用方法的按钮,然后生成Excel报告。代码有效。问题是代码在同一个选项卡上显示所有网格视图(有3个)。我可以添加什么代码,以便当我单击按钮时,它将下载文档,但每个网格视图都显示在单独的选项卡上。选项卡名称将分别位于前1,前2和前3。所以为了澄清,现在所有的网格视图都显示在顶部1选项卡上,我需要再制作2个选项卡(前2和前3)并放入gridview2在顶部2选项卡上,以及顶部3选项卡上的gridview3。

using System;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
using System.IO;



public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    DataSet dataSet = new DataSet();

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ISALog1ConnectionString"].ToString());

    SqlCommand cmd = new SqlCommand("exec ProxyReport", conn);
    cmd.CommandTimeout = 200;

    SqlDataAdapter ad = new SqlDataAdapter(cmd);
    ad.Fill(dataSet);

    GridView1.DataSource = dataSet.Tables[0];
    GridView1.DataBind();
    GridView2.DataSource = dataSet.Tables[1];
    GridView2.DataBind();
    GridView3.DataSource = dataSet.Tables[2];
    GridView3.DataBind();

}
protected void Button1_Click(object sender, EventArgs e)
{
    string attachment = "attachment; filename=Top 1.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.RenderControl(htw);
    GridView2.RenderControl(htw);
    GridView3.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{

}


}

1 个答案:

答案 0 :(得分:3)

是的,您需要从codeplex:http://epplus.codeplex.com/添加EPPlus,然后将其添加到您的代码中。您可以使用它来添加任意数量的工作表:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using OfficeOpenXml;
    using System.Configuration;

    public partial class _Default : System.Web.UI.Page
    {
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
    GetExcel ge = new GetExcel();
    ge.ProcessRequest(HttpContext.Current);
}

public class GetExcel : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        DataSet dataSet = new DataSet();

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ISALog1ConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand("exec ProxyReport", conn);
        cmd.CommandTimeout = 200;
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        ad.Fill(dataSet);

       GridView1.DataSource = dataSet.Tables[0];
       GridView1.DataBind();
       GridView2.DataSource = dataSet.Tables[1];
       GridView2.DataBind();
       GridView3.DataSource = dataSet.Tables[2];
       GridView3.DataBind();   

        dataSet.Tables[0].TableName = "1";
        dataSet.Tables[1].TableName = "2";
        dataSet.Tables[2].TableName = "3";

        int count = 3;   

            MemoryStream ms = GetExcel.DataTableToExcelXlsx(dataSet, count);
            ms.WriteTo(context.Response.OutputStream);
            context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            context.Response.AddHeader("Content-Disposition", "attachment;filename=EPPlusData.xlsx");
            context.Response.StatusCode = 200;
            context.Response.End();

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public static MemoryStream DataTableToExcelXlsx(DataSet ds, int count)
    {
        MemoryStream Result = new MemoryStream();
        ExcelPackage pack = new ExcelPackage();

        for (int i = 1; i <= count; i++)
        {
            DataTable table = ds.Tables[i.ToString()];
            ExcelWorksheet ws = pack.Workbook.Worksheets.Add("MySheet" + i.ToString());

            int col = 1;
            int row = 1;

            foreach (DataColumn cl in table.Columns)
            {
                ws.Cells[row, col].Value = cl.ColumnName;
                col++;
            }
            col = 1;
            foreach (DataRow rw in table.Rows)
            {
                foreach (DataColumn cl in table.Columns)
                {
                    if (rw[cl.ColumnName] != DBNull.Value)
                        ws.Cells[row + 1, col].Value = rw[cl.ColumnName].ToString();
                    col++;
                }
                row++;
                col = 1;
            }
        }
        pack.SaveAs(Result);
        return Result;
    }
}}