1

私はVB.netを使い始めたばかりです。私のアプリケーションでは、Excel ファイルに使用されている行の正確な数が必要です 。正確な行数を取得したことはありません。いくつかのWebサイトで他のコードも試しましたが、何もしませんでした!! 誰か助けてくれませんか

こんにちは、実際に私は 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

4 に答える 4

5

Microsoft.Office.Interop.Excel と vb.net 2010 を使用して Excel ワークシートを sql 2008 にインポートするルーチンに使用される行が必要で、次のものを使用しました。

usedRowsCount = xlWorksheet.UsedRange.Rows.Count

于 2014-02-27T18:13:19.070 に答える
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

それが役に立てば幸い :)

于 2012-09-14T14:57:45.323 に答える
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
于 2012-09-05T11:59:54.470 に答える