简体   繁体   中英

csvhelper: how to write a specific "cell" on an existing csv on C#?

I have a customer list in csv format which I'm using to send out emails. I would like to write to the CSV after each row has been executed in order to place a conditional rule. I'm using csvhelper to manipulate the file. Here's the code:

var scan = new StreamReader(myBlob);
var csvv = new CsvReader(scan, CultureInfo.InvariantCulture);
var records = csvv.GetRecords<Records>().ToList();
var scanwriter = new StreamWriter(myBlob4);
var csvwriter = new CsvWriter(scanwriter, CultureInfo.InvariantCulture);

foreach (Records record in records)
{
                
    var from = new EmailAddress("example.com", "John");
    var to = new EmailAddress(record.Email, record.Name);
            
    var subject = "exapmple";

    var msg = MailHelper.CreateSingleEmail(from, to, subject, txtf, htmlf);

    StringBuilder text = new StringBuilder();
    text.AppendFormat("sent", record.EmailSent);
                
    csvwriter.WriteField(record.EmailSent);
    csvwriter.NextRecord();
                
            
    var response = await client.SendEmailAsync(msg);
}

However my csv is not appending the "sent" value to the file under the emailsent column. I'm using StringBuilder which might not be helpful in this scenario.

It seems like you are trying to do something more like this.

void Main()
{
    var records = new List<SendEmail>
    {
        new SendEmail{ Email = "example.com", Name = "John" },
        new SendEmail{ Email = "example2.com", Name = "Jenny" }
    };
    var csvwriter = new CsvWriter(Console.Out, CultureInfo.InvariantCulture);
    
    foreach (var record in records)
    {
//      var from = new EmailAddress("example.com", "John");
//      var to = new EmailAddress(record.Email, record.Name);
//
//      var subject = "exapmple";
//
//      var msg = MailHelper.CreateSingleEmail(from, to, subject, txtf, htmlf);

        record.EmailSent = "sent";
        
        csvwriter.WriteRecord(record);
        csvwriter.NextRecord();

        //var response = await client.SendEmailAsync(msg);
    }
}

public class SendEmail
{
    public string Email { get; set; }
    public string Name { get; set; }
    public string EmailSent { get; set; }
}
//using blocks will make sure the streams and disposed and file handles are closed properly,
// **even if an exception is thrown **
using(var scan = new StreamReader(myBlob))
using (var csvv = new CsvReader(scan, CultureInfo.InvariantCulture))
using (var scanwriter = new StreamWriter(myBlob4))
using (var csvwriter = new CsvWriter(scanwriter, CultureInfo.InvariantCulture))
{
    var records = csvv.GetRecords<Records>(); //ToList() was not needed or helpful here
    foreach (var record in records)
    {               
        var from = new EmailAddress("example.com", "John");
        var to = new EmailAddress(record.Email, record.Name);
            
        var subject = "example";
        var msg = MailHelper.CreateSingleEmail(from, to, subject, txtf, htmlf);
                
        csvwriter.WriteField($"sent {record.EmailSent}");
        csvwriter.NextRecord();           
            
        var response = await client.SendEmailAsync(msg);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM