Recent Posts

Baris kode dibawah ini memungkinkan anda menjalankan file .bat tanpa menampilkan Command Window.


Buka Notepad lalu pastekan kode diatas. Ganti path pada baris kedua dengan path file .bat anda. Simpan dengan ekstensi .vbs misal run.vbs.
Terlebih dahulu buat prosedur ExportToExcel()

Public Sub exportToExcel(ByVal sfd As FileDialog, ByVal dgv As DataGridView)
        Try
            sfd.Title = "Export to Excel Workbook"
            sfd.Filter = "Excel Workbook (*.xlsx)|*.xlsx| Excel 97-2003 Workbook (*.xls)|*.xls"
            sfd.ShowDialog()

            If sfd.FileName = "" Then
                Exit Sub
            End If

            Dim xls As New Excel.Application
            Dim book As Excel.Workbook
            Dim sheet As Excel.Worksheet

            xls.Workbooks.Add()
            book = xls.ActiveWorkbook
            sheet = book.ActiveSheet

            Dim j As Integer = 1
            For Each Cols As DataGridViewColumn In dgv.Columns
                If Cols.Visible Then
                    sheet.Cells(1, j).Value = Cols.HeaderText
                    j += 1
                End If
            Next

            Dim row As Integer = 2
            Dim col As Integer = 1

            For Each rows As DataGridViewRow In dgv.Rows
                If rows.Visible = True Then
                    For i As Integer = 0 To dgv.ColumnCount - 1
                        If dgv.Columns(i).Visible Then
                            sheet.Cells(row, col) = rows.Cells(i).Value
                            col = col + 1
                        End If
                    Next
                    row += 1
                    col = 1
                End If
            Next
            
            book.SaveAs(sfd.FileName)
            xls.Workbooks.Close()
            xls.Quit()
            Dim strFile As String = sfd.FileName
            Dim process As New Process()
            process.StartInfo.Verb = "runas"
            process.StartInfo.UseShellExecute = True
            If MsgBox("Export to Excel Workbook Success." & vbCr & "Do you want to open this file ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Export to Excell") = MsgBoxResult.Yes Then
                process.Start(strFile)
            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical, "btnExportToExcel_Click()")
        End Try
End Sub
selanjutnya panggil procedure tersebut :
Private Sub btnExportToExcelWorkbook_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportToExcelWorkbookToolStripMenuItem.Click
        If Me.DataGridView1.RowCount > 0 Then
            Call exportToExcel(SaveFileDialog1, DataGridView1)
        End If
End Sub