Hi luckydead,
Please refer below sample.
Namespaces
C#
using System.IO;
using ClosedXML.Excel;
VB.Net
Imports System.IO
Imports ClosedXML.Excel
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
string[] Lines = System.IO.File.ReadAllLines(Application.StartupPath + @"\Lines.txt");
foreach (string line in Lines)
{
string[] LineParts = line.Split('|');
if (LineParts.Count() < 2)
continue;
else
{
string Key = LineParts[0];
string Value = LineParts[1];
TabPage dynamicTab = new TabPage(Key);
DataGridView btn = 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 = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
btn.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
btn.Anchor = AnchorStyles.Top & AnchorStyles.Left & 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);
}
}
}
private void Button1_Click(System.Object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
foreach (TabPage tabPage in TabControl1.TabPages)
{
DataGridView dgv = tabPage.Controls.OfType<DataGridView>().FirstOrDefault();
DataTable dt = new DataTable();
dt.TableName = dgv.Rows[0].Cells[1].FormattedValue.ToString();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("WorkCenter"), new DataColumn("Line") });
foreach (DataGridViewRow row in dgv.Rows)
{
dt.Rows.Add(row.Cells[0].FormattedValue.ToString(), row.Cells[1].FormattedValue.ToString());
dgv.AllowUserToAddRows = false;
}
ds.Tables.Add(dt);
dgv.AllowUserToAddRows = true;
}
string folderPath = @"C:\Excel\";
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
using (XLWorkbook wb = new XLWorkbook())
{
foreach (DataTable dt in ds.Tables)
wb.Worksheets.Add(dt, dt.TableName);
wb.SaveAs(folderPath + Convert.ToString("DataGridViewExport.xlsx"));
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim Lines() As String = IO.File.ReadAllLines(My.Application.Info.DirectoryPath & "\Lines.txt")
For Each line As String In Lines
Dim LineParts() As String = line.Split("|")
If LineParts.Count < 2 Then
Continue For
Else
Dim Key As String = LineParts(0)
Dim Value As String = LineParts(1)
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 = DataGridViewColumnHeadersHeightSizeMode.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
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As DataSet = New DataSet()
For Each tabPage As TabPage In TabControl1.TabPages
Dim dgv As DataGridView = tabPage.Controls.OfType(Of DataGridView)().FirstOrDefault()
Dim dt As DataTable = New DataTable()
dt.TableName = dgv.Rows(0).Cells(1).FormattedValue.ToString()
dt.Columns.AddRange(New DataColumn() {New DataColumn("WorkCenter"), New DataColumn("Line")})
For Each row As DataGridViewRow In dgv.Rows
dt.Rows.Add(row.Cells(0).FormattedValue.ToString(), row.Cells(1).FormattedValue.ToString())
dgv.AllowUserToAddRows = False
Next
ds.Tables.Add(dt)
dgv.AllowUserToAddRows = True
Next
Dim folderPath As String = "C:\Excel\"
If Not Directory.Exists(folderPath) Then Directory.CreateDirectory(folderPath)
Using wb As XLWorkbook = New XLWorkbook()
For Each dt As DataTable In ds.Tables
wb.Worksheets.Add(dt, dt.TableName)
Next
wb.SaveAs(folderPath & Convert.ToString("DataGridViewExport.xlsx"))
End Using
End Sub
Screenshot