简体   繁体   中英

How to get the exact number of rows of an Excel file with VB.NET?

I just started working with VB.NET. In my application I will need to have the exact number of used rows in an Excel file.

I had a look at this post Reading line count using VB.NET and I tried all the answers but I've never got the exact number of rows.

Could anybody help me out?

Hello in actually I work with SQL SERVER 2008 I tried this code

Imports System.Diagnostics.Process
Imports System.IO
Imports System.Data.DataTable
Imports Microsoft.Office.Tools
Imports Excel
Imports Microsoft.Office.Interop

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles Button1.Click
        Dim selectedFile As String = String.Empty

        OpenFileDialog1.ShowDialog()
        selectedFile = OpenFileDialog1.FileName

        If (selectedFile IsNot Nothing) Then
            ListBox1.Items.Add(selectedFile)

        End If
    End Sub
    
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim fullPath As String
        Dim fileResult As String
        Dim numRow As Integer
        fullPath = Path.GetFullPath(ListBox1.SelectedItem)

        fileResult = Path.GetFileName(fullPath)
        Dim file_count As Integer = File.ReadAllLines(fullPath).Length
        MsgBox(file_count)
    

but again the count is not correct, and I really do not know why!!

I wanted the used rows for a routine importing an Excel worksheet into SQL 2008 using Microsoft.Office.Interop.Excel and VB.NET 2010 and I used just:

usedRowsCount = xlWorksheet.UsedRange.Rows.Count

Hello finally I got the solution:

Imports Microsoft.Office.Interop.Excel
Imports System.Data.DataTable
Imports Microsoft.Office.Interop

private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    Dim fullPath As String
    Dim fileResult As String
    Dim numRow As Integer
    fullPath = Path.GetFullPath(ListBox1.SelectedItem)

    fileResult = Path.GetFileName(fullPath)

    Dim obook As Excel.Workbook
    Dim oapp As Excel.Application
    oapp = New Excel.Application
    obook = oapp.Workbooks.Open(fullPath)
    numRow = 3

    While (obook.ActiveSheet.Cells(numRow, 1).Value IsNot Nothing)
        numRow = numRow + 1
    End While

    MsgBox(numRow)

End Sub

and you have to add the folowing references:

Microsoft Excel 12.0 Library
Microsoft Office 12.0 Library
Microsoft Office Tools v9.0
Microsoft visual Basic for application extensibility

Hope it helps:)

Get the data of an Excel file into datatable and count the rows:

Public Class Form1

    Private Sub getexcelfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim excelfile As New OpenFileDialog()
        excelfile.ShowDialog()
        If (excelfile.ShowDialog() = DialogResult.Cancel) Then
            Return
        Else
            Dim file As String = excelfile.FileName
            Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + file + ";Extended Properties=Excel 8.0;"
            Dim con As New OleDb.OleDbConnection(str)
            con.Open()
            Dim ds As New OleDb.OleDbDataAdapter("Select * from [Sheet1$]", con)
            Dim dt As New DataTable()
            ds.Fill(dt)
            Dim rowcount As Integer
            rowcount = dt.Rows.Count()

        End If

    End Sub
End Class
Public Class Form1

Private Sub getexcelfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim excelfile As New OpenFileDialog()
    excelfile.ShowDialog()
    If (excelfile.ShowDialog() = DialogResult.Cancel) Then
        Return
    Else
        Dim file As String = excelfile.FileName
        Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + file + ";Extended Properties=Excel 8.0;"
        Dim con As New OleDb.OleDbConnection(str)
        con.Open()
        Dim ds As New OleDb.OleDbDataAdapter("Select * from [Sheet1$]", con)
        Dim dt As New DataTable()
        ds.Fill(dt)
        Dim rowcount As Integer
        rowcount = dt.Rows.Count()

    End If

End Sub
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM