| 作者 | 主题 |
|---|---|
|
郑州代工 游侠 经验值:344 发帖数:30 精华帖:0 |
楼主
主题:请教问题:附脚本,Wincc中SQL数据读取到listview,然后将listview数据写入Excel中
已经成功的将数据写入到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
承接自动化项目编程、设计、调试。
QQ:8304368
|
|
dcount107 侠圣 经验值:2840 发帖数:1737 精华帖:55 |
楼
主题:回复:请教问题:附脚本,Wincc中SQL数据读取到listview,然后将listview数据写入Excel中
既然不支持ListItems(iRow),只有变通一下
quote:[COLOR=BLUE] dim oItem For Each oItem In ListView1.ListItems 'HMIRuntime.Trace oItem 'HMIRuntime.Trace oItem.SubItems(1) 'HMIRuntime.Trace oItem.SubItems(2) '...... Next [/COLOR]
非淡泊无以明志,非宁静无以致远
|