如何使用VB.net获取excel文件的确切行数?

时间:2012-09-05 11:02:31

标签: .net vb.net excel

我刚刚开始使用VB.net,在我的应用程序中,我需要在excel文件中使用确切的已用行数我看过这篇文章Reading line count using vb.net 我尝试了所有的答案,但我从来没有得到确切的行数,我也尝试过在某些网站上的其他代码,但也没有! 任何人都可以帮助我PLZ

你好我实际上在使用SQL SERVER 2008我试过这段代码

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)

但是计数再次不正确,我真的不知道为什么!!

4 个答案:

答案 0 :(得分:5)

我希望使用Microsoft.Office.Interop.Excel和vb.net 2010将常用工作表导入到sql 2008中的例程使用行,并且只使用:

usedRowsCount = xlWorksheet.UsedRange.Rows.Count

答案 1 :(得分:2)

你好最后我得到了解决方案:

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

你必须添加以下参考文献:

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

希望有所帮助:)

答案 2 :(得分:1)

将excel文件的数据存入datatable并计算行数。

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

答案 3 :(得分:-1)

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