Take the 2-minute tour ×

While Uploading an Excel File i need to check any data present in first sheet say sheet1. im reading the file using oledbreader and converting to datatable. here im checking values is there in datatable if it is empty, therfore it is assumped that excel sheet has no vlaues.

This is the code im using,

 Dim objConn As System.Data.OleDb.OleDbConnection = Nothing
        Dim dtExcel As DataTable = Nothing
        Dim excelSheetName As String = String.Empty
            objConn = New System.Data.OleDb.OleDbConnection(excelconnstring)
            dtExcel = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            Dim i As Integer = 0
            excelSheetName = dtExcel.Rows(0)("TABLE_NAME").ToString()
            Dim excelCommand As OleDbCommand
            excelCommand = New OleDbCommand("Select * from [" & excelSheetName & "]", objConn)

            Dim dt As New DataTable
            Dim oda As New OleDbDataAdapter
            oda.SelectCommand = excelCommand
            If Not dt.Rows.Count = 0 Then
                If String.IsNullOrEmpty(dt.Rows(0)(0).ToString()) Then
'error message thrown that sheet is empty
                End If
            End If

Im checking here the first row in dt is empty but in some excel sheets while binding to datatable even though in excel sheet the first cell has value, the dt has null value in first row and other values are binded properly.y this is happening and how to check the excel sheet has empty values.

share|improve this question
add comment

1 Answer

see http://www.codeproject.com/KB/office/excel_using_oledb.aspx CodeProject

share|improve this answer
thanks for that post. but still im getting empty rows for the firstcell when i export excel without header. what im doing wrong. this is my connection string for excel 97-2003 file connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=\"Excel 8.0;HDR=NO;\""; –  Sinduja Apr 28 '11 at 12:04
add comment

Your Answer


