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) 

セルには、1つまたは2つの数値引数が必要です。

于 2012-10-17T16:54:58.237 に答える
1

再テストしたところ、2、3列を超えるリストビューを完全にテストしていないことがわかりました。これを試して:

    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 に答える