两组的单独计数

时间:2015-02-19 20:30:50

标签: c# asp.net excel

我分别创建了两个GridViews: Members and Sponsors并提供了这两组人员的total count。我的问题是我有两个在Excel中打开的按钮: MemSpreadshtBTN (会员)和 SPSpreadshtBTN (赞助商)我无法弄清楚如何只为会员和另一个只是Excel格式的赞助商。请帮忙。

ASPX

<asp:Label ID="totalCountLBL" runat="server" Text="Total Count:" Visible="false"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button
        ID="MemSpreadshtBTN" runat="server" OnClick="MemSpreadshtBTN_OnClick" Text="Member Spreadsheet" Visible="false" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button
        ID="SPSpreadshtBTN" runat="server" OnClick="SPSpreadshtBTN_OnClick" Text="Sponsor Spreadsheet" Visible="false" /><br />
<asp:GridView ID="GridView1"...>
...
<asp:GridView ID="GridView2"...>
...

C#

protected void TotalCount()
{
    conn.Open();
    try
    {
        string strTotalCt = "SELECT count(ConferenceRegistrationID) FROM ConferenceRegistration cr, Conference con WHERE con.ConferenceID=cr.ConferenceIDNum AND con.ConferenceID=@confID AND cr.Deleted='N'";
        SqlCommand ChkTotal = new SqlCommand(strTotalCt, conn);
        ChkTotal.Parameters.AddWithValue("@confID", conferenceDDL.SelectedValue);

        int temp = Convert.ToInt32(ChkTotal.ExecuteScalar().ToString());
        if (temp > 0)
        {
            totalCountLBL.Text = "<strong>Total Count:" + temp + "</strong>";
            totalCountLBL.Visible = true;
            MemSpreadshtBTN.Visible = true;
            SPSpreadshtBTN.Visible = true;
            returnLBL.Text = "<div style='margin-top: 10px;'><a href='~/Admin/Participants.aspx' onclick='window.history.go(-1); return false;'>Return to previous page</a>.</div>";
            returnLBL.Visible = true;
        }
        else
        {
            ChkOther();
        }
    }
    finally
    {
        conn.Close();
    }
}


protected void MemSpreadshtBTN_OnClick(object sender, EventArgs e)
{
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=ParticipantsSpreadsheet.xls");
    Response.ContentType = "application/vnd.xls";

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();

    System.Web.UI.HtmlTextWriter htmlWrite =
    new HtmlTextWriter(stringWrite);

    htmlWrite.Write(totalCountLBL.Text.ToString() + "<br />");
    GridView1.RenderControl(htmlWrite);

    Response.Write(stringWrite.ToString());

    Response.End();
}

protected void SPSpreadshtBTN_OnClick(object sender, EventArgs e)
{
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=ParticipantsSpreadsheet.xls");
    Response.ContentType = "application/vnd.xls";

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();

    System.Web.UI.HtmlTextWriter htmlWrite =
    new HtmlTextWriter(stringWrite);

    htmlWrite.Write(totalCountLBL.Text.ToString() + "<br />");
    GridView2.RenderControl(htmlWrite);

    Response.Write(stringWrite.ToString());

    Response.End();
}

2 个答案:

答案 0 :(得分:0)

为此,您需要使用nuget安装excel包。搜索'excelpackage'并安装它。

public DataTable gettable(int SponsorIDNum)
        {
        SqlConnection thisConnection = new SqlConnection("server=.;database=local;Integrated Security=SSPI")
        string query = "select * from tbluser where companyID =" +companyID;
        SqlDataAdapter ad = new SqlDataAdapter(query, thisConnection);
        DataSet ds = new DataSet();
        ad.Fill(ds, "Categories");
        DataTable dt = ds.Tables[0];
        return dt;
        }
    protected void Member_Click(object sender, EventArgs e)
        {
        try
            {
            var pck = new OfficeOpenXml.ExcelPackage();
            var ws = pck.Workbook.Worksheets.Add("Name of the Worksheet");
            // get your DataTable
            var tbl = gettable(0);
            ws.Cells["A1"].LoadFromDataTable(tbl, true, OfficeOpenXml.Table.TableStyles.Medium6);

            var dataRange = ws.Cells[ws.Dimension.Address.ToString()];
            dataRange.AutoFitColumns();

            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;  filename=NameOfExcelFile.xlsx");
            Response.BinaryWrite(pck.GetAsByteArray());
            }
        catch (Exception ex)
            {
            // log exception
            throw;
            }
        Response.End();

        }

    protected void Sponsor_Click(object sender, EventArgs e)
        {
        try
            {
            var pck = new OfficeOpenXml.ExcelPackage();
            var ws = pck.Workbook.Worksheets.Add("Name of the Worksheet1");
            // get your DataTable
            var tbl = gettable(1);
            ws.Cells["A1"].LoadFromDataTable(tbl, true, OfficeOpenXml.Table.TableStyles.Medium6);

            var dataRange = ws.Cells[ws.Dimension.Address.ToString()];
            dataRange.AutoFitColumns();

            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;  filename=NameOfExcelFile.xlsx");
            Response.BinaryWrite(pck.GetAsByteArray());
            }
        catch (Exception ex)
            {
            // log exception
            throw;
            }
        Response.End();
        }

在“成员”按钮中,单击传递列值。这应该可以解决问题。 来源:Inserting data into an excel sheet from a DataTable

答案 1 :(得分:0)

通过在ASPX上添加2个标签解决了我的问题:

<asp:Label ID="MembertotalCountLBL" runat="server" Text="Total Count:" Visible="false"></asp:Label>
<asp:Label ID="SponsortotalCountLBL" runat="server" Text="Total Count:" Visible="false"></asp:Label>

然后在C#中添加了两个类似于TotalCount()的方法,除了方法#1为成员调用SponsorsIDNum is null,方法#2调用选择中的赞助商{/ 1}}强烈的>陈述。之后,我更改了以下内容并且有效:

MemSpreadshtBTN

SponsorsIDNum is not null

SPSpreadshtBTN

htmlWrite.Write(MembertotalCountLBL.Text.ToString() + "<br />");