1

I have a class

 public class AccCsv
    {
       
        public string IdNumber { get; set; }
       
        public string Firstname { get; set; }
       
        public string Lastname { get; set; }
      
        public string AccountNumber { get; set; }
}

I am trying to read these values from an excel document. My service for reading the doc looks like so

 public List<debtorCsv> GetAllDebtorImportedData(string path)
        {
            List<AccCsv> records;

            using (var reader = new StreamReader(path))
            using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
            {
                records = csv.GetRecords<AccCsv>().ToList();
               
            }
            return records;
        }

However, when I run it I run into an error that says "Header with name IdNumber[0] was not found" (for all headers). I do have headers in my excel doc. Does anyone who has used CSVhelper know what my error could be Thanks

Snap of my data: sample data

Megatron213
  • 81
  • 1
  • 12
  • Could you provide us an example of the data you're trying to convert so we can test it ourselves? – msmolcic Dec 04 '20 at 14:16
  • I have edited my question to include a snap of my data – Megatron213 Dec 04 '20 at 14:22
  • CSVhelper can only read text files that contain Comma Separated Values. So **not** Excel documents, unless you use Excel to explicitly save them in that format. Google "c# read excel .docx file" to possibly get ahead. – Hans Passant Dec 04 '20 at 14:33
  • Can you show the raw sample data instead of from Excel? – Josh Close Dec 05 '20 at 15:17
  • Thanks, I realized it was my data conversion from Excel that was not working properly. However, I am struggling to find a way to have some of the fields empty. I get a runtime error if it finds a header without a value. – Megatron213 Dec 08 '20 at 08:08
  • Does this answer your question? [How to configure CsvHelper to skip MissingFieldFound rows](https://stackoverflow.com/questions/52589868/how-to-configure-csvhelper-to-skip-missingfieldfound-rows) – Drag and Drop Apr 20 '21 at 12:23

2 Answers2

1

I found that similar issue may occur if you do not have a public, parameter-less constructor defined for your model class.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 09 '22 at 06:57
0

Have you tried to apply the decoration like this: [Name("IdNumber")] to your POCOS (as docs says : https://joshclose.github.io/CsvHelper/getting-started/) ?

something like:

 public class AccCsv
    {
     
        [Name("IdNumber")]  
        public string IdNumber { get; set; }
       
        [Name("Firstname")]
        public string Firstname { get; set; }
       
        [Name("Lastname")]
        public string Lastname { get; set; }
      
        [Name("AccountNumber")]
        public string AccountNumber { get; set; }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
federico scamuzzi
  • 3,708
  • 1
  • 17
  • 24