如何将excel的数据导入到datagridview?

解决方案 »

  1.   

    标准表格的话可以用ADO像读取数据库一样读取excel数据,再添加到datagridview。
      

  2.   


            //读Excel到DataGridView
            private void RedExcel()
            {
                string sPath = "F:\\2009-4-25.xls";
                string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";             // if you don't want to sho the header row (first row) in the grid
                // use 'HDR=NO' in the string
                string strSQL = "SELECT * FROM [excel名称$]";
                OleDbConnection excelConnection = new OleDbConnection(connectionString);
                excelConnection.Open(); // this will open excel file            OleDbCommand dbCommand = new OleDbCommand(strSQL, excelConnection);
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);            // create data table            DataTable dTable = new DataTable();            dataAdapter.Fill(dTable);            // bind the datasource            dataGridView4.DataSource = dTable;
                // assign the dataBindingSrc to the DataGridView            //dgvExcelList.DataSource = dataBingingSrc;
                // dispose used objects            dTable.Dispose();
                dataAdapter.Dispose();
                dbCommand.Dispose();
                excelConnection.Close();
                excelConnection.Dispose();
            }
    这是我写的一个测试代码,你照着做就是了