How format a string column in Datagridview to be esport to excel?

Jorge Enciso 116 Reputation points
2021-09-02T20:10:09.61+00:00

I have a data grid:
128871-image.png

this is the code:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim rowsTotal, colsTotal As Short
Dim I, j, iC As Short
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Dim xlApp As New Excel.Application

    Try  
        Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add  
        Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)  
        xlApp.Visible = True  
        rowsTotal = DataGridView1.RowCount - 1  
        colsTotal = DataGridView1.Columns.Count - 1  

        With excelWorksheet  
            .Cells.Select()  
            .Cells.Delete()  
            For iC = 0 To colsTotal  
                .Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText  
            Next  


            For I = 0 To rowsTotal - 1  
                For j = 0 To colsTotal  
                    .Cells(I + 2, j + 1).value = DataGridView1.Rows(I).Cells(j).Value  
                Next j  
            Next I  

            .Rows("1:1").Font.FontStyle = "Bold"  
            .Rows("1:1").Font.Size = 10  
            .Cells.Columns.AutoFit()  
            .Cells.Select()  
            .Cells.EntireColumn.AutoFit()  
            .Cells(1, 1).Select()  
        End With  

    Catch ex As Exception  
        MsgBox("Export to Excel Error: " & ex.Message)  
    Finally  
        'RELEASE ALLOACTED RESOURCES  
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default  
        xlApp = Nothing  

    End Try  

End Sub  

now the problem is I get the first column without the zeros at the left, I need this zeros because is part of the item number.

![128864-image.png]2

Microsoft 365 and Office | Development | Other
Developer technologies | VB
0 comments No comments
{count} votes

Answer accepted by question author
  1. Jorge Enciso 116 Reputation points
    2021-09-13T12:32:11.263+00:00

    ok, i read a couple articles and examples in the forum and come with this modification in my code:

    For I = 0 To rowsTotal - 1
    For j = 0 To colsTotal
    .Cells.Columns.AutoFit().ToString()
    .Cells.NumberFormat = "@"
    .Cells(I + 2, j + 1).value = DataGridView1.Rows(I).Cells(j).Value.ToString()
    Next j
    Next I

    it works perfectly, I'm assume if you explicitly give a format for a type of field the diferent type is take it.
    anyway, thank you so much for the answers and guidance, im prety sure i will come back with more questions :)

    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    2021-09-04T12:19:02.437+00:00

    Here is an example for getting practical columns from an Excel WorkSheet.

    Code to get DataTable

    Form code where all code for column formatting is in the project.

    Public Class LimitedForm  
        Private Sub LimitedForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown  
            DataGridView1.DataSource = ExcelOperations3.Read()  
            For Each dataGridViewColumn As DataGridViewColumn In DataGridView1.Columns  
                dataGridViewColumn.HeaderText = dataGridViewColumn.HeaderText.SplitCamelCase()  
            Next  
            DataGridView1.ExpandColumns()  
        End Sub  
      
    End Class  
    

    129275-limitedform.png

    129331-countrysheet.png

    0 comments No comments

  2. Jorge Enciso 116 Reputation points
    2021-09-07T12:57:16.597+00:00

    Karen
    I add this formatting and the excel open but no data is delivered.


  3. Jorge Enciso 116 Reputation points
    2021-09-08T11:46:49.313+00:00

    XingyuZhao-MSFT,
    The first column is a string.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.