RSS

Module Export Listview to Excel Vb.net

Export data dari listview ke format excel sangat sering dilakukan, apalagi data tersebut terkait dengan data operasional harian yang akan di modifikasi dengan aplikasi lain. Microsoft excel merupakan salah satu aplikasi yang paling sering digunakan untuk mengedit dokumen dalam bentuk data. berikut cara export data dari listview ke microsoft excel dengan VB.NET


Yang perlu disedikan adalah: 
  1. Object Listview
  2. Object Progress Bar
  3. Copy module ExportToExcel
  4. Panggil dengan "Call ExportToExcel(Listview1, ProgressBar1)
'Module ExportToEcel VB.NET

Imports Excel = Microsoft.Office.Interop.Excel

Module modExcel
    Public Sub ExportToExcel(li As ListView, pb As ProgressBar)
        Try
            Dim objExcel As New Excel.Application
            'objExcel.Visible = True
            objExcel.UserControl = True
            Dim oldCI As System.Globalization.CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture
            System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")

            Dim bkWorkBook As Excel.Workbook
            Dim shWorkSheet As Excel.Worksheet
            Dim i As Integer
            Dim j As Integer

            objExcel = New Excel.Application
            bkWorkBook = objExcel.Workbooks.Add
            shWorkSheet = objExcel.Sheets.Add
            'shWorkSheet.Name = "Sheet1"
            shWorkSheet = CType(bkWorkBook.ActiveSheet, Excel.Worksheet)

            pb.Visible = True
            pb.Maximum = li.Items.Count
            pb.Value = 0

            'Masukkan Header
            For i = 0 To li.Columns.Count - 1
                shWorkSheet.Cells(1, i + 1) = li.Columns(i).Text
            Next

            'Masukkan isi
            For i = 0 To li.Items.Count - 1
                pb.Value += 1
                For j = 0 To li.Items(i).SubItems.Count - 1
                    If sLeft(li.Items(i).SubItems(j).Text, 1) = "0" Then
                        shWorkSheet.Cells(i + 2, j + 1) = "'" & li.Items(i).SubItems(j).Text
                    Else
                        shWorkSheet.Cells(i + 2, j + 1) = li.Items(i).SubItems(j).Text
                    End If
                Next
            Next

            objExcel.Visible = True

            pb.Visible = False
            System.Threading.Thread.CurrentThread.CurrentCulture = oldCI

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

End Module


  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comments:

Posting Komentar