I want to read excel file in my C#,WPF application. which is running on different machines having different versions of excels(some has excel-2003 while some machines have excel-2007 etc and some machines doesn't have any office installed at all). I want to read excel file content regardless of with/without any excel versions/packages installed on the target machines.
I am currently using Microsoft.Office.Interop.Excel to read data from excel file but on running my app on target machine which doesn't have any office installed, my application doesn't work.
How can I do it? I am doing it the following way, added namespaces
using Excel = Microsoft.Office.Interop.Excel;
Here comes the method's body:
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
xlWorkBook = xlApp.Workbooks.Open(_filePath);
List<string> Folders = new List<string>();
try
{
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
for (int cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
{
Folders.Add(((range.Cells[1, cCnt] as Excel.Range).Value).ToString());
}
}
catch (Exception ex)
{
//some error msg
}
finally
{
xlWorkBook.Close();
xlApp.Quit();
}
return Folders;
}
There are some solutions already here:
How to open an Excel file in C#?
If you are developing open source application, there are some third party libraries, so take a look at the following: