Dinesh Santhalingam

Dinesh Santhalingam

  • NA
  • 737
  • 367.5k

How to read a excel file in c#?

Jan 24 2017 4:26 AM
I have a excel file.I want to read the file using c#.
Excel file contains following.
  1. Name          subject             Marks  
  2. A              maths               35
  3. B              maths               50
  4. C              maths               69
  5. ------------Empty row-----------------
  6. D              maths               36
 I am using Interop to read the Excel file .
i want my o/p like below. 
  1. Name          subject             Marks    
  2. A              maths               35  
  3. B              maths               50  
  4. C              maths               69   
  5. D              maths               36  
 What i did:
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Excel = Microsoft.Office.Interop.Excel;  
  6. namespace excel  
  7. {  
  8. class Program  
  9. {  
  10. static void Main(string[] args)  
  11. {  
  12. Excel.Application xlApp = new Excel.Application();  
  13. Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\dinesh\Sample.xlsx");  
  14. Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; // assume it is the first sheet  
  15. Excel.Range xlRange = xlWorksheet.UsedRange;  
  16.   
  17.   
  18. xlWorkbook.Worksheets.Add(xlWorksheet);  
  19.   
  20. }  
  21. }  
  22. }  
  23.    

Answers (20)