簡體   English   中英

如何使用 VB.NET 獲取 Excel 文件的確切行數?

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

我剛開始使用 VB.NET。在我的應用程序中,我需要在 Excel 文件中使用准確的行數。

我查看了這篇文章Reading line count using VB.NET我嘗試了所有的答案,但我從來沒有得到確切的行數。

有人可以幫幫我嗎?

你好實際上我使用 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)
    

但是計數又不正確,我真的不知道為什么!

我想要使用 Microsoft.Office.Interop.Excel 和 VB.NET 2010 將 Excel 工作表導入 SQL 2008 2008 的例程中使用的行,我只使用了:

usedRowsCount = xlWorksheet.UsedRange.Rows.Count

你好,我終於得到了解決方案:

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

希望能幫助到你:)

將一個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
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM