C# 读取Excel

网上一大堆拷贝粘贴的,很多过时了,自己验证了一个,记录下。


string strExcel = @"filename.xlsx";
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strExcel + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
OleDbConnection dbConn = new OleDbConnection(strConn);
try
{
     dbConn.Open();
     string strSql = "select * from [Master$]";
     OleDbCommand myCommand = new OleDbCommand(strSql, dbConn);
     OleDbDataReader reader = myCommand.ExecuteReader();
     if (reader == null)
    {
         return;
    }
    while(reader.Read())
    {
        string strValue = Convert.ToString(reader.GetValue(0));
    }
}
catch(Expection e)
{
}

你可能感兴趣的:(C# 读取Excel)