string path = @"C:UsersAdministratorDesktopfolder of thefolder of theExcel";
if (Directory.Exists(path))//判断有没有这个文件夹
{
DataTable dt = new DataTable();
DirectoryInfo folder = new DirectoryInfo(path);
DataColumn vC = null;
foreach (FileInfo file in folder.GetFiles("*.xlsx"))//只要为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;//工作区开始列
int maxColumnNum = ws.Dimension.End.Column; //工作区结束列
int minRowNum = ws.Dimension.Start.Row; //工作区开始行号
int maxRowNum = ws.Dimension.End.Row; //工作区结束行号
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();
//清空datatable
}
dataGridView1.DataSource = dt;
}