string path = @ "C:\Users\Administrator\Desktop\folder of The\folder of The\excel";
if (directory.exists (path))//Determine if there is no this folder
{
DataTable dt = new DataTable ();
DirectoryInfo folder = new DirectoryInfo (path);
DataColumn VC = null;
foreach (FileInfo file in folder. GetFiles ("*.xlsx"))//As long as the suffix of xlsx
{
FileStream fs = new FileStream (file. FullName, FileMode.Open, FileAccess.ReadWrite, fileshare.readwrite);
using (excelpackage PCK = new Excelpackage (FS))
{
Excelworksheet ws = PCK. WORKBOOK.WORKSHEETS[1];
int mincolumnnum = ws. dimension.start.column;//Work Area Start column
int maxcolumnnum = ws. Dimension.End.Column; Work Area End Column
int minrownum = ws. Dimension.Start.Row; Work area start line number
int maxrownum = ws. Dimension.End.Row; Work area End line number
if (VC = = null)
{
for (int j = 1; J <= Maxcolumnnum; j + +)
{
VC = new DataColumn ("" + J, typeof (String));
Dt. Columns.Add (VC);
}
}
if (Maxrownum > 200)
{
Maxrownum = 200;
}
for (int n = 1; n <= maxrownum; n++)
{
DataRow vrow = dt. NewRow ();
for (int m = 1; M <= maxcolumnnum; m++)
{
VROW[M-1] = ws. Cells[n, M]. Value;
}
Dt. Rows.Add (Vrow);
}
}
Dt. Clear ();
Empty the DataTable
}
Datagridview1.datasource = DT;
}
Add all Excel under a folder to the DataTable