如何在VB6中抓取又名显示Excel文档的所有内容

发布于 2025-01-03 03:30:06 字数 596 浏览 0 评论 0原文

我编写了一个程序来读取 Excel 文档并将其显示在消息框中。但是,问题是,我想将其全部废弃(即显示到 messageBox),而不知道要选择哪一行或哪列。我写了这段代码:

  Private Sub Command1_Click()
  On Error GoTo Err

  StartExcel

  Set ExcelWBk = Excel.Workbooks.Open(App.Path & "\Dataku.xls")
  Set ExcelWS = ExcelWBk.Worksheets(1)
  With ExcelWS

    Dim i As Integer
    Dim strData As String

    For i = 1 To 5

      strData = strData & .Cells(i, 1) & vbCrLf
    Next i
  End With

  MsgBox strData
  CloseWorkSheet
  ClearExcelMemory
  Exit Sub
Err:
  ClearExcelMemory 
End Sub

但它仅返回该列(第 1 列)的数据。我需要读取整个 Excel 文件。

i made a program to read an excel document and display it on a messageBox. but, the thing is, i want to scrap aka display all of it to messageBox without knowing which row or column to choose. i wrote this code:

  Private Sub Command1_Click()
  On Error GoTo Err

  StartExcel

  Set ExcelWBk = Excel.Workbooks.Open(App.Path & "\Dataku.xls")
  Set ExcelWS = ExcelWBk.Worksheets(1)
  With ExcelWS

    Dim i As Integer
    Dim strData As String

    For i = 1 To 5

      strData = strData & .Cells(i, 1) & vbCrLf
    Next i
  End With

  MsgBox strData
  CloseWorkSheet
  ClearExcelMemory
  Exit Sub
Err:
  ClearExcelMemory 
End Sub

but it returned into datas of that column (column 1) only. i need to read whole excel file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽梦紫曦~ 2025-01-10 03:30:06

像这样的东西(在PowerPoint中测试,因为我没有VB6)将逐个单元格地获取第一个工作表的UsedRange(使用数组以提高效率)

请更改您的文件路径以适应。

第一个版本 - 在 PowerPoint 中测试

Sub GetData()
Dim objExcel As Object
Dim objWB As Object
Dim objws As Object
Dim X As Variant
Dim lngCol As Long
Dim lngRow As Long
Dim strOut As String


Set objExcel = CreateObject("Excel.Application")
On Error Resume Next
Set objWB = objExcel.Workbooks.Open("c:\temp\test.xlsx")
Set objws = objWB.Sheets(1)
On Error GoTo 0
If objws Is Nothing Then Exit Sub

'recalc usedrange
objws.usedrange
X = objws.usedrange
For lngRow = 1 To UBound(X, 1)
    For lngCol = 1 To UBound(X, 2)
        strOut = strOut & (X(lngRow, lngCol) & vbNewLine)
    Next
Next

objWB.Close False
objExcel.Quit
Set objExcel = Nothing
If Len(strOut) > 0 Then MsgBox strOut
End Sub

VBS 版本

Dim objExcel
Dim objWB
Dim objws 
Dim X
Dim lngCol
Dim lngRow
Dim strOut  

Set objExcel = CreateObject("Excel.Application") 
On Error Resume Next 
Set objWB = objExcel.Workbooks.Open("c:\temp\test.xlsx") 
Set objws = objWB.Sheets(1) 
On Error GoTo 0 
If IsEmpty(objws) Then Stop

'recalc usedrange 
objws.usedrange 
X = objws.usedrange 
For lngRow = 1 To UBound(X, 1) 
    For lngCol = 1 To UBound(X, 2) 
        strOut = strOut & (X(lngRow, lngCol) & vbNewLine) 
    Next 
Next 

objWB.Close False 
objExcel.Quit 
Set objExcel = Nothing 
If Len(strOut) > 0 Then MsgBox strOut 

Something like this (tested in PowerPoint as I dont have VB6) will get the UsedRange of the first sheet cell by cell (using an array for efficiency)

Pls change your file path to suit.

First version - tested in PowerPoint

Sub GetData()
Dim objExcel As Object
Dim objWB As Object
Dim objws As Object
Dim X As Variant
Dim lngCol As Long
Dim lngRow As Long
Dim strOut As String


Set objExcel = CreateObject("Excel.Application")
On Error Resume Next
Set objWB = objExcel.Workbooks.Open("c:\temp\test.xlsx")
Set objws = objWB.Sheets(1)
On Error GoTo 0
If objws Is Nothing Then Exit Sub

'recalc usedrange
objws.usedrange
X = objws.usedrange
For lngRow = 1 To UBound(X, 1)
    For lngCol = 1 To UBound(X, 2)
        strOut = strOut & (X(lngRow, lngCol) & vbNewLine)
    Next
Next

objWB.Close False
objExcel.Quit
Set objExcel = Nothing
If Len(strOut) > 0 Then MsgBox strOut
End Sub

VBS version

Dim objExcel
Dim objWB
Dim objws 
Dim X
Dim lngCol
Dim lngRow
Dim strOut  

Set objExcel = CreateObject("Excel.Application") 
On Error Resume Next 
Set objWB = objExcel.Workbooks.Open("c:\temp\test.xlsx") 
Set objws = objWB.Sheets(1) 
On Error GoTo 0 
If IsEmpty(objws) Then Stop

'recalc usedrange 
objws.usedrange 
X = objws.usedrange 
For lngRow = 1 To UBound(X, 1) 
    For lngCol = 1 To UBound(X, 2) 
        strOut = strOut & (X(lngRow, lngCol) & vbNewLine) 
    Next 
Next 

objWB.Close False 
objExcel.Quit 
Set objExcel = Nothing 
If Len(strOut) > 0 Then MsgBox strOut 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文