.Net Core: Reading data from CSV & Excel files











up vote
0
down vote

favorite












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









share|improve this question




















  • 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 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 at 18:53















up vote
0
down vote

favorite












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









share|improve this question




















  • 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 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 at 18:53













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 16:38

























asked Nov 12 at 17:56









kaka1234

17813




17813








  • 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 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 at 18:53














  • 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 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 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 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 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 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 at 18:53












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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);
}
}





share|improve this answer























  • 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 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 at 19:34










  • see the updated answer
    – Kevin
    Nov 12 at 20:00










  • well did it work?
    – Kevin
    Nov 13 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 at 16:36











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',
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
});


}
});














 

draft saved


draft discarded


















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








up vote
0
down vote













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);
}
}





share|improve this answer























  • 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 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 at 19:34










  • see the updated answer
    – Kevin
    Nov 12 at 20:00










  • well did it work?
    – Kevin
    Nov 13 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 at 16:36















up vote
0
down vote













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);
}
}





share|improve this answer























  • 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 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 at 19:34










  • see the updated answer
    – Kevin
    Nov 12 at 20:00










  • well did it work?
    – Kevin
    Nov 13 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 at 16:36













up vote
0
down vote










up vote
0
down vote









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);
}
}





share|improve this answer














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);
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 20:00

























answered Nov 12 at 18:22









Kevin

1,3971711




1,3971711












  • 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 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 at 19:34










  • see the updated answer
    – Kevin
    Nov 12 at 20:00










  • well did it work?
    – Kevin
    Nov 13 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 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 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 at 19:34










  • see the updated answer
    – Kevin
    Nov 12 at 20:00










  • well did it work?
    – Kevin
    Nov 13 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 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 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 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 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 at 19:34












see the updated answer
– Kevin
Nov 12 at 20:00




see the updated answer
– Kevin
Nov 12 at 20:00












well did it work?
– Kevin
Nov 13 at 13:52




well did it work?
– Kevin
Nov 13 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 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 at 16:36


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents