GridView1.DataSource = CreateDataSource(); GridView1.DataBind(); protected void Button1_Click(object sender, EventArgs e) return myds; myda.Fill(myds); DataSet myds = new DataSet(); OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon); OleDbConnection olecon = new OleDbConnection(strCon); strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;"; string strCon; private DataSet CreateDataSource() 读取Excel数据的代码:这个很简单的 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" /> </asp:GridView> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" Css DataGridFixedHeader"/> <SelectedRowStyle BackColor="#" Font-Bold="True" ForeColor="White" /> <RowStyle ForeColor="#000066" /> </Columns> <asp:BoundField DataField="起薪" HeaderText="起薪" /> <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" /> <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" /> <asp:BoundField DataField="出生日期" HeaderText="邮政编码" /> <asp:BoundField DataField="姓名" HeaderText="姓名" /> <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" /> <Columns> <FooterStyle BackColor="White" ForeColor="#000066" /> BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" > <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" public override void VerifyRenderingInServerForm(Control control) Response.End(); Response.Write(tw.ToString()); GridView1.RenderControl(hw); HtmlTextWriter hw = new HtmlTextWriter(tw); StringWriter tw = new StringWriter(); this.EnableViewState = false; Response.ContentType = FileType; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentEncoding = System.Text.Encoding.UTF7; Response.Charset = "GB2312"; private void Export(string FileType, string FileName) Export("application/ms-excel", "学生成绩报表.xls"); protected void Button1_Click(object sender, EventArgs e) sqlcon.Close(); GridView1.DataBind(); GridView1.DataKeyNames = new string[] { "身份证号码" }; GridView1.DataSource = myds; myda.Fill(myds, "飞狐工作室"); sqlcon.Open(); DataSet myds = new DataSet(); SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); sqlcon = new SqlConnection(strCon); string sqlstr = "select top 5 * from 飞狐工作室"; public void bind() bind(); if (!IsPostBack) protected void Page_Load(object sender, EventArgs e) string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa"; SqlCommand sqlcom; SqlConnection sqlcon; public partial class Default7 : System.Web.UI.Page using System.Text; using System.IO; using System.Drawing; using System.Data.SqlClient; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.WebControls; using System.Web.UI; using System.Web.Security; using System.Web; using System.Configuration; using System.Data; using System; 后台代码:其实GridView操作(7)。 还有由于是文件操作所以要引入名称空间IO和Text public override void VerifyRenderingInServerForm(Control control) //如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 Response.End(); Response.Write(tw.ToString()); GridView1.RenderControl(hw); HtmlTextWriter hw = new HtmlTextWriter(tw); StringWriter tw = new StringWriter(); this.EnableViewState = false; Response.ContentType = FileType; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentEncoding = System.Text.Encoding.UTF7; Response.Charset = "GB2312"; private void Export(string FileType, string FileName) Export("application/ms-excel", "学生成绩报表.xls"); protected void Button1_Click(object sender, EventArgs e) 页面增加一个按钮,对比一下e)。学习 否则默认表头为隐藏的!解决方案: 效果图:听说EventArgs。 18.GridView数据导入Excel/Excel数据读入GridView </asp:GridView> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" Css DataGridFixedHeader"/> <SelectedRowStyle BackColor="#" Font-Bold="True" ForeColor="White" /> <RowStyle ForeColor="#000066" /> </Columns> <asp:BoundField DataField="起薪" HeaderText="起薪" /> <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" /> <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" /> <asp:BoundField DataField="出生日期" HeaderText="邮政编码" /> <asp:BoundField DataField="姓名" HeaderText="姓名" /> <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" /> <asp:CommandField HeaderText="编辑" ShowEditButton="True" /> <Columns> <FooterStyle BackColor="White" ForeColor="#000066" /> OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" > <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowEditing="GridView1_RowEditing" 前台:唯一的花头就是设置ShowFooter="True",e)。你要根据具体情况对待可能你要处理的是int e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString(); e.Row.Cells[3].Text = "平均薪水为:"; e.Row.Cells[6].Text = sum.ToString(); e.Row.Cells[5].Text = "总薪水为:gridview。"; else if (e.Row.RowType == DataControlRowType.Footer) sum += Convert.ToDouble(e.Row.Cells[6].Text); if (e.Row.RowIndex >= 0) protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) private double sum = 0;//取指定列的数据和 sqlcon.Close(); GridView1.DataBind(); GridView1.DataKeyNames = new string[] { "身份证号码" }; GridView1.DataSource = myds; myda.Fill(myds, "飞狐工作室"); sqlcon.Open(); DataSet myds = new DataSet(); SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); sqlcon = new SqlConnection(strCon); string sqlstr = "select top 5 * from 飞狐工作室"; public void bind() bind(); GridView1.EditIndex = -1; protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) bind(); GridView1.EditIndex = -1; sqlcon.Close(); sqlcom.ExecuteNonQuery(); sqlcon.Open(); sqlcom = new SqlCommand(sqlstr, sqlcon); + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='" string sqlstr = "update 飞狐工作室 set 姓名='" sqlcon = new SqlConnection(strCon); protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) bind(); GridView1.EditIndex = e.NewEditIndex; protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) bind(); if (!IsPostBack) protected void Page_Load(object sender, EventArgs e) string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa"; SqlCommand sqlcom; SqlConnection sqlcon; public partial class Default7 : System.Web.UI.Page using System.Drawing; using System.Data.SqlClient; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.WebControls; using System.Web.UI; using System.Web.Security; using System.Web; using System.Configuration; using System.Data; using System; 后台全部代码:操作。 e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString(); e.Row.Cells[3].Text = "平均薪水为:"; e.Row.Cells[6].Text = sum.ToString(); e.Row.Cells[5].Text = "总薪水为:听听eventargs。"; else if (e.Row.RowType == DataControlRowType.Footer) sum += Convert.ToDouble(e.Row.Cells[6].Text); if (e.Row.RowIndex >= 0) protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) private double sum = 0;//取指定列的数据和, 解决方案:看着GridView操作(7)。 效果图:听听e。 17.GridView加入自动求和求平均值小计 EventArgs (责任编辑:admin) |