[英]Separate Count for Two Groups
我分別創建了兩個GridViews: Members and Sponsors
並提供了這兩組人的total count
。 我的問題是我有兩個在Excel中打開的按鈕: MemSpreadshtBTN (會員)和SPSpreadshtBTN (贊助商),我無法弄清楚如何僅為會員獲取計數,另一個只為Excel格式的贊助商。 請幫忙。
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#
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();
}
為此,您需要使用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();
}
在“成員”按鈕中,單擊傳遞列值。 這應該可以解決問題。 來源: 從DataTable將數據插入Excel工作表
通過在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
調用對於成員SponsorsIDNum is null
,對於SponsorsIDNum is null
方法#2調用對於Select語句中的SponsorsIDNum is not null
。 之后,我更改了以下內容並且有效:
MemSpreadshtBTN
htmlWrite.Write(MembertotalCountLBL.Text.ToString() + "<br />");
SPSpreadshtBTN
htmlWrite.Write(SponsortotalCountLBL.Text.ToString() + "<br />");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.