41996

Change the name of headers in CSV file using CSVHelper in C#

<h3>Question</h3>

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


<h3>Answer1:</h3>

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); } } } }
<h3>Answer2:</h3>

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.

来源:https://stackoverflow.com/questions/38044320/change-the-name-of-headers-in-csv-file-using-csvhelper-in-c-sharp

Recommend

  • Replace empty CSV value with NULL using sed
  • Storing output of os.system() in a variable
  • Project structure for EF/Silverlight application
  • 4D plot (3D+color) from 4 row vectors
  • parse dates with icalendar and compare to python datetime
  • if (isset($_POST not working for Internet Explorer
  • BigQuery Help - How to cast and convert to float and date format
  • Bootstrap Datepicker : Changing start date to tomorrow
  • Play Store In-App Billing custom price programmatically?
  • Inno Setup: Checking existence of a file in 32-bit System32 (Sysnative) folder
  • GWT Super Dev Mode
  • Django SearchVector on choices field
  • How to send custom logs to Google Play Developer Console?
  • How to retrieve data from Firebase Database with join if key only return true?
  • Apache Felix not able to access Postgres JDBC
  • Toolbar search local data, retain value in cell even after searching
  • REGEXP_REPLACE pattern has to be const? Comparing strings in BigQuery
  • Two way binding not working in ng-bootstrap radio button in an angular reactive form
  • Radio button in xamarin.ios
  • Two hosts in jdbc url
  • Http Requests not getting routed to Https NodeJs
  • Displaying XML file data in a HTML table
  • Why is this Animatable property being set again?
  • Python C binding error
  • Wrap array elements in divs based on same value
  • analytics.js event not working properly
  • Jenkins - could not read Username for 'https://github.com': No such device or address
  • Sample deviceQuery cuda program
  • Set initial vuetify v-select value
  • How to redirect into different page by user type in php and mysql
  • xpath assertion failure with dynamic xpath
  • Rotating Towards Path in OpenGL
  • C++ STL stack pop operation giving segmentation fault
  • XEP-0166: Jingle protocol implementation for voice/video chat in iOS
  • Sign a Pdf using custom digital signature in Java
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?