如何从Excel列填充组合框

时间:2018-07-04 10:02:45

标签: excel vb.net visual-studio-2017

经过数小时的研究,没有找到答案,我想寻求有关该问题的帮助。

我目前正在VB.NET中编写一个表单,该表单可以根据多个comboBox作为条件将粘贴行从excel文件复制到新创建的excel文件中(每个comboxBox代表Excel中的一列)。

当前程序有2种形式:

第一种形式用于浏览和打开Excel文件。

第二种形式用于复制/粘贴新的excel文件中的数据,具体取决于comboBox。

我无法找到与comboBox关联的内容,但到目前为止,这仍然是我所做的:

第一种形式

Public Class Form1
Public excel As Microsoft.Office.Interop.Excel.Application

Private Sub Browse1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Browse1.Click
    ofd.Filter = "Excel Files(.xls)|*.xls|Excel Files(.xlsx)|*.xlsx|Excel Files(*.xlsm)|*.xlsm"
    If (ofd.ShowDialog() = DialogResult.OK) Then
        TextBox1.Text = ofd.FileName
    End If
End Sub

Private Sub Comfirm1_Click(sender As Object, e As EventArgs) Handles Comfirm1.Click
    Dim excel As New Microsoft.Office.Interop.Excel.Application
    Dim wb As Microsoft.Office.Interop.Excel.Workbook
    Dim FinderForm As New Form2
    If TextBox1.Text = "" Then
        MessageBox.Show("Wait!", "Please pick your file first!", MessageBoxButtons.OKCancel)
    Else
        wb = excel.Workbooks.Open(TextBox1.Text)
        excel.Visible = False
        wb.Activate()
        FinderForm.Show()
        Me.Hide()
    End If

End Sub
End Class

第二张表格

Public Class Form2
Public excel As Microsoft.Office.Interop.Excel.Application

Private Sub BBack_Click(sender As Object, e As EventArgs) Handles BBack.Click
    Dim FinderFile As New Form1
    excel.Quit()
    FinderFile.Show()
    Me.Close()
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

End Sub
End Class

在此先感谢您的帮助!告诉我您是否需要项目文件。

修改

到目前为止,这是我的进步:

第一表格

Imports Microsoft.Office.Interop.Excel

Public Class Form1
Public Property ExcelApp As Application
Public Property ExcelWorkBook As Workbook

Private Sub Browse1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Browse1.Click
    ofd.Filter = "Excel Files(.xls)|*.xls|Excel Files(.xlsx)|*.xlsx|Excel Files(*.xlsm)|*.xlsm"
    If (ofd.ShowDialog() = DialogResult.OK) Then
        TextBox1.Text = ofd.FileName
    End If
End Sub

Private Sub Comfirm1_Click(sender As Object, e As EventArgs) Handles Comfirm1.Click
    If TextBox1.Text = "" Then
        MessageBox.Show("Wait!", "Please pick your file first!", MessageBoxButtons.OKCancel)
    Else
        ExcelApp = New Application
        ExcelWorkBook = ExcelApp.Workbooks.Open(TextBox1.Text)
        ExcelApp.Visible = False
        ExcelWorkBook.Activate()
        Dim FinderForm As New Form2(ExcelApp)
        FinderForm.Show()
        Me.Hide()
    End If
End Sub
End Class

第二张表格

Imports Microsoft.Office.Interop.Excel

Public Class Form2
Property ExcelApp As Application
Property ExcelWorkBook As Workbook

Sub New(ByRef App As Application)
    InitializeComponent()
    ExcelApp = App
End Sub

Private Sub BBack_Click(sender As Object, e As EventArgs) Handles BBack.Click
    ExcelApp.Quit()
    System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp)
    ExcelApp = Nothing
    Form1.Show()
    Me.Close()
End Sub

Private Function GetDataFromExcelByCom(ByVal Optional hasTitle As Boolean = False) As DataTable
    Dim sheets As ExcelApp.Sheets
    Dim oMissiong As Object = System.Reflection.Missing.Value
    Dim workbook As ExcelWorkbook = Nothing
    Dim dt As DataTable = New DataTable()

    Try
        If ExcelApp Is Nothing Then Return Nothing
        workbook = ExcelApp.Workbooks.Open(excelFilePath, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong)
        sheets = workbook.Worksheets
        Dim worksheet As ExcelApp.Worksheet = sheets(1)
        Dim ji As Integer = CType(12, Integer)
        If worksheet Is Nothing Then Return Nothing
        Dim iRowCount As Integer = worksheet.UsedRange.Rows.Count
        Dim iColCount As Integer = worksheet.UsedRange.Columns.Count

        For i As Integer = 0 To iColCount - 1
            Dim name = "column" & i

            If hasTitle Then
                Dim txt = (CType(worksheet.Cells(1, i + 1), ExcelApp.Range)).Text.ToString()
                If Not String.IsNullOrWhiteSpace(txt) Then name = txt
            End If

            While dt.Columns.Contains(name)
                name = name & "_1"
            End While

            dt.Columns.Add(New DataColumn(name, GetType(String)))
        Next

        Dim range As ExcelApp.Range
        Dim rowIdx As Integer = If(hasTitle, 2, 1)

        For iRow As Integer = rowIdx To iRowCount
            Dim dr As DataRow = dt.NewRow()

            For iCol As Integer = 1 To iColCount
                range = CType(worksheet.Cells(iRow, iCol), ExcelApp.Range)
                dr(iCol - 1) = If((range.Value2 Is Nothing), "", range.Text.ToString())
            Next

            dt.Rows.Add(dr)
        Next

        Return dt
    Catch
        Return Nothing
    Finally
        workbook.Close(False, oMissiong, oMissiong)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)
        workbook = Nothing
        ExcelWorkBook.Close()
        ExcelWorkBook = Nothing
    End Try
End Function

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged


End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged

End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged

End Sub
Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox4.SelectedIndexChanged

End Sub
Private Sub ComboBox5_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox5.SelectedIndexChanged

End Sub
Private Sub ComboBox6_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox6.SelectedIndexChanged

End Sub

End Class

我认为这解决了多个excel问题。

0 个答案:

没有答案