0

in my datagridview I have the first column visible = false. I need that this column not export to excel

I have the following code to export to excel:

If Sfd.ShowDialog() = DialogResult.OK Then

            Dim App As New Excel.Application
            Dim WB As Excel.Workbook
            Dim WS As New Excel.Worksheet

            WB = App.Workbooks.Add()

            WS = WB.ActiveSheet

            For i As Integer = 1 To DG.Columns.Count
                WS.Cells(1, i) = DG.Columns(i - 1).HeaderText
            Next

            For i As Integer = 0 To DG.Rows.Count - 1
                For j As Integer = 0 To DG.Columns.Count - 1
                    WS.Cells(i + 2, j + 1) = DG.Rows(i).Cells(j).Value.ToString()
                    WS.Cells(i + 2, 1).Font.Color = Color.Blue
                Next
            Next

            With WS
                With .Range(.Cells(1, 1), .Cells(1, DG.ColumnCount)).Font
                    .Color = Color.White
                    .Bold = 1
                    .Size = 12
                End With
                .Range(.Cells(1, 1), .Cells(1, DG.ColumnCount)).Interior.Color = Color.Black
                .Columns.AutoFit()
                .Columns.HorizontalAlignment = 2
            End With

            WB.SaveAs(Sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal)

            WB.Close()
            Process.Start(Sfd.FileName)
        End If

Thanks

0

4 Answers 4

1
  For i = 0 To DG.Columns.Count - 1
     WS.Cells(1, i + 1) = DG.Columns(i).HeaderText
  Next
Sign up to request clarification or add additional context in comments.

Comments

0

Hi I had the same problem try the code below it works great in my application this is the link where i copied my code Exporting displayed columns in dataGridView to Excel

' creating Excel Application

        If ((dgSMS.Columns.Count = 0) Or (dgSMS.Rows.Count = 0)) Then Exit Sub
        Dim XlApp = New Excel.Application With {.Visible = True}
        XlApp.Workbooks.Add(Excel.XlSheetType.xlWorksheet)
        Dim xlWS = XlApp.ActiveSheet
        xlWS.Name = "Exported Data"

        'Copy visible data from DGV to Excel
        Dim columnCollection As DataGridViewColumnCollection = dgSMS.Columns
        Dim currentVisibleColumn As DataGridViewColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible)
        Dim lastColumnExported As DataGridViewColumn = currentVisibleColumn
        Dim visibleColumnCount As Integer = columnCollection.GetColumnCount(DataGridViewElementStates.Visible)

        'Finally export the data
        For c = 1 To visibleColumnCount
            xlWS.Cells(1, c) = currentVisibleColumn.HeaderText
            currentVisibleColumn = columnCollection.GetNextColumn(lastColumnExported, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
            lastColumnExported = currentVisibleColumn
        Next

        'Only export visible cells
        For r = 0 To dgSMS.Rows.Count - 1
            'Reset values
            currentVisibleColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible)
            lastColumnExported = currentVisibleColumn
            For c = 1 To visibleColumnCount
                Dim value = dgSMS.Rows(r).Cells(currentVisibleColumn.Index).Value
                If value <> vbNullString Then
                    xlWS.Cells(r + 2, c) = value.ToString()
                End If
                currentVisibleColumn = columnCollection.GetNextColumn(lastColumnExported, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
                lastColumnExported = currentVisibleColumn
            Next
        Next

        'Autosize columns in excel
        Dim columns = xlWS.UsedRange.Columns
        columns.AutoFit()


        ' save the application

        'workbook.SaveAs("c:\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _
        '  Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing)



        ' Exit from the application

        ' app.Quit()

Comments

0

Why dont you first generate a datatable that you can build without adding the column which you dont want to export, then you can apply same export procedure to this datatable.

1 Comment

How change this line with a datatable? DtExport.Rows(i).Cells(j).Value.ToString()
0

Change your for loop as below:

  For i As Integer = 1 To DG.Columns.Count-1
                WS.Cells(1, i) = DG.Columns(i).HeaderText
  Next

 For i As Integer = 0 To DG.Rows.Count - 1
                For j As Integer = 1 To DG.Columns.Count - 1
                    WS.Cells(i + 2, j + 1) = DG.Rows(i).Cells(j).Value.ToString()
                    WS.Cells(i + 2, 1).Font.Color = Color.Blue
                Next
 Next

I just changed the inner for loop start from 1. that is from the second column.so it won't export first column

4 Comments

yes the rows of column(0) don´t show but the headertext of the column yes
@WFgo now I have edited my answer for header text also..check with that
@WFgo Did you check with my coding?
it not works, the first column disappear completely but also the rows of second column also dissapear

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.