我自定义公共类之输入excel表格地址得到DataSet(7)

版权声明:随便看,喜欢的话加我qq,一起讨论。 https://blog.csdn.net/qq_43687284/article/details/84175323
 //输入表格地址返回dataset 查询的是excel
        public DataSet ExcelToDS(string Path)
        {
            string connStr;
            if (Path == ".xls")
                connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
            else
                connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Path + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\""; 
            OleDbConnection conn = new OleDbConnection(connStr);
            conn.Open();
            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            DataSet ds = null;
            strExcel = "select * from [sheet1$]";
            myCommand = new OleDbDataAdapter(strExcel, connStr);
            ds = new DataSet();
            myCommand.Fill(ds, "table1");
            return ds;
        }

猜你喜欢

转载自blog.csdn.net/qq_43687284/article/details/84175323
今日推荐