string filenames = path.Substring(path.LastIndexOf("\\") + 1);
string ext = filenames.Substring(filenames.LastIndexOf(".") + 1);
if (ext == "xls" || ext == "xlsx")
{
String strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;"
+ "Data Source=" + path
+ ";Extended Properties='Excel 8.0;HDR=Yes'";
OleDbConnection connExcel = new OleDbConnection(strExcelConn);
OleDbCommand cmdExcel = new OleDbCommand();
cmdExcel.Connection = connExcel;
connExcel.Open();
System.Data.DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
System.Data.DataSet ds = new System.Data.DataSet();
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
cmdExcel.CommandText = "SELECT Top 10 * From [" + SheetName + "]";
System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter(cmdExcel);
System.Data.OleDb.OleDbDataReader dr = cmdExcel.ExecuteReader();
}
No comments:
Post a Comment