locked
reading the excel data from excel sheet using C#. RRS feed

  • Question

  • Hi there,

    I wanna read excel file data using C#, Can someone please provide me some reference.

    Thanks

    Ajay


    Thanks Ajay

    Wednesday, June 26, 2013 7:33 AM

Answers

  • hope this helps as a start..
    using System.Data; 
    using System.Data.OleDb; 
        
    ..
        
    string conn=  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & fileName & "';Extended Properties='Excel 12.0;HDR=Yes';"  
    OleDbConnection objConn = new OleDbConnection(conn); 
    objConn.Open(); 
    OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
    OleDbDataAdapter objAdapter = new OleDbDataAdapter(); 
    objAdapter.SelectCommand = objCmdSelect;   
    DataSet objDataset = new DataSet();  
    objAdapter.Fill(objDataset); 
    objConn.Close(); 
    DataTable objTable = objDataset.Tables[0];
    //objTable contains your excel data
     

    • Marked as answer by Bob Shen Monday, July 8, 2013 9:23 AM
    Wednesday, June 26, 2013 9:11 PM
  • Hi Ayay

    You can use the NPOI lib for reading and writing XLS file. Samples are included.

    Reards Rinaldo


    Full of ideas but don't know how. Coding for fun.

    • Marked as answer by Bob Shen Monday, July 8, 2013 9:23 AM
    Wednesday, June 26, 2013 8:24 AM
  • hi Ajay

    try this

    http://stackoverflow.com/questions/657131/how-to-read-data-of-an-excel-file-using-c

    may be helpful to you...


    Harshad..... Always 4 U

    • Marked as answer by Bob Shen Monday, July 8, 2013 9:23 AM
    Wednesday, June 26, 2013 11:24 AM

All replies

  • Hi Ayay

    You can use the NPOI lib for reading and writing XLS file. Samples are included.

    Reards Rinaldo


    Full of ideas but don't know how. Coding for fun.

    • Marked as answer by Bob Shen Monday, July 8, 2013 9:23 AM
    Wednesday, June 26, 2013 8:24 AM
  • hi Ajay

    try this

    http://stackoverflow.com/questions/657131/how-to-read-data-of-an-excel-file-using-c

    may be helpful to you...


    Harshad..... Always 4 U

    • Marked as answer by Bob Shen Monday, July 8, 2013 9:23 AM
    Wednesday, June 26, 2013 11:24 AM
  • hope this helps as a start..
    using System.Data; 
    using System.Data.OleDb; 
        
    ..
        
    string conn=  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & fileName & "';Extended Properties='Excel 12.0;HDR=Yes';"  
    OleDbConnection objConn = new OleDbConnection(conn); 
    objConn.Open(); 
    OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
    OleDbDataAdapter objAdapter = new OleDbDataAdapter(); 
    objAdapter.SelectCommand = objCmdSelect;   
    DataSet objDataset = new DataSet();  
    objAdapter.Fill(objDataset); 
    objConn.Close(); 
    DataTable objTable = objDataset.Tables[0];
    //objTable contains your excel data
     

    • Marked as answer by Bob Shen Monday, July 8, 2013 9:23 AM
    Wednesday, June 26, 2013 9:11 PM
  • Hi, here is also a simple alternative you can try to read excel file in C#:

    // Load Excel file.
    var workbook = ExcelFile.Load("Workbook.xlsx");
    
    // Select active worksheet.
    var worksheet = workbook.Worksheets.ActiveWorksheet;
    
    // Display the value of first cell in MessageBox.
    MessageBox.Show(worksheet.Cells["A1"].GetFormattedValue());

    I used this C# Excel component.

    Tuesday, August 27, 2013 8:30 AM