简体   繁体   English

如何将列表框中的所有项目导出到VB中的excel

[英]how to export all items in listbox to excel in vb

i'm using vb windows form. 我正在使用VB Windows窗体。

I'm trying to export all items in a listbox1 to excel file using a button, but the problem that it export only the first item 我正在尝试使用按钮将listbox1中的所有项目导出到excel文件,但是它仅导出第一个项目的问题

i want to export all the listbox1 items 我想导出所有listbox1项目

here is my code 这是我的代码

    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

You'll need to loop through the item and increment the row you print in : 您需要遍历该项目并增加您在其中打印的行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM