Visual Studio - Export DataGridView into Excel in TEXT format


I have this code that transfer all rows from my DataGridView into Excel, but in General format.

I would like to do the transfer in TEXT format.

How can i do that please?

Thanks for your help

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim xlApp As Microsoft.Office.Interop.Excel.Application
        Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        xlApp = New Microsoft.Office.Interop.Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("Feuil1")

        For i = 0 To DataGrid.RowCount - 1
            For j = 0 To DataGrid.ColumnCount - 1
                For k As Integer = 1 To DataGrid.Columns.Count
                    If DataGrid(j, i).Value IsNot Nothing Then
                        xlWorkSheet.Cells(1, k) = DataGrid.Columns(k - 1).HeaderText
                        xlWorkSheet.Cells(i + 2, j + 1) = DataGrid(j, i).Value.ToString()
                    End If



        Dim res As MsgBoxResult
        res = MsgBox("Process completed, Would you like to open file?", MsgBoxStyle.YesNo)
        If (res = MsgBoxResult.Yes) Then
        End If
    End Sub

Open in new window

LVL 11
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robberbaron (robr)Commented:
by recording a macro in excel , gave me
   ActiveCell.Offset(1, 1).Range("A1:A10").Select
    Selection.NumberFormat = "@"

so try

        xlWorkSheet.Cells(1, k).NumberFormat = "@"  '<<<<<                
        xlWorkSheet.Cells(1, k) = DataGrid.Columns(k - 1).HeaderText
         xlWorkSheet.Cells(i + 2, j + 1).NumberFormat = "@"  '<<<<<        
        xlWorkSheet.Cells(i + 2, j + 1) = DataGrid(j, i).Value.ToString()

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wilder1626Author Commented:
Thanks a lot for your help

It works
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.