VB导出listview到Excel

时间:2012-10-17 16:26:08

标签: vb.net excel listview extract

当我运行代码时,我一直收到'HRESULT异常:0x800A03EC'错误。 一些论坛说'1'需要改为'0',反之亦然。 在这一点上,我只是不知道什么是错的,没有什么是坚持的。我相信我已经加载了所有需要的引用,所以任何建议/帮助都会非常感激。

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim objExcel As New Excel.Application
    Dim bkWorkBook As Workbook
    Dim shWorkSheet As Worksheet
    Dim i As Integer
    Dim j As Integer

    objExcel = New Excel.Application
    bkWorkBook = objExcel.Workbooks.Add
    shWorkSheet = bkWorkBook.ActiveSheet
    For i = 0 To Me.ListView1.Columns.Count
        shWorkSheet.Cells(1, Chr(64 + i)) = Me.ListView1.Columns(i)
    Next
    For i = 1 To Me.ListView1.Items.Count
        shWorkSheet.Cells(i + 2, "A") = Me.ListView1.Items(i).Text
        For j = 2 To Me.ListView1.Items.Count
            shWorkSheet.Cells(i + 2, Chr(64 + j)) = Me.ListView1.Items(i).SubItems(j - 1)
        Next
    Next

    objExcel.Visible = True
End Sub

谢谢,

4 个答案:

答案 0 :(得分:1)

shWorkSheet.Cells(1, Chr(64 + i)) 

应该是

shWorkSheet.Cells(1, i) 

Cells需要一个或两个数字参数。

答案 1 :(得分:1)

我重新测试并发现我没有用多个列完全测试listview。试试这个:

    Try
        Dim objExcel As New Excel.Application
        Dim bkWorkBook As Workbook
        Dim shWorkSheet As Worksheet
        Dim i As Integer
        Dim j As Integer

        objExcel = New Excel.Application
        bkWorkBook = objExcel.Workbooks.Add
        shWorkSheet = CType(bkWorkBook.ActiveSheet, Worksheet)
        For i = 0 To Me.ListView1.Columns.Count - 1
            shWorkSheet.Cells(1, i + 1) = Me.ListView1.Columns(i).Text
        Next
        For i = 0 To Me.ListView1.Items.Count - 1
            For j = 0 To Me.ListView1.Items(i).SubItems.Count - 1
                shWorkSheet.Cells(i + 2, j + 1) = Me.ListView1.Items(i).SubItems(j).Text
            Next
        Next

        objExcel.Visible = True
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

答案 2 :(得分:0)

此代码仅添加:CreateObject和自动修复行和列

  Public Sub ExportLw2Excel(Lw As ListView)
    Try
        Dim objExcel As Object ' New Excel.Application
        objExcel = CreateObject("Excel.Application")
        Dim bkWorkBook As Object 'As Workbook
        Dim shWorkSheet As Object 'As Worksheet
        Dim i As Integer
        Dim j As Integer

        'objExcel = New Excel.Application
        bkWorkBook = objExcel.Workbooks.Add
        shWorkSheet = bkWorkBook.activesheet '  CType(bkWorkBook.ActiveSheet, Worksheet)
        For i = 0 To Lw.Columns.Count - 1
            shWorkSheet.Cells(1, i + 1) = Lw.Columns(i).Text
        Next
        For i = 0 To Lw.Items.Count - 1
            For j = 0 To Lw.Items(i).SubItems.Count - 1
                shWorkSheet.Cells(i + 2, j + 1) = Lw.Items(i).SubItems(j).Text
            Next
        Next

        objExcel.Cells.Select
        objExcel.Cells.EntireColumn.AutoFit
        objExcel.Cells.EntireRow.AutoFit
        objExcel.Range("A1:B1").Select
        objExcel.Selection.Font.Bold = True
        objExcel.Range("A1").Select

        objExcel.Visible = True
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

答案 3 :(得分:-1)

如果要将带有彩色线条的列表视图导出到Excel,这是一个解决方案。请根据ListViews行和列索引正确定位索引!!

Try
    Dim objExcel As New Excel.Application
    Dim bkWorkBook As Excel.Workbook
    Dim shWorkSheet As Excel.Worksheet
    Dim i As Integer
    Dim j As Integer

    objExcel = New Excel.Application
    bkWorkBook = objExcel.Workbooks.Add
    shWorkSheet = CType(bkWorkBook.ActiveSheet, Excel.Worksheet)
    For i = 0 To objLv.Columns.Count - 1
        shWorkSheet.Cells(1, i + 1) = objLv.Columns(i).Text
    Next

    For i = 0 To objLv.Items.Count - 1
        Dim clr = ColorTranslator.ToOle(objLv.Items(i - 1).BackColor)
        shWorkSheet.Rows(i).Interior.Color = clr
        For j = 2 To objLv.Columns.Count
                shWorkSheet.Cells(i + 1, j - 1).Value = objLv.Items(i - 1).SubItems(j - 1).Text
        Next
    Next

    objExcel.Visible = True
Catch ex As Exception
    MsgBox(ex.Message)
End Try