Georgi Petev

Georgi Petev

  • NA
  • 13
  • 490

Export DataGridview from TabPages to excel

Sep 26 2022 9:12 AM

Hello i got stuck and cant figure out how to create to export the datagridviews from each tabpage to excel when click on button.

Here is my code for create the datagridviews+tabpages (that will be readed from .txt file)

[code]

Dim Lines() As String = IO.File.ReadAllLines(My.Application.Info.DirectoryPath & "\Lines.txt")
        For Each line As String In Lines
            Dim LineParts() As String = Strings.Split(line, "|", 2) 'Split the current line into two chunks at the first =
            If LineParts.Count < 2 Then
                Continue For 'No = in the line, so skip it
            Else
                Dim Key As String = LineParts(0)
                Dim Value As String = LineParts(1) 'This contains the part after the =
                'Do whatever you want with the value here. e.g.
                Dim dynamicTab As New TabPage(Key)
                Dim btn As New DataGridView()
                btn.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                btn.AlternatingRowsDefaultCellStyle.BackColor = Color.White
                btn.AlternatingRowsDefaultCellStyle.ForeColor = Color.Black
                btn.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.FromArgb(0, 177, 89)
                btn.AlternatingRowsDefaultCellStyle.SelectionForeColor = Color.White

                btn.ColumnHeadersDefaultCellStyle.BackColor = Color.White
                btn.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black
                btn.ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.FromArgb(0, 177, 89)
                btn.ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.White

                btn.DefaultCellStyle.BackColor = Color.White
                btn.DefaultCellStyle.ForeColor = Color.Black
                btn.DefaultCellStyle.SelectionBackColor = Color.FromArgb(0, 177, 89)
                btn.DefaultCellStyle.SelectionForeColor = Color.White
                btn.RowHeadersVisible = True
                btn.ColumnHeadersHeightSizeMode = AutoSize
                btn.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                btn.Anchor = AnchorStyles.Top And AnchorStyles.Left And AnchorStyles.Right
                btn.ScrollBars = ScrollBars.Both
                btn.Dock = DockStyle.Fill

                btn.Columns.Add("SAPCenter", "WorkCenter")
                btn.Columns.Add("GetLine", "Line")
                btn.Columns("GetLine").DefaultCellStyle.NullValue = Value
                btn.Columns("GetLine").ReadOnly = True
                TabControl1.TabPages.Add(dynamicTab)
                dynamicTab.Controls.Add(btn)
               
            End If
        Next

[/code]

This is on Form Load to get all information from the txt file and create the tabPages.

So now i have 1 button to export this, but i cant find a way how to get the information from the datagridviews in each TabPage.

What i want to do is:

- Each TabName to be SheetName

- Each SheetName to record the information from each TabPage datagridview using ClosedXML

Thats all, can someone help me out?


Answers (1)