Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a coding to export datagridview to excel using vb.net, what i want is to create a coding that can amke border like table around the data that i exported from datagridview that can automatically increase it row if the data in datagridview increased.

this is my coding for export:

Private Sub ExportExcel()
       Dim xlApp As Microsoft.Office.Interop.Excel.Application
       Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
       Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
       Dim oValue As Object = System.Reflection.Missing.Value
       Dim sPath As String = String.Empty
       Dim dlgSave As New SaveFileDialog
       dlgSave.DefaultExt = "xls"
       dlgSave.Filter = "Microsoft Excel|*.xls"
       dlgSave.InitialDirectory = Application.StartupPath


       If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
           Try

               xlApp = New Microsoft.Office.Interop.Excel.Application


               xlBook = xlApp.Workbooks.Add(oValue)


               xlSheet = xlBook.Worksheets("sheet1")


               Dim xlRow As Long = 5
               Dim xlCol As Short = 1



               For k As Integer = 0 To DataGridView1.ColumnCount - 1



                   xlSheet.Cells(5, xlCol) = DataGridView1(k, 0).Value


                   xlCol += 1
               Next



               For i As Integer = 0 To DataGridView1.RowCount - 1

                   xlCol = 1


                   For k As Integer = 0 To DataGridView1.ColumnCount - 1
                       xlSheet.Cells(4, k + 1).Value = DataGridView1.Columns(k).HeaderText


                       xlSheet.Cells(xlRow, xlCol) = DataGridView1(k, i).Value


                       xlCol += 1
                   Next


                   xlRow += 1



               Next


               xlSheet.Columns.AutoFit()
               Dim sFileName As String = Replace(dlgSave.FileName, ".xls", "xls")


               xlSheet.SaveAs(sFileName)

               xlBook.Close()

               xlApp.Quit()


               releaseObject(xlApp)
               releaseObject(xlBook)
               releaseObject(xlSheet)



               MsgBox("Data sudah berhasil di ekspor", MsgBoxStyle.Information, "PRMS/SOB Date Tagging")
           Catch
               MsgBox(ErrorToString)
           Finally

           End Try
       End If

   End Sub
   Private Sub releaseObject(ByVal obj As Object)
       Try

           System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)


           obj = Nothing
       Catch ex As Exception
           obj = Nothing
       Finally

           GC.Collect()

       End Try
   End Sub
Posted

1 solution

Form this you can border around a1 row that wise use set border for which cells of table you required by range:

VB
xlRng = xlWorkSheet.Range("a1")
xlRng.EntireRow.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)


For more formatting operation you can follow this link:

http://vb.net-informations.com/excel-2007/vb.net_excel_page_format.htm[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900