在VB.net中将列表框中选定的项目粘贴到Excel工作表中,可以通过以下步骤实现:
Dim excelApp As New Excel.Application
Dim workbook As Excel.Workbook = excelApp.Workbooks.Open("C:\path\to\your\workbook.xlsx")
Dim worksheet As Excel.Worksheet = workbook.Sheets("Sheet1")
Dim selectedItems As New List(Of String)
For Each item As Object In ListBox1.SelectedItems
selectedItems.Add(item.ToString())
Next
For i As Integer = 0 To selectedItems.Count - 1
worksheet.Cells(i + 1, 1).Value = selectedItems(i)
Next
workbook.Save()
workbook.Close()
excelApp.Quit()
完整的代码示例:
Imports Excel = Microsoft.Office.Interop.Excel
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim excelApp As New Excel.Application
Dim workbook As Excel.Workbook = excelApp.Workbooks.Open("C:\path\to\your\workbook.xlsx")
Dim worksheet As Excel.Worksheet = workbook.Sheets("Sheet1")
Dim selectedItems As New List(Of String)
For Each item As Object In ListBox1.SelectedItems
selectedItems.Add(item.ToString())
Next
For i As Integer = 0 To selectedItems.Count - 1
worksheet.Cells(i + 1, 1).Value = selectedItems(i)
Next
workbook.Save()
workbook.Close()
excelApp.Quit()
End Sub
这样,选定的项目就会被粘贴到Excel工作表的第一列中。请注意,上述代码仅适用于使用Microsoft Excel的情况,如果使用其他Excel软件,可能需要相应的修改。
领取专属 10元无门槛券
手把手带您无忧上云