把EXCEL文件的數據通過頁面插入數據庫

前臺代碼:

<body>
    <form id="form1" runat="server">
    <div>
        boob <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="導入Excle預覽" />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        </div>
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="確定將以上數據導入數據庫"
            Visible="False" />
    </form>

</body>


 

後臺代碼:

protected void Button1_Click(object sender, EventArgs e)
        {
            string type1 = FileUpload1.PostedFile.ContentType;
            string type = type1.Substring(type1.LastIndexOf("-") + 1, 5);

            if (string.Equals(type, "excel"))
            {
                string newName = Server.MapPath("App_Data//") + DateTime.Now.ToString("hhmmss") + ".xls";
                FileUpload1.SaveAs(newName);
                string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + newName + ";Extended Properties=Excel 8.0;";
                OleDbConnection conn = new OleDbConnection(connStr);
                if (conn.State.ToString() == "Closed")
                {
                    conn.Open();
                }
                OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
                DataSet ds = new DataSet();
                oda.Fill(ds);
                conn.Close();
                GridView1.DataSource = ds;
                GridView1.DataBind();
                File.Delete(newName);
                Session["paper"] = ds;
                Button2.Visible = true;
            }

            else
            {
                Page.RegisterStartupScript("", "<script>alert('文件格式不正確')</script>");
            }

        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            DataSet ds = (DataSet)Session["paper"];
            SqlConnection conn = new SqlConnection("Server=PC-20120222VHZA;DataBase=Test;Integrated Security=True");
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string user = ds.Tables[0].Rows[i][0].ToString();
                string paper = ds.Tables[0].Rows[i][1].ToString();
                string score = ds.Tables[0].Rows[i][2].ToString();
                if (Sms.isrepetion(user, paper))
                {
                    string sql = "insert into [dbo].[paper]([user],[paper],[score]) values('" + user + "','" + paper + "','" + score + "')";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    try
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Response.Write("插入失敗!由於:" + ex.Message);
                    }
                }
            }
            conn.Close();
            Response.Write("<SCRIPT>alert('數據已成功導入到數據庫!');</SCRIPT>");
        }


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章