1. 程式人生 > >我自定義公共類之輸入excel表格地址得到DataSet(7)

我自定義公共類之輸入excel表格地址得到DataSet(7)

 //輸入表格地址返回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;
        }