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)
{
if (!Directory.Exists(@"C:\TabPage"))
{
Directory.CreateDirectory(@"C:\TabPage");
}
foreach (TabPage tabPage in TabControl1.TabPages)
{
if (!Directory.Exists(Path.Combine(@"C:\TabPage", tabPage.Text)))
{
Directory.CreateDirectory(Path.Combine(@"C:\TabPage", tabPage.Text));
}
DataGridView dgv = tabPage.Controls.OfType<DataGridView>().FirstOrDefault();
foreach (DataGridViewRow row in dgv.Rows)
{
if (!Directory.Exists(Path.Combine(@"C:\TabPage", tabPage.Text, row.Cells[0].FormattedValue.ToString())))
{
Directory.CreateDirectory(Path.Combine(@"C:\TabPage", tabPage.Text, row.Cells[0].FormattedValue.ToString()));
}
dgv.AllowUserToAddRows = false;
}
dgv.AllowUserToAddRows = true;
}
}
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
If Not Directory.Exists("C:\TabPage") Then
Directory.CreateDirectory("C:\TabPage")
End If
For Each tabPage As TabPage In TabControl1.TabPages
If Not Directory.Exists(Path.Combine("C:\TabPage", tabPage.Text)) Then
Directory.CreateDirectory(Path.Combine("C:\TabPage", tabPage.Text))
End If
Dim dgv As DataGridView = tabPage.Controls.OfType(Of DataGridView)().FirstOrDefault()
For Each row As DataGridViewRow In dgv.Rows
If Not Directory.Exists(Path.Combine("C:\TabPage", tabPage.Text, row.Cells(0).FormattedValue.ToString())) Then
Directory.CreateDirectory(Path.Combine("C:\TabPage", tabPage.Text, row.Cells(0).FormattedValue.ToString()))
End If
dgv.AllowUserToAddRows = False
Next
dgv.AllowUserToAddRows = True
Next
End Sub
Screenshot