已经成功的将数据写入到listview,在VB中做实验数据能够从listview数据写入Excel中,但是在VB中就是不能读数据,那位大侠指点一下。
通过调试发现错误的地方在这里,ListView1.ListItems(irow)这条语句不能从ListView1中读取数据,Scrip Debugger显示 读取的数据error
脚本如下
Sub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y)
On Error Resume Next
Dim irow, icol
Dim xlApp 'As Excel.Application
Dim xlBook 'As Excel.Workbook
Dim xlSheet 'As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
Dim ListView1
Set ListView1=ScreenItems("ListView1")
With ListView1
If MsgBox("您真的要将资料导出到EXCEL中吗?", vbExclamation + vbYesNo, "警告") = vbYes Then
If .ListItems.Count > 0 Then
MsgBox "&ListView1.ListItems.Count&"
xlSheet.Cells(1, 2) = "温湿度数据Excel报表"
xlApp.Range("A1:F1").MergeCells = True
xlApp.Range("A1:F1").HorizontalAlignment = xlCenter
xlSheet.Columns(1).ColumnWidth = 18
xlSheet.Cells(2, 1) = "时间"
xlSheet.Cells(2, 2) = "标签1"
xlSheet.Cells(2, 3) = "标签2"
xlSheet.Cells(2, 4) = "标签3"
xlSheet.Cells(2, 5) = "标签4"
xlSheet.Cells(2, 6) = "标签5"
For irow = 1 To .ListItems.Count
xlSheet.Cells(irow + 2, 1).Value = .ListItems(irow)
xlSheet.Cells(irow + 2, 2).Value = .ListItems(irow).SubItems(1)
xlSheet.Cells(irow + 2, 3).Value = .ListItems(irow).SubItems(2)
xlSheet.Cells(irow + 2, 4).Value = .ListItems(irow).SubItems(3)
xlSheet.Cells(irow + 2, 5).Value = .ListItems(irow).SubItems(4)
xlSheet.Cells(irow + 2, 6).Value = .ListItems(irow).SubItems(5)
Next 'irow
xlApp.Range("A2:F2").Columns.Interior.ColorIndex = 40
xlApp.Range("A2:F2").Borders.LineStyle = xlContinuous
xlApp.Visible = True
xlApp.Range(xlSheet.Cells(2 + .ListItems.Count + 1, 1), xlSheet.Cells(2 + .ListItems.Count + 1, 4)).Columns.Interior.ColorIndex = 40
xlApp.Range(xlSheet.Cells(2 + .ListItems.Count + 1, 1), xlSheet.Cells(2 + .ListItems.Count + 1, 4)).Borders.LineStyle = xlContinuous
Else
MsgBox "*******!", vbExclamation + vbOKOnly, "警告"
End If
Else
End If
xlApp.QUIT
Set xlApp = Nothing '交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing
End With
End Sub