zoukankan      html  css  js  c++  java
  • C#如何将excel的数据导入到SQL2005数据库中

    public DataSet ExecleDs(string filenameurl, string table)
        {
           
            string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            DataSet ds = new DataSet();
            OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
            odda.Fill(ds, table);
            return ds;
        }
        protected void Button8_Click(object sender, EventArgs e)//确定添加键
        {
            string strConn = "Data Source=.;DataBase=gongnuan;Uid=sa;Pwd=sa";
            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文件时,返回
            }
            SqlConnection cn = new SqlConnection(strConn);
            cn.Open();
            string filename = FileUpload1.FileName;              //获取Execle文件名  DateTime日期函数
            string savePath = Server.MapPath(("~\\upfiles\\") + filename);//Server.MapPath 获得虚拟服务器相对路径
            FileUpload1.SaveAs(savePath);                        //SaveAs 将上传的文件内容保存在服务器上
            DataSet ds = ExecleDs(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++)
                {
                    string pininputdate = dr[i]["excel列名"].ToString();//日期 excel列名【名称不能变,否则就会出错】
                   
                    string insertstr = "insert语句";

                    SqlCommand cmd = new SqlCommand(insertstr, cn);
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (MembershipCreateUserException ex)       //捕捉异常
                    {
                        Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
                    }

                }
                Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>");
            }
            cn.Close();
           
        }

  • 相关阅读:
    BZOJ 1051: [HAOI2006]受欢迎的牛
    BZOJ 3668: [Noi2014]起床困难综合症
    BZOJ 4395: [Usaco2015 dec]Switching on the Lights
    BZOJ 2763: [JLOI2011]飞行路线
    Bzoj 3196 Tyvj 1730 二逼平衡树
    BZOJ 2407: 探险/4398: 福慧双修
    BZOJ 3040: 最短路(road)
    BZOJ 2809: [Apio2012]dispatching
    BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
    BZOJ 4590: [Shoi2015]自动刷题机
  • 原文地址:https://www.cnblogs.com/EDSON/p/2469339.html
Copyright © 2011-2022 走看看