Private dataset importexcel (string strfilename)
{
If (strfilename = "")
Return NULL;
String strconn = "";
// Intercept the filename suffix and determine whether it is in 03 format or 07 format
String strfileextension = strfilename. substring (strfilename. indexof (".") + 1 );
// 07 format
If (strfileextension = "XLSX ")
{
Strconn = "provider = Microsoft. Ace. oledb.12.0;" +
"Extended properties = 'excel 12.0;" +
"HDR = Yes '; Data Source =" + strfilename;
}
// 03 format
Else if (strfileextension = "xls ")
{
Strconn = "provider = Microsoft. Jet. oledb.4.0;" +
"Data Source =" + strfilename + ";" +
"Extended properties = Excel 8.0 ";
}
// Non-Excel format
Else
{
Lblinfo. Text = @ "unsupported file types ";
Return NULL;
}
Oledbconnection conn = new oledbconnection (strconn );
Conn. open ();
Oledbdataadapter excelda = new oledbdataadapter ("select storenumber from [sheet1 $]", Conn );
Dataset excelds = new dataset ();
Try
{
Excelda. Fill (excelds );
}
Catch (exception ERR)
{
Lblinfo. Text = err. tostring ();
}
Conn. Close ();
Return excelds;
}