11

I am using CSV Helper library to produce CSV files for the user to to populate and upload into the system. My issue is that the WriteHeader method just writes the attributes of a class with names like "PropertyValue", which is not user friendly. Is there a method I can use to make the text produced user friendly but is still able to successfully map the class to the files data?

My code looks like the following:

public ActionResult UploadPropertyCSV(HttpPostedFileBase file)
    {
        List<PropertyModel> properties = new List<PropertyModel>();
        RIMEDb dbContext = new RIMEDb();
        bool success = false;
        foreach (string requestFiles in Request.Files)
        {
            if (file != null && file.ContentLength > 0 && file.FileName.EndsWith(".csv")) 
            {
                using(StreamReader str = new StreamReader(file.InputStream))
                {
                    using(CsvHelper.CsvReader theReader = new CsvHelper.CsvReader(str))
                    {
                        while (theReader.Read())
                        {
                            RIMUtil.PropertyUploadCSVRowHelper row = new RIMUtil.PropertyUploadCSVRowHelper()
                            {
                                UnitNumber = theReader.GetField(0),
                                StreetNumber = theReader.GetField(1),
                                StreetName = theReader.GetField(2),
                                AlternateAddress = theReader.GetField(3),
                                City = theReader.GetField(4)
                            };

                            Property property = new Property();
                            property.UnitNumber = row.UnitNumber;
                            property.StreetNumber = row.StreetNumber;
                            property.StreetName = row.StreetName;
                            property.AlternateAddress = row.AlternateAddress;
                            property.City = dbContext.PostalCodes.Where(p => p.PostalCode1 == row.PostalCode).FirstOrDefault().City;

                            dbContext.Properties.Add(property);
                            try
                            {
                                dbContext.SaveChanges();
                                success = true;
                            }
                            catch(System.Data.Entity.Validation.DbEntityValidationException ex)
                            {
                                success = false;
                                RIMUtil.LogError("Ptoblem validating fields in database. Please check your CSV file for errors.");
                            }
                            catch(Exception e)
                            {
                                RIMUtil.LogError("Error saving property to database. Please check your CSV file for errors.");
                            }
                        }
                    }
                }
            }
        }
        return Json(success);
    }

I'm wondering if theres some metadata tag or something I can put on top of each attribute in my PropertyUploadCSVRowHelper class to put the text I want produced in the file

Thanks in advance

1
  • You should look into CsvHelper Mappings. This is what you are referring to in your question. Commented Jun 27, 2016 at 3:01

2 Answers 2

18

Not sure if this existed 2 years ago but now, we can change the property/column name by using the following attribute function:

[CsvHelper.Configuration.Attributes.Name("Column/Field Name")] 

Full code:

using CsvHelper;
using System.Collections.Generic;
using System.IO;

namespace Test
{
    class Program
    {
        class CsvColumns
        {
            private string column_01;

            [CsvHelper.Configuration.Attributes.Name("Column 01")] // changes header/column name Column_01 to Column 01
            public string Column_01 { get => column_01; set => column_01 = value; }
        }

        static void Main(string[] args)
        {
            List<CsvColumns> csvOutput = new List<CsvColumns>();
            CsvColumns rows = new CsvColumns();
            rows.Column_01 = "data1";
            csvOutput.Add(rows);

            string filename = "test.csv";
            using (StreamWriter writer = File.CreateText(filename))
            {
                CsvWriter csv = new CsvWriter(writer);
                csv.WriteRecords(csvOutput);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

This might not be answering your question directly as you said you wanted to use csvhelper, but if you're only writing small size files (this is a simple function that I use to generate csv. Note, csvhelper will be much better for larger files as this is just building a string and not streaming the data.

Just customise the columns array in the code below variable to suit your needs.

public string GetCsv(string[] columns, List<object[]> data)
{
    StringBuilder CsvData = new StringBuilder();

    //add column headers
    string[] s = new string[columns.Length];
    for (Int32 j = 0; j < columns.Length; j++)
    {
        s[j] = columns[j];
        if (s[j].Contains("\"")) //replace " with ""
            s[j].Replace("\"", "\"\"");
        if (s[j].Contains("\"") || s[j].Contains(" ")) //add "'s around any string with space or "
            s[j] = "\"" + s[j] + "\"";
    }
    CsvData.AppendLine(string.Join(",", s));

    //add rows
    foreach (var row in data)
    {
        for (int j = 0; j < columns.Length; j++)
        {
            s[j] = row[j] == null ? "" : row[j].ToString();
            if (s[j].Contains("\"")) //replace " with ""
                s[j].Replace("\"", "\"\"");
            if (s[j].Contains("\"") || s[j].Contains(" ")) //add "'s around any string with space or "
                s[j] = "\"" + s[j] + "\"";
        }
        CsvData.AppendLine(string.Join(",", s));
    }

    return CsvData.ToString();

}

Here is a fiddle example of how to use it: https://dotnetfiddle.net/2WHf6o

Good luck.

1 Comment

I've added in my code changes to help with explaining my problem.Yours sounds like a good solution but we need to use the CSVHelper library. Do you know if there is some metadata field I can use to produce difference text when writing the header for each attribute of the class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.