网上随便一搜,代码一大把
各种方式都可以实现...
刚刚还做了用ADODB把EXCEL当数据库读的代码...

解决方案 »

  1.   

    参考: ASP.NET中Excel数据批量导入导出SQL Server        protected void btnUpload_Click(object sender, EventArgs e)
            {         
                DataSet ds = GetExcelData();
                InsertDB(ds);
            }        /// <summary>
            /// 该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径,sheetname为表示那个Excel表,此用Sheet1;
            /// </summary>
            /// <param name="ds">ds</param>
            private void InsertDB(DataSet ds)
            {
                SqlConnection _con = new SqlConnection(@"Data Source=STKWX028\SQLEXPRESS;Initial Catalog=Library;Integrated Security=True");
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = _con;
                StringBuilder sb = new StringBuilder();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        sb.Append(" INSERT INTO bookInfo(bookID,bookName,author,publisher,price,readerID,readerType,bookStatus) VALUES('");
                        sb.Append(ds.Tables[0].Rows[i].ItemArray[0].ToString() + "','");
                        sb.Append(ds.Tables[0].Rows[i].ItemArray[1].ToString() + "','");
                        sb.Append(ds.Tables[0].Rows[i].ItemArray[2].ToString() + "','");
                        sb.Append(ds.Tables[0].Rows[i].ItemArray[3].ToString() + "','");
                        sb.Append(ds.Tables[0].Rows[i].ItemArray[4].ToString() + "','");
                        sb.Append(ds.Tables[0].Rows[i].ItemArray[5].ToString() + "','");
                        sb.Append(ds.Tables[0].Rows[i].ItemArray[6].ToString() + "','");
                        sb.Append(ds.Tables[0].Rows[i].ItemArray[7].ToString() + "' ) ");
                        cmd.CommandText = sb.ToString();
                    }
                }
                _con.Open();
                int j = cmd.ExecuteNonQuery();
                _con.Close();
                if (j > 0)
                {
                    lblMessage.Text = "Insert into DB table Sucessfully!";
                }
            }        /// <summary>
            /// get data source from excel file
            /// </summary>
            /// <returns>dataset ds</returns>
            private DataSet GetExcelData()
            {
                DataSet ds = new DataSet();
                string filePath = inputFile.PostedFile.FileName;
                string connStr03 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;"; ;
                string connStr07 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=YES'";
                string queryStr = "SELECT * FROM [Sheet1$]";
                OleDbConnection conn03 = new OleDbConnection(connStr03);
                OleDbConnection conn07 = new OleDbConnection(connStr07);
                if (inputFile.HasFile)
                {
                    string fileExt = System.IO.Path.GetExtension(inputFile.FileName);
                    if (fileExt == ".xls")
                    {
                        OleDbDataAdapter myAdapter = new OleDbDataAdapter(queryStr, conn03);
                        myAdapter.Fill(ds);
                    }
                    else if (fileExt == ".xlsx")
                    {
                        OleDbDataAdapter myAdapter = new OleDbDataAdapter(queryStr, conn03);
                        myAdapter.Fill(ds);
                    }
                    else
                    {
                        lblMessage.Text = "The file is not exist!";
                    }
                }
                return ds;
            }
      

  2.   

    Refer here:
    http://www.cnblogs.com/insus/archive/2012/06/12/2545801.html
    http://www.cnblogs.com/insus/archive/2011/05/05/2037808.html
    http://www.cnblogs.com/insus/archive/2009/03/30/1425052.html
    http://www.cnblogs.com/insus/p/3814857.html
    http://www.cnblogs.com/insus/p/3779879.html
    http://www.cnblogs.com/insus/archive/2012/09/22/2698515.html
      

  3.   

    用二楼的办法在打开excel的链接时估计会遇到 
    the 'microsoft.ace.oledb.12.0' provider is not registered on the local machine可以尝试用npoi来操作excel的,后面写到excel应该很简单的