Hi luckydead,
Use the ClosedXml library.
You need to first read data from excel file. After that add TabControl on OpenFileDialog FileOk event.
Note: For this sample i have used below Articles. For more details refer below Article links.
Windows Forms (WinForms) OpenFileDialog Box Tutorial with example in C# and VB.Net
Please refer below sample.
Namespaces
C#
using ClosedXML.Excel;
using System.ComponentModel;
VB.Net
Imports ClosedXML.Excel
Imports System.ComponentModel
Code
C#
private void btnImport_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
DataSet ds = GetDataTableFromExcel();
foreach (DataTable dt in ds.Tables)
{
DataGridView dgv = new DataGridView();
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgv.DataSource = dt;
TabPage tabPage = new TabPage(dt.TableName);
tabPage.Controls.Add(dgv);
tabControl1.TabPages.Add(tabPage);
}
}
private DataSet GetDataTableFromExcel()
{
DataSet ds = new DataSet();
string filePath = openFileDialog1.FileName;
using (XLWorkbook workBook = new XLWorkbook(filePath))
{
foreach (IXLWorksheet workSheet in workBook.Worksheets)
{
bool firstRow = true;
DataTable dt = new DataTable();
IXLWorksheet worksheet = workBook.Worksheet(workSheet.Name);
dt.TableName = workSheet.Name;
foreach (IXLRow row in worksheet.Rows())
{
if (firstRow)
{
foreach (IXLCell cell in row.Cells())
{
dt.Columns.Add(cell.Value.ToString());
}
firstRow = false;
}
else
{
dt.Rows.Add();
int i = 0;
foreach (IXLCell cell in row.Cells())
{
for (int k = 0; k < dt.Columns.Count; k++)
{
if (dt.Columns[k].ColumnName.ToLower() != cell.Value.ToString().ToLower())
{
dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
}
else
{
break;
}
}
i++;
}
}
}
ds.Tables.Add(dt);
}
return ds;
}
}
VB.Net
Private Sub btnImport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnImport.Click
OpenFileDialog1.ShowDialog()
End Sub
Private Sub openFileDialog1_FileOk(ByVal sender As Object, ByVal e As CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim ds As DataSet = GetDataTableFromExcel()
For Each dt As DataTable In ds.Tables
Dim dgv As DataGridView = New DataGridView()
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
dgv.DataSource = dt
Dim tabPage As TabPage = New TabPage(dt.TableName)
tabPage.Controls.Add(dgv)
tabControl1.TabPages.Add(tabPage)
Next
End Sub
Private Function GetDataTableFromExcel() As DataSet
Dim ds As DataSet = New DataSet()
Dim filePath As String = OpenFileDialog1.FileName
Using workBook As XLWorkbook = New XLWorkbook(filePath)
For Each workSheet As IXLWorksheet In workBook.Worksheets
Dim firstRow As Boolean = True
Dim dt As DataTable = New DataTable()
Dim worksheet1 As IXLWorksheet = workBook.Worksheet(workSheet.Name)
dt.TableName = workSheet.Name
For Each row As IXLRow In worksheet.Rows()
If firstRow Then
For Each cell As IXLCell In row.Cells()
dt.Columns.Add(cell.Value.ToString())
Next
firstRow = False
Else
dt.Rows.Add()
Dim i As Integer = 0
For Each cell As IXLCell In row.Cells()
For k As Integer = 0 To dt.Columns.Count - 1
If dt.Columns(k).ColumnName.ToLower() <> cell.Value.ToString().ToLower() Then
dt.Rows(dt.Rows.Count - 1)(i) = cell.Value.ToString()
Else
Exit For
End If
Next
i += 1
Next
End If
Next
ds.Tables.Add(dt)
Next
Return ds
End Using
End Function
Screenshot