Add all Excel under a folder to the DataTable

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.