<%@ Page Title="主页" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <h2> 欢迎使用 ASP.NET! </h2> <p> 若要了解关于 ASP.NET 的详细信息,请访问 <a href="http://www.asp.net/cn" title="ASP.NET 网站">www.asp.net/cn</a>。 </p> <p> 您还可以找到 <a href="http://go.microsoft.com/fwlink/?LinkID=152368" title="MSDN ASP.NET 文档">MSDN 上有关 ASP.NET 的文档</a>。 </p> <p> <asp:Button ID="Button1" runat="server" Text="显示Excel数据" onclick="Button1_Click" /> </p> <p> <asp:GridView ID="GridView1" runat="server" Width="100%"> <Columns> <asp:TemplateField HeaderText="操作"> <ItemTemplate> <asp:Button ID="Button3" runat="server" Text="删除" CommandArgument=‘<%# Eval("编号") %>‘ onclick="Button3_Click" /> <asp:Button ID="Button4" runat="server" Text="编辑" CommandArgument=‘<%# Eval("编号") %>‘ onclick="Button4_Click" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </p> <p> 编号:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </p> <p> 姓名:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </p> <p> 性别: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </p> <p> <asp:Button ID="Button2" runat="server" Text="新增" onclick="Button2_Click" /> <asp:Button ID="Button5" runat="server" Text="提交" onclick="Button5_Click" style="height: 21px" /> </p> <p> </p> <p> </p> <p> </p> <p> </p> </asp:Content>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.OleDb; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=‘Excel 8.0;HDR=yes‘"; protected void Page_Load(object sender, EventArgs e) { string fileName = "content/student.xls"; fileName = Server.MapPath(fileName); connStr = string.Format(connStr,fileName);//连接字符串 } protected void Button1_Click(object sender, EventArgs e) { //读取student.xls文件中的学生数据 BindList(); } private void BindList() { string sql = "select * from [Sheet1$]"; OleDbConnection conn = new OleDbConnection(connStr); conn.Open(); OleDbCommand cmd = new OleDbCommand(sql, conn); OleDbDataAdapter da = new OleDbDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); conn.Close(); GridView1.DataSource = dt; GridView1.DataBind(); } //新增 protected void Button2_Click(object sender, EventArgs e) { string sql = "insert into [Sheet1$] values(@a,@b,@c)"; OleDbConnection conn = new OleDbConnection(connStr); conn.Open(); OleDbCommand cmd = new OleDbCommand(sql,conn); OleDbParameter[] pm = new OleDbParameter[3]; pm[0] = new OleDbParameter("@a",TextBox1.Text); pm[1] = new OleDbParameter("@b",TextBox2.Text); pm[2] = new OleDbParameter("@c",TextBox3.Text); foreach (OleDbParameter item in pm) { cmd.Parameters.Add(item); } int i = cmd.ExecuteNonQuery(); conn.Close(); if (i>0) { Response.Write("新增成功"); BindList(); } else { Response.Write("新增失败"); } } //删除 protected void Button3_Click(object sender, EventArgs e) { Button bt= sender as Button; string id = bt.CommandArgument; } //编辑 protected void Button4_Click(object sender, EventArgs e) { string sql = "select * from [Sheet1$] where 编号=@a"; Button bt = sender as Button; string id = bt.CommandArgument; OleDbParameter pm = new OleDbParameter("@a", id); OleDbConnection conn = new OleDbConnection(connStr); conn.Open(); OleDbCommand cmd = new OleDbCommand(sql,conn); cmd.Parameters.Add(pm); OleDbDataReader sdr = cmd.ExecuteReader(); bool b = sdr.Read(); if (b==true) { TextBox1.Text = sdr["编号"].ToString(); TextBox2.Text = sdr["姓名"].ToString(); TextBox3.Text = sdr["性别"].ToString(); } conn.Close(); } //提交 protected void Button5_Click(object sender, EventArgs e) { string sql = "update [Sheet1$] set 姓名=@b,性别=@c where 编号=@a"; OleDbParameter[] pm = new OleDbParameter[3]; pm[0] = new OleDbParameter("@a",TextBox1.Text); pm[1] = new OleDbParameter("@b",TextBox2.Text); pm[2] = new OleDbParameter("@c",TextBox3.Text); OleDbConnection conn = new OleDbConnection(connStr); conn.Open(); OleDbCommand cmd = new OleDbCommand(sql,conn); foreach (OleDbParameter item in pm) { cmd.Parameters.Add(item); } int i = cmd.ExecuteNonQuery(); conn.Close(); if (i>0) { BindList(); Response.Write("修改成功"); } else { Response.Write("修改失败"); } } } }
原文:http://www.cnblogs.com/xiaz/p/5243037.html