שתף באמצעות


Import Excel to VB .NET Using Listview

Question

Wednesday, January 8, 2014 3:26 AM

Hi  Guys,

I'm looking forward that help me to import excel data into listview, i'm newbie here, so practicing different modules,

Thanks guys,

All replies (2)

Wednesday, January 8, 2014 3:58 AM ✅Answered | 2 votes

Hi,

See This code:

Imports Microsoft.Office.Interop


Private Sub getXlFile()
        Dim xlApp As Excel.Application
        Dim xlWorkbook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim xlRange As Excel.Range

        Dim xlCol As Integer
        Dim xlRow As Integer

        Dim strDestination As String
        Dim Data(0 To 100) As String


        With OpenFileDialog1
            .Filter = "Excel Office|*.xls;*.xlsx"
            .FileName = ""
            .ShowDialog()
            strDestination = .FileName

            TextBox1.Text = .FileName
        End With

        With ListView1
            .View = View.Details
            .FullRowSelect = True
            .GridLines = True
            .Columns.Clear()
            .Items.Clear()


            If strDestination <> "" And TextBox2.Text <> "" Then
                xlApp = New Excel.Application

                xlWorkbook = xlApp.Workbooks.Open(strDestination)
                xlWorkSheet = xlWorkbook.Worksheets(TextBox2.Text)
                xlRange = xlWorkSheet.UsedRange

                If xlRange.Columns.Count > 0 Then
                    If xlRange.Rows.Count > 0 Then

                        'Header
                        For xlCol = 1 To xlRange.Columns.Count
                            .Columns.Add("Column" & xlCol)
                        Next



                        'Detail
                        For xlRow = 1 To xlRange.Rows.Count
                            For xlCol = 1 To xlRange.Columns.Count
                                Data(xlCol) = xlRange.Cells(xlRow, xlCol).text

                                If xlCol = 1 Then
                                    .Items.Add(Data(xlCol).ToString)
                                Else
                                    .Items(xlRow - 1).SubItems.Add(Data(xlCol).ToString)
                                End If
                            Next
                        Next
                        xlWorkbook.Close()
                        xlApp.Quit()

               
                    End If
                End If
            Else
                MessageBox.Show("Pls. input correct attributes", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End With

    End Sub

you could see my sample; http://code.msdn.microsoft.com/Import-Excel-using-VB-NET-223cb140


Wednesday, January 8, 2014 6:34 AM

Hi Har,

Follow up question, can't kill process excel.exe?