我正在使用vb windows窗体。
我正在尝试使用按钮将listbox1中的所有项目导出到excel文件,但问题是它只导出第一个项目
我想导出所有listbox1项目
这是我的代码
Imports Microsoft.Office.Interop
Public Class Form1
Dim MsExcel As Excel.Application
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsExcel = CreateObject("Excel.Application")
MsExcel.Workbooks.Add()
MsExcel.Range("A1").Value = ListBox1.Items
MsExcel.Visible = True
End Sub
End Class
答案 0 :(得分:2)
您需要遍历该项目并增加您打印的行:
Imports Microsoft.Office.Interop
Public Class Form1
Dim oItem As Object
Dim OffS As Integer
Dim MsExcel As Excel.Application
Dim Wb As Excel.Workbook
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsExcel = CreateObject("Excel.Application")
Set Wb = MsExcel.Workbooks.Open("Path_of_File")
OffS = 0
For Each oItem In ListBox1.Items
Wb.Sheets(1).Range("A1").Offset(OffS, 0).Value = oItem
OffS = OffS + 1
Next oItem
Wb.SaveAs
DoEvents
Wb.Close
MsExcel.Visible = True
End Sub
End Class