将数据从列表视图控件导出到文本文件/excel 并在需要时将其导入回列表视图控件

问题描述

我的程序有输出数据到 Listview 控件。现在我想将数据导出到文本文件/excel,然后在需要时将其加载回列表视图控件。我知道如何使用列表框控件完成这两项任务,但列表视图对我来说是新的。我一直在研究和玩代码几个小时,但我不断收到一个错误或另一个错误。请在下面找到我的代码的最新版本以导出数据。此外,如果您能提供要导入的代码,那就太好了。

   'Export the listview to an Excel spreadsheet

     Dim SaveFileDialog1 As New SaveFileDialog
     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
    Dim placeholder As String

  '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 lstViewUsers.Items
    For i As Integer = 0 To item.SubItems.Count - 1
        placeholder = sheet.Cells(row,col) = item.SubItems(i).Text
        col = col + 1
    Next
    row += 1

Data

Error

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)