文件转换table

 protected static DataTable GetDataTable(string fileName)
        {
            //execl 2003和2007以上的版本所用的引擎是不一样的
            //excel2007,兼容2003             
            //string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + " ;Extended Properties=Excel 8.0";//2003
            //string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';";//2007以上

            string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';";
            DataSet ds = new DataSet();
            using (OleDbConnection conn = new OleDbConnection(strCon))
            {
                string sql = "select * from [Sheet1$]";
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                da.Fill(ds);
            }
            return ds.Tables[0];
        }

 

posted on 2014-08-20 10:48  小景  阅读(156)  评论(0编辑  收藏  举报