C# 在生成Excel文件之前,如何过滤数据集中的数据?
我正在通过下面的代码向相应的用户发送带有C# 在生成Excel文件之前,如何过滤数据集中的数据?,c#,asp.net,excel,email,C#,Asp.net,Excel,Email,我正在通过下面的代码向相应的用户发送带有附件的邮件 private void Form1_Load(object sender, EventArgs e) { Cls_Email_Sql ce = new Cls_Email_Sql(); List<string> ls_attach = new List<string>(); using (System.Data.SqlClient.SqlConnection c
附件的邮件
private void Form1_Load(object sender, EventArgs e)
{
Cls_Email_Sql ce = new Cls_Email_Sql();
List<string> ls_attach = new List<string>();
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(SqlConn))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
{
cmd.CommandText = "GET_INWARD_REMINDER_REPORT";
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
conn.Close();
DataTable tb_RA = ds.Tables[0];
DataTable tb_User = ds.Tables[1];
string strcolorHead = "#C0C0C0";
string strcolorDet = "#C0C0C0";
var groups = tb_RA.AsEnumerable().GroupBy(r => r.Field<string>("RAName")); // grouping the RA column
foreach (var group in groups) // RA Table
{
sbodyMail = "Dear " + group.Key + ", <br /><br /> " +
"As per the details available in the system, below are the summary "+
"of number of documents lying with your reportees for more than five days. "+
"This is for your information and necessary action ";
sbodyMail += "<table style='width: 400px;font-size:12px;font-family: Arial, Helvetica, sans-serif;' " +
"border='0'><tr><td style='width: 100%;'></b><td></tr></table> " +
"<table style='width: 470px;font-size:12px; font-family: Arial, Helvetica, sans-serif;height: 53px' border='1'><tr> " +
"<td style='width: 30px; height: 14px;color:black;background-color:" + strcolorHead + " ;white-space:nowrap'><strong>SR No</strong></td> " +
"<td style='width: 300px; height: 14px;color:black;background-color:" + strcolorHead + " ;white-space:nowrap'><strong>UserName</strong></td> " +
"<td style='width: 120px; height: 14px;color:black;background-color:" + strcolorHead + " ;white-space:nowrap'><strong>Document type</strong></td> " +
"<td style='width: 20px; height: 14px;color:black;background-color:" + strcolorHead + " ;white-space:nowrap'><strong><div>No. of docs working </div><div> for more than five days</div></strong></td> ";
foreach (var row in group)
{
sbodyMail += "<tr>" +
"<td style='width: 30px; height: 14px;background-color:" + strcolorDet + "'>" + row["SR_No"].ToString() + " </td> " +
"<td style='width: 100px; height: 14px;background-color:" + strcolorDet + "'>" + row["userName"].ToString() + " </td> " +
"<td style='width: 100px; height: 14px;background-color:" + strcolorDet + "'>" + row["Document_Type"].ToString() + " </td> " +
"<td style='width: 100px; height: 14px;background-color:" + strcolorDet + "'>" + row["CountofDocNo"].ToString() + " </td> " +
"</tr>";
}
sbodyMail += "</table><br>" + //close of header
"<b>THIS IS A SYSTEM GENERATED MAIL. PLEASE DO NOT REPLY </b>";
string startupPath = "";
List<string> ls_attach1 = new List<string>();
MailMessage mail = new MailMessage();
startupPath = Environment.CurrentDirectory;
ls_attach1.Add(startupPath + "\\Attachment\\Reminder_Sheet.xls");
ExcelLibrary.DataSetHelper.CreateWorkbook(startupPath + "\\Attachment\\Reminder_Sheet.xls", ds);
foreach (var attach in ls_attach1)
{
mail.Attachments.Add(new Attachment(attach));
}
foreach (Attachment attachments in mail.Attachments)
{
attachments.Dispose();
}
ce.SendEmail("nadeem.khan@powersoft.in", "", "", "Information on documents for processing", sbodyMail,
"AUTOSQL", "Powersoft", ls_attach1, "ConnectionString");
}
}
private void Form1\u加载(对象发送方,事件参数e)
{
Cls_Email_Sql ce=新的Cls_Email_Sql();
列表ls_attach=新列表();
使用(System.Data.SqlClient.SqlConnection conn=new System.Data.SqlClient.SqlConnection(SqlConn))
{
使用(System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient.SqlCommand())
{
cmd.CommandText=“获取内部提醒报告”;
cmd.Connection=conn;
cmd.CommandType=CommandType.storedProcess;
conn.Open();
System.Data.SqlClient.SqlDataAdapter=new System.Data.SqlClient.SqlDataAdapter(cmd);
数据集ds=新数据集();
适配器。填充(ds);
康涅狄格州关闭();
DataTable tb_RA=ds.Tables[0];
DataTable tb_User=ds.Tables[1];
字符串strcolorHead=“#c0”;
字符串strcolorDet=“#c0”;
var groups=tb_RA.AsEnumerable().GroupBy(r=>r.Field(“RAName”);//对RA列进行分组
foreach(组中的var组)//RA表
{
sbodyMail=“亲爱的”+group.Key+,
+
“根据系统中提供的详细信息,以下是摘要”+
“与您的报告对象在一起超过五天的文档数。”+
“这是为您提供信息和必要的行动”;
sbodyMail+=“”+
" " +
“SR No”+
“用户名”+
“文档类型”+
“工作超过五天的文件数量””;
foreach(组中的var行)
{
sbodyMail+=“”+
“+行[“序号]。ToString()+”+
“”+行[“用户名”]。ToString()+“”+
“”+行[“文档类型”]。ToString()+“”+
“+行[“CountofDocNo”]。ToString()+”+
"";
}
sbodyMail+=“
”+//报头关闭
“这是系统生成的邮件,请不要回复”;
字符串startupPath=“”;
列表ls_attach1=新列表();
MailMessage mail=新的MailMessage();
startupPath=Environment.CurrentDirectory;
ls_attach1.Add(startupPath+“\\Attachment\\rements_Sheet.xls”);
ExcelLibrary.DataSetHelper.CreateWorkbook(startupPath+“\\附件\\提醒\u Sheet.xls”,ds);
foreach(在ls_attach1中附加变量)
{
mail.Attachments.Add(新附件(附件));
}
foreach(邮件中的附件。附件)
{
附件.Dispose();
}
ce.发送电子邮件(“nadeem。khan@powersoft.in“,”,“,”,“处理文件的信息”,sbodyMail,
“AUTOSQL”、“Powersoft”、ls_attach1、“ConnectionString”);
}
}
目前,每个用户都可以看到Excel工作表中的所有数据
应该是这样的,用户只能看到自己的数据。例如:
例如:Chetan Mody应该只能看到excel表中的两行,但他可以查看所有记录
我如何做到这一点?这是添加的代码片段,您将能够过滤数据集
You can use DataTable.Select:
var strExpr = "username = Maya Vaidya AND Ref_no = 8G";
var strSort = "Ref_date";
// Use the Select method to find all rows matching the filter.
foundRows = ds.Table[0].Select(strExpr, strSort);
上述代码可用于过滤数据集,结果可用于创建Excel,需要在邮件中作为附件发送
更新
1.首先填充主数据集
现在启动一个查询,以获取要基于其使用以下代码筛选主数据集的筛选值
using (SqlConnection sourceConnection =
new SqlConnection(connectionString))
{
sourceConnection.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand(
"SELECT * FROM " +
"dbo.Users group by user_Name;",
sourceConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
//Get the record which you can use to filter the main Dataset
//apply the above code snippet shared to filter main Dataset.
//Create Excel
//Send Mail
}
}
您在哪里进行选择?您进行了分组,但您没有选择要用显式代码显示的数据。或者我只是看不到它?我想是的,您没有从excel工作表中筛选您想要的数据。据我所知,您只是附加excel工作表,而不做任何操作。您希望发生什么n?@J.Steen:很抱歉,我错过了那部分代码,现在更新了问题。看一看并没有真正的帮助-除了a(大概)之外,您没有指定ds
变量包含什么名为GET_Injection_Remembers_REPORT的存储过程。然后将其推入excel文件,而不做任何其他操作。您希望筛选数据集,共享代码将完成此任务。您的数据集将被筛选,并且只包含用户特定的数据,您可以将其转换为excel并作为附件发送。您可以获取列出所有用户,并使用他们逐个筛选数据集,使用每个筛选器创建Excel并作为附件发送。但是“username=Maya Vaidya and Ref_no=8G”
这是硬代码。我需要将其动态化。如何实现。帮助我您可以在填充数据集之前启动另一个查询以获取记录,并获取要用作数据集中筛选器的详细信息。将查询返回的记录保存在某个集合中。现在迭代foreach循环。每次迭代都会筛选主循环数据集并获取相关数据,创建excel并触发邮件。也许你能用一段代码帮助同样的事情,而不是它会很好。因为我无法这样做更新答案。用