【问题标题】:how to get the exact number of rows of an excel file with VB.net?如何使用 VB.net 获取 excel 文件的确切行数?
【发布时间】:2012-09-05 11:02:31
【问题描述】:

我刚开始使用 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)

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

【问题讨论】:

  • 这取决于您访问文件的方式。你是如何打开 Excel 文件的? VSTO?
  • 我从文件对话框打开文件!请看一下编辑过的问题提前谢谢!
  • File.ReadAllLines 将打开一个 TEXT 文件并允许您从中读取所有行。电子表格不是文本文件,因此这不起作用。你到底需要什么?是第一个工作表中使用的行数吗?在所有工作表中?为什么使用的行数很重要?你想达到什么目标?
  • 实际上我必须为行数设置条件,例如,如果它大于数字,则此文件将无效!

标签: .net vb.net excel


【解决方案1】:

我想要使用 Microsoft.Office.Interop.Excel 和 vb.net 2010 将 Excel 工作表导入 sql 2008 的例程使用的行,并且只使用了:

usedRowsCount = xlWorksheet.UsedRange.Rows.Count

【讨论】:

    【解决方案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
    

    希望对你有帮助:)

    【讨论】:

      【解决方案3】:

      将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
      

      【讨论】:

      • 感谢您的回答,但我使用的是 SQL Server 2008,对于我在项目的另一部分工作的延误感到抱歉!
      • 这个解决方案的问题,它只适用于 2003 的 excel 文件而不是 2007 的那些(.xlsx)!为此,您必须将字符串连接更改为此“Provider=Microsoft.ACE.OLEDB.12.0;” +“数据源=”+文件+“;扩展属性=Excel 12.0 Xml;”它适用于两者:)!
      【解决方案4】:
      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
      

      【讨论】:

      • 您应该编辑您的第一个答案,而不是发布另一个答案。我将您的示例复制到您的第一个答案中;你应该删除这个。
      猜你喜欢
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多