Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm using visual studio 2012 and sql server 2012 to make a program

this is my coding to export datagridview to excel:
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 = 2
                Dim xlCol As Short = 1



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


                    
                    xlSheet.Cells(1, 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(1, 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


using this coding i want to insert picture and text into the header like in the crystal report into microsoft excel when i export the data into excel.

how do i make a coding to insert picture and header text into the VB coding so i can export the picture and header to Excel?
Posted

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