各位,麻烦帮忙看看怎么下面的
protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void BtnImport_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
        {
            Response.Write("<script>alert('请您选择Excel文件')</script> ");
            return;//当无文件时,返回
        }
        string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
        if (IsXls != ".xls")
        {
            Response.Write("<script>alert('只可以选择Excel文件')</script>");
            return;//当选择的不是Excel文件时,返回
        }
        string filename = FileUpload1.FileName;              //获取Execle文件名  DateTime日期函数
        string savePath = Server.MapPath(("upfiles\\") + filename);//Server.MapPath 获得虚拟服务器相对路径
        FileUpload1.SaveAs(savePath);                        //SaveAs 将上传的文件内容保存在服务器上
        DataSet ds = ExcelSqlConnection(savePath, filename);           //调用自定义方法
        DataRow[] dr = ds.Tables[0].Select();            //定义一个DataRow数组
        int rowsnum = ds.Tables[0].Rows.Count;
        if (rowsnum == 0)
        {
            Response.Write("<script>alert('Excel表为空表,无数据!')</script>");   //当Excel表为空时,对用户进行提示
        }
        else
        {
            for (int i = 0; i < dr.Length; i++)
            {
                //前面需要在建立一个“upfiles”的文件夹,通过下面的方式获取Excel的值,然后再将这些值用插入到数据库里面
                string id = dr[i]["编号"].ToString();
                string name = dr[i]["姓名"].ToString();
                string sex = dr[i]["性别"].ToString();
                string password = dr[i]["密码"].ToString();
                string rclass = dr[i]["班级"].ToString();
                string profession = dr[i]["专业"].ToString();
                string role = dr[i]["身份"].ToString();
                                //Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
            }
            Response.Write("<script>alert('Excle表导入成功!');</script>");
        }
    }    #region 连接Excel  读取Excel数据   并返回DataSet数据集合
    /// <summary>
    /// 连接Excel  读取Excel数据   并返回DataSet数据集合
    /// </summary>
    /// <param name="filepath">Excel服务器路径</param>
    /// <param name="tableName">Excel表名称</param>
    /// <returns></returns>
    public static System.Data.DataSet ExcelSqlConnection(string filepath, string tableName)
    {
        string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
        OleDbConnection ExcelConn = new OleDbConnection(strCon);        try
        {
            string strCom = string.Format("SELECT * FROM [Sheet1$]");
            ExcelConn.Open();
            OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, ExcelConn);
            DataSet ds = new DataSet();
            myCommand.Fill(ds, "[" + tableName + "$]");
            ExcelConn.Close();
            return ds;
        }
        catch
        {
            ExcelConn.Close();
            return null;
        }
    }
    #endregion

解决方案 »

  1.   

     for (int i = 0; i < dr.Length; i++)
                {
                    //前面需要在建立一个“upfiles”的文件夹,通过下面的方式获取Excel的值,然后再将这些值用插入到数据库里面
                    string id = dr[i]["编号"].ToString();
                    string name = dr[i]["姓名"].ToString();
                    string sex = dr[i]["性别"].ToString();
                    string password = dr[i]["密码"].ToString();
                    string rclass = dr[i]["班级"].ToString();
                    string profession = dr[i]["专业"].ToString();
                    string role = dr[i]["身份"].ToString();
                                    //Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
                }光见你取了,没见你存呀。
      

  2.   

    跟据值生成insert SQL语句执行,跟踪一下看看那里出问题了。
      

  3.   

    检查到你的代码中没有类似insert into table 这样的SQL 也没有调用存储过程 咋插入呢。
      

  4.   


                for (int i = 0; i < dr.Length; i++)
                {
                    //前面需要在建立一个“upfiles”的文件夹,通过下面的方式获取Excel的值,然后再将这些值用插入到数据库里面
                    string id = dr[i]["编号"].ToString();
                    string name = dr[i]["姓名"].ToString();
                    string sex = dr[i]["性别"].ToString();
                    string password = dr[i]["密码"].ToString();
                    string rclass = dr[i]["班级"].ToString();
                    string profession = dr[i]["专业"].ToString();
                    string role = dr[i]["身份"].ToString();
                                    //Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
                }
                Response.Write("<script>alert('Excle表导入成功!');</script>");表示只看到你取 没看到你存