.Net Core: Reading data from CSV & Excel files
Using .net core & c# here.
I have a UI from which user can upload the Excel or CSV files. Once they upload this goes to my web api which handles the reading of the data from these files and returns json.
My Api code as:
[HttpPost("upload")]
public async Task<IActionResult> FileUpload(IFormFile file)
JArray data = new JArray();
using (ExcelPackage package = new ExcelPackage(file.OpenReadStream()))
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
//Process, read from excel here and populate jarray
return Ok(data );
In my above code I am using EPPlus for reading the excel file. For excel file it works all fine but it cannot read csv file which is the limitation of EPPlus.
I searched and found another library CSVHelper: https://joshclose.github.io/CsvHelper/ The issue with this is it does vice versa and can read from CSV but not from Excel.
Is there any library available which supports reading from both.
Or would it be possible use EPPlus only but convert uploaded CSV to excel on the fly and then read. (please note I am not storing the excel file anywhere so cant use save as to save it as excel)
Any inputs please?
--Updated - Added code for reading data from excel---
int rowCount = worksheet.Dimension.End.Row;
int colCount = worksheet.Dimension.End.Column;
for (int row = 1; row <= rowCount; row++)
for (int col = 1; col <= colCount; col++)
var rowValue = worksheet.Cells[row, col].Value;
//With the code suggested in the answer rowcount is always 1
c# excel csv asp.net-core epplus
add a comment |
Using .net core & c# here.
I have a UI from which user can upload the Excel or CSV files. Once they upload this goes to my web api which handles the reading of the data from these files and returns json.
My Api code as:
[HttpPost("upload")]
public async Task<IActionResult> FileUpload(IFormFile file)
JArray data = new JArray();
using (ExcelPackage package = new ExcelPackage(file.OpenReadStream()))
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
//Process, read from excel here and populate jarray
return Ok(data );
In my above code I am using EPPlus for reading the excel file. For excel file it works all fine but it cannot read csv file which is the limitation of EPPlus.
I searched and found another library CSVHelper: https://joshclose.github.io/CsvHelper/ The issue with this is it does vice versa and can read from CSV but not from Excel.
Is there any library available which supports reading from both.
Or would it be possible use EPPlus only but convert uploaded CSV to excel on the fly and then read. (please note I am not storing the excel file anywhere so cant use save as to save it as excel)
Any inputs please?
--Updated - Added code for reading data from excel---
int rowCount = worksheet.Dimension.End.Row;
int colCount = worksheet.Dimension.End.Column;
for (int row = 1; row <= rowCount; row++)
for (int col = 1; col <= colCount; col++)
var rowValue = worksheet.Cells[row, col].Value;
//With the code suggested in the answer rowcount is always 1
c# excel csv asp.net-core epplus
1
If I were you, I'd use the libraries most appropriate for that format rather than trying to find a one size fits all tool. Have your code use EPPlus if it's an Excel file, CsvHelper if it's a CSV file.
– mason
Nov 12 '18 at 18:26
@mason thanks, well I just wanted to know in case there was such library available so that I dont have to repeat steps. Yes if none is available then I may use 2 libraries as you suggested.
– kaka1234
Nov 12 '18 at 18:53
add a comment |
Using .net core & c# here.
I have a UI from which user can upload the Excel or CSV files. Once they upload this goes to my web api which handles the reading of the data from these files and returns json.
My Api code as:
[HttpPost("upload")]
public async Task<IActionResult> FileUpload(IFormFile file)
JArray data = new JArray();
using (ExcelPackage package = new ExcelPackage(file.OpenReadStream()))
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
//Process, read from excel here and populate jarray
return Ok(data );
In my above code I am using EPPlus for reading the excel file. For excel file it works all fine but it cannot read csv file which is the limitation of EPPlus.
I searched and found another library CSVHelper: https://joshclose.github.io/CsvHelper/ The issue with this is it does vice versa and can read from CSV but not from Excel.
Is there any library available which supports reading from both.
Or would it be possible use EPPlus only but convert uploaded CSV to excel on the fly and then read. (please note I am not storing the excel file anywhere so cant use save as to save it as excel)
Any inputs please?
--Updated - Added code for reading data from excel---
int rowCount = worksheet.Dimension.End.Row;
int colCount = worksheet.Dimension.End.Column;
for (int row = 1; row <= rowCount; row++)
for (int col = 1; col <= colCount; col++)
var rowValue = worksheet.Cells[row, col].Value;
//With the code suggested in the answer rowcount is always 1
c# excel csv asp.net-core epplus
Using .net core & c# here.
I have a UI from which user can upload the Excel or CSV files. Once they upload this goes to my web api which handles the reading of the data from these files and returns json.
My Api code as:
[HttpPost("upload")]
public async Task<IActionResult> FileUpload(IFormFile file)
JArray data = new JArray();
using (ExcelPackage package = new ExcelPackage(file.OpenReadStream()))
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
//Process, read from excel here and populate jarray
return Ok(data );
In my above code I am using EPPlus for reading the excel file. For excel file it works all fine but it cannot read csv file which is the limitation of EPPlus.
I searched and found another library CSVHelper: https://joshclose.github.io/CsvHelper/ The issue with this is it does vice versa and can read from CSV but not from Excel.
Is there any library available which supports reading from both.
Or would it be possible use EPPlus only but convert uploaded CSV to excel on the fly and then read. (please note I am not storing the excel file anywhere so cant use save as to save it as excel)
Any inputs please?
--Updated - Added code for reading data from excel---
int rowCount = worksheet.Dimension.End.Row;
int colCount = worksheet.Dimension.End.Column;
for (int row = 1; row <= rowCount; row++)
for (int col = 1; col <= colCount; col++)
var rowValue = worksheet.Cells[row, col].Value;
//With the code suggested in the answer rowcount is always 1
c# excel csv asp.net-core epplus
c# excel csv asp.net-core epplus
edited Nov 15 '18 at 16:38
kaka1234
asked Nov 12 '18 at 17:56
kaka1234kaka1234
19813
19813
1
If I were you, I'd use the libraries most appropriate for that format rather than trying to find a one size fits all tool. Have your code use EPPlus if it's an Excel file, CsvHelper if it's a CSV file.
– mason
Nov 12 '18 at 18:26
@mason thanks, well I just wanted to know in case there was such library available so that I dont have to repeat steps. Yes if none is available then I may use 2 libraries as you suggested.
– kaka1234
Nov 12 '18 at 18:53
add a comment |
1
If I were you, I'd use the libraries most appropriate for that format rather than trying to find a one size fits all tool. Have your code use EPPlus if it's an Excel file, CsvHelper if it's a CSV file.
– mason
Nov 12 '18 at 18:26
@mason thanks, well I just wanted to know in case there was such library available so that I dont have to repeat steps. Yes if none is available then I may use 2 libraries as you suggested.
– kaka1234
Nov 12 '18 at 18:53
1
1
If I were you, I'd use the libraries most appropriate for that format rather than trying to find a one size fits all tool. Have your code use EPPlus if it's an Excel file, CsvHelper if it's a CSV file.
– mason
Nov 12 '18 at 18:26
If I were you, I'd use the libraries most appropriate for that format rather than trying to find a one size fits all tool. Have your code use EPPlus if it's an Excel file, CsvHelper if it's a CSV file.
– mason
Nov 12 '18 at 18:26
@mason thanks, well I just wanted to know in case there was such library available so that I dont have to repeat steps. Yes if none is available then I may use 2 libraries as you suggested.
– kaka1234
Nov 12 '18 at 18:53
@mason thanks, well I just wanted to know in case there was such library available so that I dont have to repeat steps. Yes if none is available then I may use 2 libraries as you suggested.
– kaka1234
Nov 12 '18 at 18:53
add a comment |
1 Answer
1
active
oldest
votes
You can use EPPLus and a MemoryStream for opening csv files into an ExcelPackage without writing to a file. Below is an example. You may have to change some of the the parameters based on your CSV file specs.
[HttpPost("upload")]
public async Task<IActionResult> FileUpload(IFormFile file)
var result = string.Empty;
string worksheetsName = "data";
bool firstRowIsHeader = false;
var format = new ExcelTextFormat();
format.Delimiter = ',';
format.TextQualifier = '"';
using (var reader = new System.IO.StreamReader(file.OpenReadStream()))
using (ExcelPackage package = new ExcelPackage())
result = reader.ReadToEnd();
ExcelWorksheet worksheet =
package.Workbook.Worksheets.Add(worksheetsName);
worksheet.Cells["A1"].LoadFromText(result, format, OfficeOpenXml.Table.TableStyles.Medium27, firstRowIsHeader);
I am uploading the file from UI what do I need to pass to new FileInfo(csvFile). Here csvFile you have is the physical location of file but I dont have that.
– kaka1234
Nov 12 '18 at 18:28
It depends what type of object you are trying to load from. You could just the contents into a string and load from that.
– Kevin
Nov 12 '18 at 19:34
see the updated answer
– Kevin
Nov 12 '18 at 20:00
well did it work?
– Kevin
Nov 13 '18 at 13:52
Sorry was travelling so could not reply. I tried your code. It works while reading an excel fine but the has issues while reading data from csv. If I have more than one row in my csv it separates each cell data by comma and doesnt distinguish by next row. See my updated post of what code I had which I used to read excel rows.
– kaka1234
Nov 15 '18 at 16:36
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267609%2fnet-core-reading-data-from-csv-excel-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use EPPLus and a MemoryStream for opening csv files into an ExcelPackage without writing to a file. Below is an example. You may have to change some of the the parameters based on your CSV file specs.
[HttpPost("upload")]
public async Task<IActionResult> FileUpload(IFormFile file)
var result = string.Empty;
string worksheetsName = "data";
bool firstRowIsHeader = false;
var format = new ExcelTextFormat();
format.Delimiter = ',';
format.TextQualifier = '"';
using (var reader = new System.IO.StreamReader(file.OpenReadStream()))
using (ExcelPackage package = new ExcelPackage())
result = reader.ReadToEnd();
ExcelWorksheet worksheet =
package.Workbook.Worksheets.Add(worksheetsName);
worksheet.Cells["A1"].LoadFromText(result, format, OfficeOpenXml.Table.TableStyles.Medium27, firstRowIsHeader);
I am uploading the file from UI what do I need to pass to new FileInfo(csvFile). Here csvFile you have is the physical location of file but I dont have that.
– kaka1234
Nov 12 '18 at 18:28
It depends what type of object you are trying to load from. You could just the contents into a string and load from that.
– Kevin
Nov 12 '18 at 19:34
see the updated answer
– Kevin
Nov 12 '18 at 20:00
well did it work?
– Kevin
Nov 13 '18 at 13:52
Sorry was travelling so could not reply. I tried your code. It works while reading an excel fine but the has issues while reading data from csv. If I have more than one row in my csv it separates each cell data by comma and doesnt distinguish by next row. See my updated post of what code I had which I used to read excel rows.
– kaka1234
Nov 15 '18 at 16:36
add a comment |
You can use EPPLus and a MemoryStream for opening csv files into an ExcelPackage without writing to a file. Below is an example. You may have to change some of the the parameters based on your CSV file specs.
[HttpPost("upload")]
public async Task<IActionResult> FileUpload(IFormFile file)
var result = string.Empty;
string worksheetsName = "data";
bool firstRowIsHeader = false;
var format = new ExcelTextFormat();
format.Delimiter = ',';
format.TextQualifier = '"';
using (var reader = new System.IO.StreamReader(file.OpenReadStream()))
using (ExcelPackage package = new ExcelPackage())
result = reader.ReadToEnd();
ExcelWorksheet worksheet =
package.Workbook.Worksheets.Add(worksheetsName);
worksheet.Cells["A1"].LoadFromText(result, format, OfficeOpenXml.Table.TableStyles.Medium27, firstRowIsHeader);
I am uploading the file from UI what do I need to pass to new FileInfo(csvFile). Here csvFile you have is the physical location of file but I dont have that.
– kaka1234
Nov 12 '18 at 18:28
It depends what type of object you are trying to load from. You could just the contents into a string and load from that.
– Kevin
Nov 12 '18 at 19:34
see the updated answer
– Kevin
Nov 12 '18 at 20:00
well did it work?
– Kevin
Nov 13 '18 at 13:52
Sorry was travelling so could not reply. I tried your code. It works while reading an excel fine but the has issues while reading data from csv. If I have more than one row in my csv it separates each cell data by comma and doesnt distinguish by next row. See my updated post of what code I had which I used to read excel rows.
– kaka1234
Nov 15 '18 at 16:36
add a comment |
You can use EPPLus and a MemoryStream for opening csv files into an ExcelPackage without writing to a file. Below is an example. You may have to change some of the the parameters based on your CSV file specs.
[HttpPost("upload")]
public async Task<IActionResult> FileUpload(IFormFile file)
var result = string.Empty;
string worksheetsName = "data";
bool firstRowIsHeader = false;
var format = new ExcelTextFormat();
format.Delimiter = ',';
format.TextQualifier = '"';
using (var reader = new System.IO.StreamReader(file.OpenReadStream()))
using (ExcelPackage package = new ExcelPackage())
result = reader.ReadToEnd();
ExcelWorksheet worksheet =
package.Workbook.Worksheets.Add(worksheetsName);
worksheet.Cells["A1"].LoadFromText(result, format, OfficeOpenXml.Table.TableStyles.Medium27, firstRowIsHeader);
You can use EPPLus and a MemoryStream for opening csv files into an ExcelPackage without writing to a file. Below is an example. You may have to change some of the the parameters based on your CSV file specs.
[HttpPost("upload")]
public async Task<IActionResult> FileUpload(IFormFile file)
var result = string.Empty;
string worksheetsName = "data";
bool firstRowIsHeader = false;
var format = new ExcelTextFormat();
format.Delimiter = ',';
format.TextQualifier = '"';
using (var reader = new System.IO.StreamReader(file.OpenReadStream()))
using (ExcelPackage package = new ExcelPackage())
result = reader.ReadToEnd();
ExcelWorksheet worksheet =
package.Workbook.Worksheets.Add(worksheetsName);
worksheet.Cells["A1"].LoadFromText(result, format, OfficeOpenXml.Table.TableStyles.Medium27, firstRowIsHeader);
edited Nov 12 '18 at 20:00
answered Nov 12 '18 at 18:22
KevinKevin
1,4931711
1,4931711
I am uploading the file from UI what do I need to pass to new FileInfo(csvFile). Here csvFile you have is the physical location of file but I dont have that.
– kaka1234
Nov 12 '18 at 18:28
It depends what type of object you are trying to load from. You could just the contents into a string and load from that.
– Kevin
Nov 12 '18 at 19:34
see the updated answer
– Kevin
Nov 12 '18 at 20:00
well did it work?
– Kevin
Nov 13 '18 at 13:52
Sorry was travelling so could not reply. I tried your code. It works while reading an excel fine but the has issues while reading data from csv. If I have more than one row in my csv it separates each cell data by comma and doesnt distinguish by next row. See my updated post of what code I had which I used to read excel rows.
– kaka1234
Nov 15 '18 at 16:36
add a comment |
I am uploading the file from UI what do I need to pass to new FileInfo(csvFile). Here csvFile you have is the physical location of file but I dont have that.
– kaka1234
Nov 12 '18 at 18:28
It depends what type of object you are trying to load from. You could just the contents into a string and load from that.
– Kevin
Nov 12 '18 at 19:34
see the updated answer
– Kevin
Nov 12 '18 at 20:00
well did it work?
– Kevin
Nov 13 '18 at 13:52
Sorry was travelling so could not reply. I tried your code. It works while reading an excel fine but the has issues while reading data from csv. If I have more than one row in my csv it separates each cell data by comma and doesnt distinguish by next row. See my updated post of what code I had which I used to read excel rows.
– kaka1234
Nov 15 '18 at 16:36
I am uploading the file from UI what do I need to pass to new FileInfo(csvFile). Here csvFile you have is the physical location of file but I dont have that.
– kaka1234
Nov 12 '18 at 18:28
I am uploading the file from UI what do I need to pass to new FileInfo(csvFile). Here csvFile you have is the physical location of file but I dont have that.
– kaka1234
Nov 12 '18 at 18:28
It depends what type of object you are trying to load from. You could just the contents into a string and load from that.
– Kevin
Nov 12 '18 at 19:34
It depends what type of object you are trying to load from. You could just the contents into a string and load from that.
– Kevin
Nov 12 '18 at 19:34
see the updated answer
– Kevin
Nov 12 '18 at 20:00
see the updated answer
– Kevin
Nov 12 '18 at 20:00
well did it work?
– Kevin
Nov 13 '18 at 13:52
well did it work?
– Kevin
Nov 13 '18 at 13:52
Sorry was travelling so could not reply. I tried your code. It works while reading an excel fine but the has issues while reading data from csv. If I have more than one row in my csv it separates each cell data by comma and doesnt distinguish by next row. See my updated post of what code I had which I used to read excel rows.
– kaka1234
Nov 15 '18 at 16:36
Sorry was travelling so could not reply. I tried your code. It works while reading an excel fine but the has issues while reading data from csv. If I have more than one row in my csv it separates each cell data by comma and doesnt distinguish by next row. See my updated post of what code I had which I used to read excel rows.
– kaka1234
Nov 15 '18 at 16:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267609%2fnet-core-reading-data-from-csv-excel-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If I were you, I'd use the libraries most appropriate for that format rather than trying to find a one size fits all tool. Have your code use EPPlus if it's an Excel file, CsvHelper if it's a CSV file.
– mason
Nov 12 '18 at 18:26
@mason thanks, well I just wanted to know in case there was such library available so that I dont have to repeat steps. Yes if none is available then I may use 2 libraries as you suggested.
– kaka1234
Nov 12 '18 at 18:53