[英]Separate Count for Two Groups
I created two GridViews: Members and Sponsors
respectively and provided a total count
of these two groups of people. 我分别创建了两个
GridViews: Members and Sponsors
并提供了这两组人的total count
。 My problem is I have two buttons which open in Excel: MemSpreadshtBTN (Member) and SPSpreadshtBTN (Sponsor) and I could not figure out how to get the count just for Members and the other just for Sponsors in Excel format. 我的问题是我有两个在Excel中打开的按钮: MemSpreadshtBTN (会员)和SPSpreadshtBTN (赞助商),我无法弄清楚如何仅为会员获取计数,另一个只为Excel格式的赞助商。 Please help.
请帮忙。
ASPX ASPX
<asp:Label ID="totalCountLBL" runat="server" Text="Total Count:" Visible="false"></asp:Label> <asp:Button
ID="MemSpreadshtBTN" runat="server" OnClick="MemSpreadshtBTN_OnClick" Text="Member Spreadsheet" Visible="false" /> <asp:Button
ID="SPSpreadshtBTN" runat="server" OnClick="SPSpreadshtBTN_OnClick" Text="Sponsor Spreadsheet" Visible="false" /><br />
<asp:GridView ID="GridView1"...>
...
<asp:GridView ID="GridView2"...>
...
C# 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();
}
For this to work you need to install the excel package using nuget. 为此,您需要使用nuget安装excel包。 search for 'excelpackage' and install it.
搜索'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();
}
In the Members button click pass the column value. 在“成员”按钮中,单击传递列值。 This should solve the problem.
这应该可以解决问题。 Source: Inserting data into an excel sheet from a DataTable
来源: 从DataTable将数据插入Excel工作表
Resolved my problem by adding 2 labels on ASPX: 通过在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>
Then added 2 methods similar to TotalCount() in C#;, except method #1 calls for SponsorsIDNum is null
for Members and method #2 calls for SponsorsIDNum is not null
for Sponsors in the Select statements. 然后在C#中添加了两个类似于TotalCount()的方法,除了方法#1对
SponsorsIDNum is null
调用对于成员SponsorsIDNum is null
,对于SponsorsIDNum is null
方法#2调用对于Select语句中的SponsorsIDNum is not null
。 Afterwhich, I changed the followings and it works: 之后,我更改了以下内容并且有效:
MemSpreadshtBTN MemSpreadshtBTN
htmlWrite.Write(MembertotalCountLBL.Text.ToString() + "<br />");
SPSpreadshtBTN SPSpreadshtBTN
htmlWrite.Write(SponsortotalCountLBL.Text.ToString() + "<br />");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.