Visual Studio - Export DataGridView into Excel in TEXT format

Posted on 2014-08-16
Last Modified: 2014-08-21

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

Question by:Wilder1626
    LVL 32

    Accepted Solution

    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

    LVL 11

    Author Closing Comment

    Thanks a lot for your help

    It works

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now