This is a cool nuget package that you can use to read excel files into dataset just like how you query the database. Very simple and no dependancy. What you need is to install the add the following 2 nuget packages into your solution.
These are the URLs,
https://www.nuget.org/packages/ExcelDataReader/
https://www.nuget.org/packages/ExcelDataReader.DataSet/
Then you can read the file as below. Note that this code will read your file horizontally. Meaning column by column.
Copy and paste this code and try.
Happy coding...
These are the URLs,
https://www.nuget.org/packages/ExcelDataReader/
https://www.nuget.org/packages/ExcelDataReader.DataSet/
Then you can read the file as below. Note that this code will read your file horizontally. Meaning column by column.
try {
OpenFileDialog of = new OpenFileDialog(); of .Filter = "Excel Files|*.xls;*.xlsx;*.xlsm"; of .ShowDialog();
var FilePath = of .FileName;
if (File.Exists(FilePath)) {
try {
using(var stream = File.Open(FilePath, FileMode.Open, FileAccess.Read)) {
using(var reader = ExcelReaderFactory.CreateReader(stream)) {
var result = reader.AsDataSet();
if (result != null && result.Tables != null && result.Tables.Count > 0) {
rowCountMultiple = result.Tables[0].Rows.Count;
colCountMultiple = result.Tables[0].Columns.Count;
List < string > valueList;
string token, value;
listTokensForPageMultiple = new Dictionary < int, List < string >> ();
for (int c = 0; c < colCountMultiple; c++) {
valueList = new List < string > ();
token = result.Tables[0].Rows[0][c].ToString();
for (int r = 0; r < rowCountMultiple; r++) {
value = result.Tables[0].Rows[r][c].ToString();
valueList.Add(value);
}
listTokensForPageMultiple.Add(c, valueList);
}
}
}
}
} catch (Exception ex) {
Common.Error(ex.Message, ex);
} finally {
GC.Collect();
}
Copy and paste this code and try.
Happy coding...
No comments:
Post a Comment