0

I am trying to read a data in an excel sheet using C#, here is my code.

    public void ReadExcel()
    {
        try
        {
            string sheetName = "Sheet1$A1:C6";
            DataTable sheetTable = loadSingleSheet(@"C:\Users\..\Desktop\Sample.xls",                  sheetName);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    private OleDbConnection returnConnection(string fileName)
    {
        return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\"");
    }
    private DataTable loadSingleSheet(string fileName, string sheetName)
    {
        DataTable sheetData = new DataTable();
        using (OleDbConnection conn = this.returnConnection(fileName))
        {
            conn.Open();
            // retrieve the data using data adapter
            OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn);
            sheetAdapter.Fill(sheetData);
        }
        return sheetData;
    }

But DataTable always get null value

enter image description here

3
  • do you want to use Microsoft.Jet or the other third party library will do? Jan 30, 2014 at 6:41
  • What is the parameter Jet OLEDB:Engine Type=5;? Try without it. Jan 30, 2014 at 6:43
  • @HamletHakobyan: same output
    – neel
    Jan 30, 2014 at 6:45

1 Answer 1

1

Here it is how to do it using ODBC and ADO.Net

    private void Form1_Load(object sender, EventArgs e)
    {   
       try
       {        
            string sheetName = "Sheet1$";// Read the whole excel sheet document      
            DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", sheetName);
            dataGridView1.DataSource = sheetTable;

            string sheetNameWithRange = "Sheet1$A1:D10"; // Read excel sheet document from A1 cell to D10 cell values.
            DataTable sheetTableWithRange = loadSingleSheet(@"C:\excelFile.xls",sheetNameWithRange);
            dataGridView2.DataSource = sheetTableWithRange;
       }
       catch (Exception Ex)
       {
            MessageBox.Show(Ex.Message, "");
       }  
    }        

    private OleDbConnection returnConnection(string fileName)
    {
        return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\"");
    }

    private DataTable loadSingleSheet(string fileName, string sheetName)
    {           
        DataTable sheetData = new DataTable();
        using (OleDbConnection conn = this.returnConnection(fileName))
        {
           conn.Open();
           // retrieve the data using data adapter
           OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn);
            sheetAdapter.Fill(sheetData);
        }                        
        return sheetData;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.