2016-07-28 69 views
-3

我通过以下代码向相应的用户发送邮件,邮件地址为attachment如何在将Excel文件移出之前过滤数据集中的数据?

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("[email protected]", "", "", "Information on documents for processing", sbodyMail, 
           "AUTOSQL", "Powersoft", ls_attach1, "ConnectionString"); 

       } 
    } 

当前,每个用户都可以看到Excel工作表中的所有数据。

应该是这样的,用户只能看到它自己的数据。对于前:

JG

对于防爆:扯谈么应该能够看到在Excel工作表中只有两排,但他能够查看所有记录。

我该怎么做?

+0

过滤主数据集你在哪里作出这样的选择?你做了一个GROUPING,但是你没有选择你想要用一段代码显示哪些数据。或者我只是没有看到它? –

+0

我认为你不是从excel工作表中筛选你想要的数据。 –

+0

据我所知,你只需附加一个Excel表格,而不需要做任何事情。你期望发生什么? –

回答

-1

这里是代码片段加入其中,你将能够过滤DataSet

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 
     } 

    } 
+0

如何根据我的代码使其工作? – BNN

+0

你想过滤'DataSet'并且共享代码将完成这项工作。您的数据集将被过滤并仅包含用户特定的数据,您可以将其转换为Excel并作为附件发送。您可以获取所有用户的列表,并逐个使用它们来过滤数据集,使用每个过滤器创建Excel和作为附件发送。 – Lara

+0

但''用户名=玛雅Vaidya和Ref_no = 8G“;'这是硬代码。我需要把它变成动态的。怎么做。帮助我 – BNN