////////C# read Excel////////////////////////////////
string strFile = "c:\\1.xls";
string strConn = string.Empty;
if (strFile.EndsWith(".xls"))
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFile + ";"
+ "Extended Properties=Excel 8.0;";
}
else if (strFile.EndsWith(".xlsx"))
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + strFile +
";Extended Properties=\"Excel 12.0;HDR=YES;\"";
}
else
{
MessageBox.Show("文件类型错误!!!");
return;
}
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable dt2 = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string sheet = dt2.Rows[0]["TABLE_NAME"].ToString();//获取excel表名,默认只取一个表
string strExcel = "select * from [" + sheet + "]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
DataTable dt = new DataTable();
myCommand.Fill(dt);//操作dt
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余1页未读,立即下载