日期:2014-05-17 浏览次数:20506 次
<%@ Page Language="C#" Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> String ConnectionString = String.Empty; protected void Page_Load(object sender, EventArgs e) { ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + ";Extended Properties='Excel 8.0;HDR=No;IMEX=2;'"; if (!IsPostBack) { DataBindExcel(); } } protected void UpDate_Click(object sender, EventArgs e) { using (OleDbConnection cn = new OleDbConnection(ConnectionString)) { cn.Open(); String sql = "UPDATE [Sheet1$B11:B11] SET F1 = '" + DateTime.Now.ToString() + "'"; OleDbCommand cmd = new OleDbCommand(sql, cn); cmd.ExecuteNonQuery(); cn.Dispose(); } Response.Redirect(Request.UrlReferrer.ToString()); } private void DataBindExcel() { using (OleDbConnection cn = new OleDbConnection(ConnectionString)) { cn.Open(); String sql = "select * FROM [Sheet1$]"; OleDbCommand cmd = new OleDbCommand(sql, cn); GridView1.DataSource = cmd.ExecuteReader(); GridView1.DataBind(); cn.Dispose(); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <asp:Button ID="UpDate" Text="更新数据为当前日期" runat="server" OnClick="UpDate_Click" /> </form