[Webinar] Learn how to a build a cloud-first strategyRegister Now


Visual Studio - Export DataGridView into Excel in TEXT format

Posted on 2014-08-16
Medium Priority
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

Robberbaron (robr) earned 2000 total points
ID: 40269667
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

ID: 40269897
Thanks a lot for your help

It works

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month20 days, 13 hours left to enroll

864 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