本人在做一个查询系统,读EXCEL数据至GridView,再将GridView送入oracle数据库或导出EXCEL,
急求ASP.NET操作EXCEL的资料,有的相关资料的贴出来啊,先谢谢了啊!!

解决方案 »

  1.   


      protected void binDis_Click(object sender, EventArgs e)
            {
                if (filePath.PostedFile.FileName != "")
                {
                    //取得文件路径
                    txtFilePath = filePath.PostedFile.FileName;
                    fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(".") + 1, 3);
                    if (fileExtName != "xls" && fileExtName != "XLS")
                    {
                        Response.Write(Functions.Alert("请选择Execl文件"));
                    }
                    else
                    {
                        ViewState["path"] = txtFilePath = filePath.PostedFile.FileName;
                        string connstr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + txtFilePath + ";Extended Properties=Excel 8.0;";
                        OleDbConnection conn = new OleDbConnection(connstr);
                        conn.Open();
                        OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", conn);
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        DataGrid2.DataSource = ds;
                        DataGrid2.DataBind();
                        conn.Close();
                        btnInsert.Visible = true;
                    }
                }
                else
                {
                    Response.Write(Functions.Alert("请浏览Execl文件"));
                }
            }
            protected void btnInsert_Click(object sender, EventArgs e)
            {
                //txtFilePath = filePath.PostedFile.FileName;
                txtFilePath = ViewState["path"].ToString();
            string connstr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + txtFilePath + ";Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(connstr);
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT   *   FROM   [Sheet1$]",conn);
            OleDbConnection conn1 = DB.createconnection();
            conn1.Open();
            OleDbTransaction trans = conn1.BeginTransaction();
            try
            {
                using (OleDbDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        string sql = "insert into student(S_ID,S_Name,S_Pwd,S_Gender,S_Nation,S_Birthday,S_Time,S_ClassID) values('" + dr[0].ToString() + "','" + dr[1].ToString() + "','" + dr[0].ToString()+ "','" + dr[2].ToString() + "','" + dr[3].ToString() + "','" + dr[4].ToString() + "','" + dr[5].ToString() + "','" + dr[6].ToString() + "')";
                        OleDbCommand cmd1 = new OleDbCommand(sql, conn1);
                        cmd1.Transaction = trans;
                        cmd1.ExecuteNonQuery();
                    }
                }
                trans.Commit();
                Response.Write(Functions.Alert("导入成功!!"));
                btnInsert.Enabled=false;
                BindData();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                Response.Write(Functions.Alert("导入失败...."+ex.Message));
            }
            finally
            {
               conn.Close();
               conn1.Close();
            }
        
            }
      

  2.   

    Excel是可以用OLEDB连上的
    楼上很详细了,连上后,操作就简单了