Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. IO;
Using system. Data;
Using system. Data. oledb;
Namespace collector. helper
{
Public static class excelhelper
{
Public static system. Collections. Generic. List <string> gettables (string filepath)
{
List <string> r = new list <string> ();
If (file. exists (filepath ))
{
// String connstr = "provider = Microsoft. Jet." + "oledb.4.0; extended properties = \" Excel 8.0 \ "; Data Source =" + filepath;
String connstr = "provider = Microsoft. Ace. oledb.12.0; Data Source =" + filepath + "; extended properties = 'excel 12.0 XML; HDR = Yes '";
Using (oledbconnection conn = new oledbconnection (connstr ))
{
Conn. open ();
Datatable dt = conn. getoledbschematable (oledbschemaguid. Tables, null );
For (INT I = 0; I <DT. Rows. Count; I ++)
{
R. Add (Dt. Rows [I] [2]. tostring (). Trim ());
}
}
}
Return R;
}
Public static datatable gettabledata (string filepath, string tablename)
{
Datatable r = new datatable ();
If (file. exists (filepath ))
{
// String connstr = "provider = Microsoft. jet. "+" oledb.4.0; extended properties = \ "Excel 8.0; HDR = false; IMEX = 1 \"; Data Source = "+ filepath;
String connstr = "provider = Microsoft. Ace. oledb.12.0; Data Source =" + filepath + "; extended properties = 'excel 12.0 XML; HDR = Yes '";
Using (oledbconnection conn = new oledbconnection (connstr ))
{
Conn. open ();
String SQL = "select * from [" + tablename + "]";
Oledbcommand OC = new oledbcommand (SQL, Conn );
Oledbdataadapter ODA = new oledbdataadapter (OC );
ODA. Fill (R );
}
}
Return R;
}
Public static system. Collections. Generic. List <string> gettablefields (string filepath, string tablename)
{
List <string> r = new list <string> ();
If (file. exists (filepath ))
{
// String connstr = "provider = Microsoft. jet. "+" oledb.4.0; extended properties = \ "Excel 8.0; HDR = false; IMEX = 1 \"; Data Source = "+ filepath;
String connstr = "provider = Microsoft. Ace. oledb.12.0; Data Source =" + filepath + "; extended properties = 'excel 12.0 XML; HDR = Yes '";
Using (oledbconnection conn = new oledbconnection (connstr ))
{
Conn. open ();
String SQL = "select * from [" + tablename + "] where 1 = 2 ";
Oledbcommand OC = new oledbcommand (SQL, Conn );
Oledbdataadapter ODA = new oledbdataadapter (OC );
Datatable dt = new datatable ();
ODA. Fill (DT );
For (INT I = 0; I <DT. Columns. Count; I ++)
{
R. Add (Dt. Columns [I]. Caption );
}
}
}
Return R;
}
}
}