Click here to Skip to main content
15,889,096 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Export Vb.net Datagridview to Microsoft Excel 2013 Pin
Benniiit24-Aug-15 7:54
Benniiit24-Aug-15 7:54 
GeneralRe: Export Vb.net Datagridview to Microsoft Excel 2013 Pin
Eddy Vluggen24-Aug-15 8:06
professionalEddy Vluggen24-Aug-15 8:06 
AnswerRe: Export Vb.net Datagridview to Microsoft Excel 2013 Pin
ChandraRam24-Aug-15 5:27
ChandraRam24-Aug-15 5:27 
GeneralRe: Export Vb.net Datagridview to Microsoft Excel 2013 Pin
Benniiit24-Aug-15 7:55
Benniiit24-Aug-15 7:55 
GeneralRe: Export Vb.net Datagridview to Microsoft Excel 2013 Pin
ChandraRam25-Aug-15 3:44
ChandraRam25-Aug-15 3:44 
GeneralRe: Export Vb.net Datagridview to Microsoft Excel 2013 Pin
Benniiit25-Aug-15 7:14
Benniiit25-Aug-15 7:14 
GeneralRe: Export Vb.net Datagridview to Microsoft Excel 2013 Pin
ChandraRam25-Aug-15 7:55
ChandraRam25-Aug-15 7:55 
AnswerRe: Export Vb.net Datagridview to Microsoft Excel 2013 Pin
GuyThiebaut28-Aug-15 4:39
professionalGuyThiebaut28-Aug-15 4:39 
This is how I do it.
I hope you should just be able to use it as is:

