- First create a folder in local project or as your requirement.
- Add a excel file in that folder that is you have to upload.
Now, create a class
public class ExcelSheet
{
public string SheetName { get; set; }
}
After create a class add below logic in our main class file.
var path = HttpContext.Current.Request.MapPath("~/ExcelFile/UserDetails.xlsx");
FileStream fileStream = File.OpenRead(path);
IExcelDataReader reader = null;
if
(fileStream.Name.EndsWith(".xls"))
{
reader = ExcelReaderFactory.CreateBinaryReader(fileStream);
}
else if
(fileStream.Name.EndsWith(".xlsx"))
{
reader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
}
DataSet excelRecords =
reader.AsDataSet();
reader.Close();
List<ExcelSheet> lstExcel = new List<ExcelSheet>();
var finalRecords = excelRecords.Tables[0];
for (int i = 0; i <
excelRecords.Tables.Count; i++)
{
ExcelSheet objExcel = new ExcelSheet();
objExcel.SheetName =
excelRecords.Tables[i].TableName;
lstExcel.Add(objExcel);
}
foreach (var item in lstExcel)
{
Console.WriteLine("Sheet
Name"
+ item.SheetName);
Console.ReadLine();
}
No comments:
Post a Comment