怎么一次性把Excel中的多个sheet都读到DataSet中

解决方案 »

  1.   


    private DataSet GetDataSetFromExcel(string fileName)
    {
    string excelStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
    DataSet ds = new DataSet();
    using (System.Data.OleDb.OleDbConnection cn = new OleDbConnection(excelStr))
    {
    using (OleDbDataAdapter dr = new OleDbDataAdapter("SELECT * FROM [sheet1$]", excelStr))
    {
    dr.Fill(ds);
                  
    }
    }
    return ds;
    }
      

  2.   

    string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + strFileName + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
                    objConn = new OleDbConnection(strConn);
                    objConn.Open();
                    System.Data.DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
                    string tableName = schemaTable.Rows[0][2].ToString().Trim();
                    OleDbDataAdapter sqlada = new OleDbDataAdapter();
                    foreach (DataRow dr in schemaTable.Rows)
                    {
                        string strSql = "Select * From [" + dr[2].ToString().Trim() + "]";
                        OleDbCommand objCmd = new OleDbCommand(strSql, objConn);
                        sqlada.SelectCommand = objCmd;
                        sqlada.Fill(data, dr[2].ToString().Trim());
                    }
                    objConn.Close();其中 HDR=YES 是指是否将第一行作为列名读取
      

  3.   

    MSB18的方法,我目前也是这样用的,这样子还是不太理想,不过也谢谢你的回复,呵呵,分就全给你了!