读取Excel文档

    //Get Data from Excel file
        public static DataTable ReadDataFromExcel(string queryString, string excelFile)
        {
            //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;IMEX=1';Data Source=@DataSource;";
            string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=@DataSource;Extended Properties=""Excel 12.0;HDR=YES;""";
            strConn = strConn.Replace("@DataSource", excelFile);

            System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(strConn);
            cn.Open();
            System.Data.DataSet dsexcel = new DataSet();
            System.Data.OleDb.OleDbDataAdapter sqlda = new System.Data.OleDb.OleDbDataAdapter(queryString, strConn);
            sqlda.Fill(dsexcel);
            cn.Close();

            return dsexcel.Tables[0];
        }
    }

posted @ 2012-03-22 19:53  欢喜王子  阅读(196)  评论(0编辑  收藏  举报