如何将ListView控件中的数据导出到Excel中?

ListView控件可以显示多列数据,与excel工作表单元格具有相似性。

如下图所示为ListView控件中的数据,

现在希望导出生成如下图所示的Excel数据

可以使用如下的代码:

Sub QQ1722187970()
   Dim oExcel
    Dim oWB
    Dim oWK
    Dim oCH As ColumnHeader
    Dim oLI As ListItem
    Dim oLSI As ListSubItem
    Set oExcel = CreateObject("Excel.Application")
    With oExcel
        oExcel.Visible = True
        Set oWB = .workbooks.Add
        Set oWK = oWB.worksheets(1)
        With oWK
            With ListView1
                .View = lvwReport
                '读取总列数
                iCol = .ColumnHeaders.Count
                '先导出标题
                For j = 1 To iCol
                    oWK.cells(1, j) = .ColumnHeaders(j).Text
                Next j
                '再逐行导出项目
                For i = 1 To .ListItems.Count
                    oWK.cells(i + 1, 1) = .ListItems(i).Text
                    For j = 2 To iCol
                        oWK.cells(i + 1, j) = .ListItems(i).ListSubItems(j - 1).Text
                    Next j
                Next i
            End With
        End With
    End With
End Sub

 

       

发表评论