2012-09-05 3 views
1

방금 ​​VB.net에서 작업하기 시작했습니다. 응용 프로그램에서 Excel 파일의 사용 된 행의 정확한 번호가 필요합니다.이 게시물을 살펴 보았습니다. Reading line count using vb.net 나는 모든 해답을 시도했지만 결코 얻지 못했습니다. 행의 정확한 숫자, 나는 또한 일부 웹 사이트에서 다른 코드를 시도했지만 아무것도! 은 아무도VB.net을 사용하여 Excel 파일의 정확한 행 수를 얻는 방법은 무엇입니까?

안녕하세요 실제로 내가 SQL 서버 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) 

하지만 시도 PLZ 도와 줄 수 카운트가 정확하지 다시, 나는 정말 이유를 알고하지 않습니다!

+0

그것은 당신이 파일을 액세스하는 방법에 따라 달라집니다. Excel 파일을 어떻게 여는 중입니까? VSTO? – AlanT

+0

나는 filedialog에서 파일을 엽니 다! plz 편집 된 질문에 미리 감사드립니다! – miranda

+0

File.ReadAllLines은 TEXT 파일을 열어 모든 행을 읽을 수 있도록합니다. 스프레드 시트는 텍스트 파일이 아니므로 작동하지 않습니다. 정확히 무엇이 필요합니까? 첫 번째 워크 시트의 사용 된 행 수입니까? 모든 워크 시트에서? 사용 된 행의 수가 중요한 이유는 무엇입니까? 너는 무엇을 성취하려고 하는가? – AlanT

답변

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 

을 당신은 folowing 참조 추가해야합니다 :

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

이 도움이 :)

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 
+0

답변 해 주셔서 감사합니다.하지만 SQL Server 2008을 사용하고 있으며 프로젝트 지연으로 인해 유감스럽게 생각합니다. – miranda

+0

이 솔루션의 문제점은 2007 (.xlsx)의 파일이 아닌 2003 Excel 파일에서만 작동한다는 것입니다. 그 문자열 연결을 "Provider = Microsoft.ACE.OLEDB.12.0;"으로 변경해야합니다. + "데이터 원본 ="+ 파일 + ", 확장 속성 = Excel 12.0 Xml;" 그것은 둘 다에 대해 작동합니다 :)! – miranda

-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 
+1

다른 답변을 게시하는 대신 첫 번째 답변을 수정해야합니다. 나는 당신의 첫 번째 대답에 당신의 모범을 베꼈습니다. 이 파일을 삭제해야합니다. – sloth

5

I 희망을 일상적인 가져 오기에 사용 된 행을 원했습니다. 단지 사용 Microsoft.Office.Interop.Excel 및 vb.net 2010을 사용하여 SQL 2008에 엑셀 워크 시트 :

usedRowsCount = xlWorksheet.UsedRange.Rows.Count

관련 문제