【问题标题】:How to extract data from stored procedure and dynamically create a zip folder and add csv file如何从存储过程中提取数据并动态创建 zip 文件夹并添加 csv 文件
【发布时间】:2021-11-08 11:49:56
【问题描述】:

下面是我想要实现的,我下面的编码是我到目前为止所做的。

我不确定我正在做的是否是完成这项任务的最佳方法。

问题首先是动态创建一个 zip 文件夹并添加一个文件,以便我可以通过电子邮件发送它。

  1. 查询存储过程并获取数据
  2. 将返回的数据从存储过程转换为 CSV 文件。
  3. 动态创建一个 zip 文件夹并存储 CSV 文件。
  4. Zip 文件和 CSV 名称应该相同。 (名称将来自日期范围,例如“yyy-dd-mm”)
  5. 将 zip 文件夹附加到并通过电子邮件发送给客户。
  6. 最后将zip文件夹保存到本地目录或保存。

请告知是否有更好的方法来完成我的任务。

虚拟数据:

DataTable dummyData = new DataTable();
//columns
dummyData.Columns.Add("Id", typeof(int));
dummyData.Columns.Add("Make", typeof(string));
dummyData.Columns.Add("Model", typeof(string));

//data
dummyData.Rows.Add(111, "Toyota", "Hilux");
dummyData.Rows.Add(102, "Isuzu", "KB");
dummyData.Rows.Add(212, "Mazda", "323");

写入 CSV 文件,保存到路径并发送电子邮件:

if (dummyData != null && dummyData.Rows.Count > 0)
{
    var fileName = "Testing11.csv";

    StringBuilder sb = new StringBuilder();

    string[] columnNames = dummyData.Columns.Cast<DataColumn>().Select(column => column.ColumnName).ToArray();
    sb.AppendLine(string.Join(",", columnNames));

    foreach (DataRow row in dummyData.Rows)
    {
        IEnumerable<string> fields = row.ItemArray.Select(field => string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\""));
        sb.AppendLine(string.Join(",", fields));
    }

    string newFile = @"C:\Testing\" + fileName + "";

    // for testing purpose
    if (File.Exists(newFile))
    {
        File.Delete(newFile);
    }

    // Write and save the file to directory.
    File.WriteAllText(newFile, sb.ToString());

    // Add file to Zip folder
    // Not to sure how i can zip it

    // Attach zip folder and send email.
    StringBuilder emailText = new StringBuilder();
    emailText.AppendLine("Good day,");
    emailText.AppendLine("<br/>");
    emailText.AppendLine("<br/>");
    emailText.AppendLine("Please find attached copy of the file.");
    emailText.AppendLine("<br/>");
    emailText.AppendLine("<br/>");
    emailText.AppendLine("Please don't hesitate to contact us if there is any discrepancies on the file.");
    emailText.AppendLine("<br/>");
    emailText.AppendLine("<br/>");
    emailText.AppendLine("Regards,");
    emailText.AppendLine("<br/>");
    emailText.AppendLine(company);
    emailText.AppendLine("<br/>");
    emailText.AppendLine("<br/>");
    emailText.AppendLine("This email was sent from an unattended email address!");

    // Send an email to client
    string emailSubject = newFile;
    SendEmail(emailTo, emailSubject, emailText.ToString(),newFile);
}
else
{
    throw new Exception("Not found!");
}

发送电子邮件:

public void SendEmail(string to, string subject, string body, string attachmentFileName = null)
{
        try
        {
            SmtpClient client = new SmtpClient("mySTP",25)
            {
                DeliveryMethod = SmtpDeliveryMethod.Network
            };

            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("emailfrom@example.com");

            foreach (string address in to.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                mailMessage.To.Add(address);

            mailMessage.IsBodyHtml = true;
            mailMessage.Body = body;
            mailMessage.Subject = subject;

            if (attachmentFileName != null)
            {
                mailMessage.Attachments.Add(new Attachment(attachmentFileName));
            }
            else 
            {
                throw new Exception("Oops! No attachment file found");
            }

            client.Send(mailMessage);
        }
        catch (Exception ex)
        {
            throw ex;
        }
}

【问题讨论】:

  • 我想你需要这个 API 来完成你的任务docs.microsoft.com/en-us/dotnet/api/…
  • 我的文件如何发送到 zip 文件夹
  • 您必须创建临时文件夹并将文件保存在那里进行归档。或者您可以使用ZipArchive 并手动向其中添加条目以避免撞到文件系统。

标签: c# email datatable zip export-to-csv


【解决方案1】:

我首先创建了一个临时文件夹,然后将文件转储到其中。之后,我将文件夹压缩并附加并发送到电子邮件。

第 1 步。

 string fileName = testingfile.csv;
 string newFile = @"C:\Testing\" + fileName + "";

然后我创建一个 zip 文件夹。我已根据请求为 zip 文件夹指定了与文件名相同的名称。

第 2 步。

 var zipFolder = fileName + ".zip";
 ZipFile.CreateFromDirectory(@"C:\Testing\", zipFolder);

现在我正在向客户发送一封电子邮件,并附上包含该文件的 zip 文件夹。

第 3 步。

string emailSubject = zipFolder;
SendEmail(emailTo, emailSubject, emailText.ToString(),zipFolder);

希望有一天能对某人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    相关资源
    最近更新 更多