Hi luckydead,
Use ClosedXML library. For more details on ClosedXML refer below article.
Refer below sample and change as per your requirement.
Namespaces
C#
using System.Data;
using System.IO;
using ClosedXML.Excel;
VB.Net
Imports System.Data
Imports System.IO
Imports ClosedXML.Excel
Code
C#
private void btnCheckFile_Click(object sender, EventArgs e)
{
string filesPath = @"E:\Files";
string[] files = new[] { filesPath + @"\Sample.xlsx", filesPath + @"\Sample1.xlsx" };
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("FileName"), new DataColumn("Status") });
string sheetName = "Customers";
foreach (string file in files)
{
using (XLWorkbook wb = new XLWorkbook(file))
{
IXLWorksheets workSheets = wb.Worksheets;
if (workSheets.Count > 1)
{
dt.Rows.Add(Path.GetFileName(file), "Error");
}
else
{
IXLWorksheet workSheet = wb.Worksheet(1);
dt.Rows.Add(Path.GetFileName(file), workSheet.Name == sheetName ? "Ok" : "Error");
}
}
}
dataGridView1.DataSource = dt;
}
VB.Net
Private Sub btnCheckFile_Click(sender As Object, e As EventArgs) Handles btnCheckFile.Click
Dim filesPath As String = "E:\Files"
Dim files As String() = {filesPath & "\Sample.xlsx", filesPath & "\Sample1.xlsx"}
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("FileName"), New DataColumn("Status")})
Dim sheetName As String = "Customers"
For Each file As String In files
Using wb As XLWorkbook = New XLWorkbook(file)
Dim workSheets As IXLWorksheets = wb.Worksheets
If workSheets.Count > 1 Then
dt.Rows.Add(Path.GetFileName(file), "Error")
Else
Dim workSheet As IXLWorksheet = wb.Worksheet(1)
dt.Rows.Add(Path.GetFileName(file), If(workSheet.Name = sheetName, "Ok", "Error"))
End If
End Using
Next
dataGridView1.DataSource = dt
End Sub
Screenshot