日期:2014-05-17  浏览次数:20401 次

求了一晚上,来个人解救下吧——excel导入sql数据库
先说问题,考试系统,试题批量导入数据库,现在的问题就是代码在 FileUpload1.SaveAs(Server.MapPath(FileName));这一句不执行,所以整个都不能导入
代码如下:
C# code

private DataTable xsldata()
    {

        if (FileUpload1.FileName == "")
        {
            Page.RegisterStartupScript("是","<script>alert('请选择文件');</script>");

            return null;

        }

        string fileExtenSion;

        fileExtenSion = Path.GetExtension(FileUpload1.FileName);

        if (fileExtenSion.ToLower() != ".xls" && fileExtenSion.ToLower() != ".xlsx")
        {

            Page.RegisterStartupScript("是", "<script>alert('上传文件格式不正确!');</script>");

            return null;

        }

        try
        {

            string FileName = "App_Data/" + Path.GetFileName(FileUpload1.FileName);

            if (File.Exists(Server.MapPath(FileName)))
            {

                File.Delete(Server.MapPath(FileName));

            }

            FileUpload1.SaveAs(Server.MapPath(FileName));
          
            //HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES 

            string connstr2003 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";

            string connstr2007 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties=\"Excel 12.0;HDR=YES\"";

            OleDbConnection conn;

            if (fileExtenSion.ToLower() == ".xls")
            {

                conn = new OleDbConnection(connstr2003);

            }

            else
            {

                conn = new OleDbConnection(connstr2007);

            }

            conn.Open();

            string sql = "select * from [Sheet1$]";

            OleDbCommand cmd = new OleDbCommand(sql, conn);

            DataTable dt = new DataTable();

            OleDbDataReader sdr = cmd.ExecuteReader();



            dt.Load(sdr);

            sdr.Close();

            conn.Close();

            //删除服务器里上传的文件 

            if (File.Exists(Server.MapPath(FileName)))
            {

                File.Delete(Server.MapPath(FileName));

            }

            return dt;

        }

        catch (Exception e)
        {

            return null;

        }

    } 


    protected void Button1_Click(object sender, EventArgs e)
    {
       try{ 

 

                DataTable dt = xsldata(); 

 

                //dataGridView2.DataSource = ds.Tables[0]; 

                int errorcount = 0;//记录错误信息条数 

                int insertcount = 0;//记录插入成功条数 

 

                int updatecount = 0;//记录更新信息条数 



                SqlConnection conn = new SqlConnection("server=.;uid=123456;pwd=123456;database=OnlineExam");
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                conn.Open();

 

                for(int i = 0; i < dt.Rows.Count; i++) 

                {

                    int CourseID = Convert.ToInt32(dt.Rows[i][0].ToString());//dt.Rows[i]["Name"].ToString(); "Name"即为Excel中Name列的表头 

                    string Title = dt.Rows[i][1].ToString();

                    string AnswerA =dt.Rows[i][2].ToString();
                    string AnswerB = dt.Rows[i][3].ToString();
                    string AnswerC = dt.Rows[i][4].ToString();
                    string AnswerD = dt.Rows[i][5].ToString();
                    string Answer = dt.Rows[i][6].ToString();

                    if (Title != "" && Title != "" && AnswerA != "" && AnswerB != "" && AnswerC != "" && AnswerD != &qu