0

运行代码时,我不断收到“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

4 回答 4

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

应该

shWorkSheet.Cells(1, i) 

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

于 2012-10-17T16:54:58.237 回答
1

我重新测试并发现我没有完全测试超过几列的列表视图。尝试这个:

    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
于 2012-10-17T16:56:12.887 回答
1

此代码仅添加: CreateObject 和 autofit 行和列

  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
于 2019-11-11T20:11:19.800 回答
-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
于 2016-09-19T10:29:09.303 回答