Sub datagridToExcel(filename As String, source As DataGridView, header As String, footer As String, convertToXls As Boolean)


    ' use a static file path for the moment
    'Dim BackupPath As String

    Dim datatable As DataTable = CType(source.DataSource, Data.DataTable)

    Dim Headers As String = String.Empty
    Dim TempRow As String = String.Empty
    Dim SeparationMarker As String = ControlChars.Tab

    'Dim FolderBrowserDialog1 As New System.Windows.Forms.FolderBrowserDialog()


    'FolderBrowserDialog1.ShowDialog()
    'BackupPath = FolderBrowserDialog1.SelectedPath

    If filename <> String.Empty Then

        If File.Exists(filename) Then

            File.Delete(filename)

        End If

        Dim SW As New System.IO.StreamWriter(filename, False, System.Text.Encoding.Unicode)

        ' get the column header names and add to the backup file

        Dim i As Integer = 0

        If Not header = String.Empty Then

            SW.WriteLine(header)
            SW.WriteLine(String.Empty)

        End If


        ' add the headers
        For i = 0 To source.Columns.Count - 1

            Headers &= source.Columns(i).Name & SeparationMarker

        Next

        SW.WriteLine(Headers.ToString)


        ' add each row
        For Each row As DataGridViewRow In source.Rows


            For a As Integer = 0 To datatable.Columns.Count - 1


                'check if it's null

                If IsDBNull(row.Cells(a)) Then

                    TempRow &= String.Empty & SeparationMarker

                Else


                    Dim fmt As String = String.Empty

                    For gridIdx As Integer = 0 To source.ColumnCount - 1

                        If source.Columns(gridIdx).Name = datatable.Columns(a).ColumnName AndAlso source.Columns(gridIdx).DefaultCellStyle.Format() <> String.Empty Then

                            fmt = "{0:" + source.Columns(gridIdx).DefaultCellStyle.Format() + "}"
                            Exit For

                        End If


                    Next

                    If fmt <> String.Empty Then

                        Dim tmpdat As String = String.Format(fmt, row.Cells(a).Value)
                        tmpdat = tmpdat.ToString.Replace(SeparationMarker, String.Empty)

                        TempRow &= tmpdat & SeparationMarker

                    Else

                        TempRow &= row.Cells(a).Value.ToString.Replace(SeparationMarker, String.Empty) & SeparationMarker


                    End If

                End If



            Next

            TempRow = TempRow.Replace(vbCrLf, String.Empty).Replace(vbCr, String.Empty).Replace(vbLf, String.Empty)

            SW.WriteLine(TempRow.ToString)
            TempRow = String.Empty

        Next

        If Not footer = String.Empty Then

            SW.WriteLine(String.Empty)
            SW.WriteLine(footer)

        End If



        SW.Close()


        If convertToXls Then

            Try


                If File.Exists(filename.Substring(0, filename.Length - 4) + ".xlsx") Then

                    File.Delete(filename.Substring(0, filename.Length - 4) + ".xlsx")

                End If


                Dim xlsapp As New Microsoft.Office.Interop.Excel.Application
                Dim xlswb As Microsoft.Office.Interop.Excel.Workbook
                xlsapp.Visible = False

                xlswb = CType(xlsapp.Workbooks.Open(filename), Excel.Workbook)
                CType(xlswb.Worksheets(1), Excel.Worksheet).Activate()
                xlsapp.ActiveWindow.SplitColumn = 0
                xlsapp.ActiveWindow.SplitRow = 1
                xlsapp.ActiveWindow.FreezePanes = True


                CType(xlswb.Worksheets(1), Excel.Worksheet).Cells.AutoFilter(1)
                CType(xlswb.Worksheets(1), Excel.Worksheet).Columns.AutoFit()
                xlswb.SaveAs(filename.Substring(0, filename.Length - 4) + ".xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing)
                xlswb.Close()
                xlsapp.Quit()
                File.Delete(filename)

                MsgBox("data exported to:" + vbNewLine + filename.Substring(0, filename.Length - 4) + ".xlsx", vbInformation, "exported to excel")

            Catch ex As Exception

            End Try


        Else

            MsgBox("Data exported to:" + vbNewLine + filename, vbInformation, "Exported to text file")


        End If



    Else

        MsgBox("Data not exported.", vbInformation, "Exported Cancelled")

    End If


End Sub

“That which can be asserted without evidence, can be dismissed without evidence.”

― Christopher Hitchens

QuestionGood freeware barcode reader library Pin
vincentvdm22-Aug-15 22:04
vincentvdm22-Aug-15 22:04 
AnswerRe: Good freeware barcode reader library Pin
Eddy Vluggen24-Aug-15 5:27
professionalEddy Vluggen24-Aug-15 5:27 
QuestionAdd a DTPicker in a Column of grid in vb6.0 Pin
Anand_@ndy21-Aug-15 19:08
Anand_@ndy21-Aug-15 19:08 
AnswerRe: Add a DTPicker in a Column of grid in vb6.0 Pin
Eddy Vluggen24-Aug-15 5:26
professionalEddy Vluggen24-Aug-15 5:26 
QuestionHow To Set H-Scrolling And V-Scrolling With AspxpivoteGrid Devexpress Pin
Member 1174163219-Aug-15 22:30
Member 1174163219-Aug-15 22:30 
AnswerRe: How To Set H-Scrolling And V-Scrolling With AspxpivoteGrid Devexpress Pin
Eddy Vluggen24-Aug-15 5:23
professionalEddy Vluggen24-Aug-15 5:23 
QuestionVB, SQL Linq, 2 Joins and multiple Groups Pin
jkirkerx18-Aug-15 8:41
professionaljkirkerx18-Aug-15 8:41 
QuestionSql Linq, Adding a group Join or Join and performing Sums, I can't figure out the Join in VB Pin
jkirkerx17-Aug-15 13:45
professionaljkirkerx17-Aug-15 13:45 
AnswerRe: Sql Linq, Adding a group Join or Join and performing Sums, I can't figure out the Join in VB Pin
Richard Deeming18-Aug-15 1:07
mveRichard Deeming18-Aug-15 1:07 
GeneralRe: Sql Linq, Adding a group Join or Join and performing Sums, I can't figure out the Join in VB Pin
jkirkerx18-Aug-15 6:37
professionaljkirkerx18-Aug-15 6:37 
QuestionExcel VBA read application CPU Usage Pin
Member 1191534517-Aug-15 8:24
Member 1191534517-Aug-15 8:24 
AnswerRe: Excel VBA read application CPU Usage Pin
Wendelius17-Aug-15 8:33
mentorWendelius17-Aug-15 8:33 
GeneralRe: Excel VBA read application CPU Usage Pin
Member 1191534517-Aug-15 9:14
Member 1191534517-Aug-15 9:14 
GeneralRe: Excel VBA read application CPU Usage Pin
Mycroft Holmes17-Aug-15 13:03
professionalMycroft Holmes17-Aug-15 13:03 
RantRe: Excel VBA read application CPU Usage Pin
Richard Deeming18-Aug-15 0:24
mveRichard Deeming18-Aug-15 0:24 
GeneralRe: Excel VBA read application CPU Usage Pin
Wendelius18-Aug-15 9:20
mentorWendelius18-Aug-15 9:20 
QuestionMenuItem question Pin
econy17-Aug-15 4:36
econy17-Aug-15 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.