Created
March 25, 2013 15:04
-
-
Save afreeland/5237760 to your computer and use it in GitHub Desktop.
C#: Import CSV file and get CSV headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public JsonResult ImportCSV(HttpPostedFileBase file) | |
{ | |
if (file == null) | |
return Json(new JSON.Result() { Status = JSON.Result.StatusEnum.Failed, DisplayMessage = "No Upload File Present" }); | |
if (file.ContentLength == 0) | |
return Json(new JSON.Result() { Status = JSON.Result.StatusEnum.Failed, DisplayMessage = "File does not have Content Length" }); | |
var delimiter = ","; | |
var lineNumber = 0; | |
var splitExpression = new Regex(@"(" + delimiter + @")(?=(?:[^""]|""[^""]*"")*$)"); | |
string[] headers = null; | |
using (var reader = new StreamReader(file.InputStream)) | |
{ | |
string line = null; | |
line = reader.ReadLine(); | |
headers = splitExpression.Split(line).Where(x => x != delimiter).ToArray(); | |
} | |
return Json(new JSON.Result() { Status = JSON.Result.StatusEnum.Success, ReturnedData = JSON.Serialize(headers), DisplayMessage = "Headers" }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment