【问题标题】:export the listview items to excel sheet with listview header将列表视图项目导出到带有列表视图标题的 Excel 工作表
【发布时间】:2018-08-13 09:16:51
【问题描述】:

我有这段代码可以将listview中的数据导出到excel表格,但是这段代码导出的数据没有listview的标题。

如何编辑此代码以显示列表视图的标题?

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    SaveFileDialog1.Title = "Save Excel File"
    SaveFileDialog1.Filter = "Excel files (*.xls)|*.xls|Excel Files (*.xlsx)|*.xslx"
    SaveFileDialog1.ShowDialog()
    'exit if no file selected
    If SaveFileDialog1.FileName = "" Then
        Exit Sub
    End If
    'create objects to interface to Excel
    Dim xls As New Excel.Application
    Dim book As Excel.Workbook
    Dim sheet As Excel.Worksheet
    'create a workbook and get reference to first worksheet
    xls.Workbooks.Add()
    book = xls.ActiveWorkbook
    sheet = book.ActiveSheet
    'step through rows and columns and copy data to worksheet
    Dim row As Integer = 1
    Dim col As Integer = 1
    For Each item As ListViewItem In ListView1.Items
        For i As Integer = 0 To item.SubItems.Count - 1
            sheet.Cells(row, col) = item.SubItems(i).Text
            col = col + 1
        Next
        row += 1
        col = 1
    Next
    'save the workbook and clean up
    book.SaveAs(SaveFileDialog1.FileName)
    xls.Workbooks.Close()
    xls.Quit()
    releaseObject(sheet)
    releaseObject(book)
    releaseObject(xls)
End Sub

Private Sub releaseObject(ByVal obj As Object)
    'Release an automation object
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    您可以使用以下代码获取每列文本:

    Dim columns As New List(Of String)
        Dim columncount As Integer = ListView1.Columns.Count - 1
    
    
        For i As Integer = 0 To columncount
            columns.Add(ListView1.Columns(i).Text)
        Next
    
    
        For Each columnname In columns
            MessageBox.Show(columnname)
        Next
    

    【讨论】:

    • 如何在我的代码中添加您的代码??如果可以的话,请给我完整的代码@someNickName
    • 好吧,我的代码在第二个 For each,你得到了从左到右排序的每列标题的名称,你不能适应吗? :p 我从来没有导出到 Excell 或使用 vb 的 Excell,但是,如果你可以将它添加到你添加的项目上方的一行并继续向右它会适合你的问题。
    【解决方案2】:

    在进入循环导出数据之前,您需要在ListView 中迭代ColumnHeaderCollection

    For i = 0 To ListView1.Columns.Count - 1
      sheet.Cells(1, i + 1) = ListView1.Items(i).Name
    Next
    

    【讨论】:

    • 你好,当我添加你所说的代码时,我有错误....你能修改我的代码吗@jeff
    【解决方案3】:
     SaveFileDialog1.Title = "Save Excel File"
            SaveFileDialog1.Filter = "Excel Files (*.xlsx)|*.xlsx"
            SaveFileDialog1.ShowDialog()
            'exit if no file selected
            If SaveFileDialog1.FileName = "" Then
                Exit Sub
            End If
            'create objects to interface to Excel
            Dim xls As New Excel.Application
            Dim book As Excel.Workbook
            Dim sheet As Excel.Worksheet
            'create a workbook and get reference to first worksheet
            xls.Workbooks.Add()
            book = xls.ActiveWorkbook
            sheet = book.ActiveSheet
    
            'step through rows and columns and copy data to worksheet
            Dim row As Integer = 2
            Dim col As Integer = 1
    
            '////////////////////////////////////////////////////////////////////////
            Dim rowhead As Integer = 1
            Dim colhead As Integer = 1
    
            Dim columns As New List(Of String)
            Dim columncount As Integer = LvCOCONFIRMATION.Columns.Count - 1
            For i As Integer = 0 To columncount
                sheet.Cells(rowhead, colhead) = LvCOCONFIRMATION.Columns(i).Text
                colhead = colhead + 1
            Next
            '////////////////////////////////////////////////////////////////////////
    
            For Each item As ListViewItem In LvCOCONFIRMATION.Items
                For i As Integer = 0 To item.SubItems.Count - 1
                    sheet.Cells(row, col) = item.SubItems(i).Text
                    col = col + 1
                Next
                row += 1
                col = 1
            Next
    
            'save the workbook and clean up
            book.SaveAs(SaveFileDialog1.FileName)
            xls.Workbooks.Close()
            xls.Quit()
            releaseObject(sheet)
            releaseObject(book)
            releaseObject(xls)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2013-09-18
      • 1970-01-01
      • 2012-03-02
      • 2018-12-06
      • 1970-01-01
      相关资源
      最近更新 更多