Search This Blog

August 14, 2013

Use C# to Read Cell Values from Excel 2010 via Microsoft.Office.Interop.Excel

In this Sprint, we have the new feature where users can upload the Excel file (xlsx) in our Silverlight Application (SL5) in different countries. After users click the upload button, the data in the spreadsheet will be inserted to the table in SQL Server 2008 R2 and the data will appear in the SL5.

In order to test this feature, I would like to use C# to read cell values from the spreadsheet so that I can easily compare the data between the spreadsheet and the table. It is easy for me to get the data from the spreadsheet via Microsoft.Office.Interop.Excel. How to do it?

Add Microsoft.Office.Interop.Excel and Microsoft.CSharp in the references.

Namespace: using Excel=Microsoft.Office.Interop.Excel;

Use the following code to read the cell value from the spreadsheet.

In the code above, I use dynamic type. That’s because each cell value has the different data type (int, string, double..) in the spreadsheet. The code is working perfectly for me to read the data in the specific range from the spreadsheet.

No comments: