16.GridView突出显示某一单元格(例如金额低于多少,分数不及格等)
效果图:
解决方案:主要是绑定后过滤
全部后台代码:
1GridView1.DataBind();
2for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
3{
4 DataRowView mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
5 string score = Convert.ToString(mydrv["起薪"]);
6 if (Convert.ToDouble(score) < 34297.00)//大家这里根据具体情况设置可能ToInt32等等
7 {
8 GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
9 }
10}
11sqlcon.Close();
2for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
3{
4 DataRowView mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
5 string score = Convert.ToString(mydrv["起薪"]);
6 if (Convert.ToDouble(score) < 34297.00)//大家这里根据具体情况设置可能ToInt32等等
7 {
8 GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
9 }
10}
11sqlcon.Close();
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11using System.Drawing;
12public partial class Default7 : System.Web.UI.Page
13{
14 SqlConnection sqlcon;
15 SqlCommand sqlcom;
16 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 bind();
22 }
23 }
24 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
25 {
26 GridView1.EditIndex = e.NewEditIndex;
27 bind();
28 }
29 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
30 {
31 sqlcon = new SqlConnection(strCon);
32 string sqlstr = "update 飞狐工作室 set 姓名='"
33 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
34 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
35 + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
36 sqlcom = new SqlCommand(sqlstr, sqlcon);
37 sqlcon.Open();
38 sqlcom.ExecuteNonQuery();
39 sqlcon.Close();
40 GridView1.EditIndex = -1;
41 bind();
42 }
43 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
44 {
45 GridView1.EditIndex = -1;
46 bind();
47 }
48 public void bind()
49 {
50 string sqlstr = "select top 10 * from 飞狐工作室";
51 sqlcon = new SqlConnection(strCon);
52 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
53 DataSet myds = new DataSet();
54 sqlcon.Open();
55 myda.Fill(myds, "飞狐工作室");
56 GridView1.DataSource = myds;
57 GridView1.DataKeyNames = new string[] { "身份证号码" };
58 GridView1.DataBind();
59 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
60 {
61 DataRowView mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
62 string score = Convert.ToString(mydrv["起薪"]);
63 if (Convert.ToDouble(score) < 34297.00)//大家这里根据具体情况设置可能ToInt32等等
64 {
65 GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
66 }
67 }
68 sqlcon.Close();
69 }
70}
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11using System.Drawing;
12public partial class Default7 : System.Web.UI.Page
13{
14 SqlConnection sqlcon;
15 SqlCommand sqlcom;
16 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 bind();
22 }
23 }
24 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
25 {
26 GridView1.EditIndex = e.NewEditIndex;
27 bind();
28 }
29 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
30 {
31 sqlcon = new SqlConnection(strCon);
32 string sqlstr = "update 飞狐工作室 set 姓名='"
33 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
34 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
35 + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
36 sqlcom = new SqlCommand(sqlstr, sqlcon);
37 sqlcon.Open();
38 sqlcom.ExecuteNonQuery();
39 sqlcon.Close();
40 GridView1.EditIndex = -1;
41 bind();
42 }
43 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
44 {
45 GridView1.EditIndex = -1;
46 bind();
47 }
48 public void bind()
49 {
50 string sqlstr = "select top 10 * from 飞狐工作室";
51 sqlcon = new SqlConnection(strCon);
52 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
53 DataSet myds = new DataSet();
54 sqlcon.Open();
55 myda.Fill(myds, "飞狐工作室");
56 GridView1.DataSource = myds;
57 GridView1.DataKeyNames = new string[] { "身份证号码" };
58 GridView1.DataBind();
59 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
60 {
61 DataRowView mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
62 string score = Convert.ToString(mydrv["起薪"]);
63 if (Convert.ToDouble(score) < 34297.00)//大家这里根据具体情况设置可能ToInt32等等
64 {
65 GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
66 }
67 }
68 sqlcon.Close();
69 }
70}
前台代码:
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" >
3<head id="Head1" runat="server">
4 <title>GridView突出显示某一单元格 清清月儿http://blog.csdn.net/21aspnet </title>
5</head>
6<body >
7 <form id="form1" runat="server">
8 <div>
9 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowEditing="GridView1_RowEditing"
10 OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" >
11 <FooterStyle BackColor="White" ForeColor="#000066" />
12 <Columns>
13 <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
14 <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
15 <asp:BoundField DataField="姓名" HeaderText="姓名" />
16 <asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
17 <asp:BoundField DataField="起薪" HeaderText="起薪" DataFormatString="{0:C}" HtmlEncode="false"/>
18 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
19 <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
20
21 </Columns>
22 <RowStyle ForeColor="#000066" />
23 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
24 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
25 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
26 </asp:GridView>
27 </div>
28 </form>
29</body>
30</html>
2<html xmlns="http://www.w3.org/1999/xhtml" >
3<head id="Head1" runat="server">
4 <title>GridView突出显示某一单元格 清清月儿http://blog.csdn.net/21aspnet </title>
5</head>
6<body >
7 <form id="form1" runat="server">
8 <div>
9 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowEditing="GridView1_RowEditing"
10 OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" >
11 <FooterStyle BackColor="White" ForeColor="#000066" />
12 <Columns>
13 <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
14 <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
15 <asp:BoundField DataField="姓名" HeaderText="姓名" />
16 <asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
17 <asp:BoundField DataField="起薪" HeaderText="起薪" DataFormatString="{0:C}" HtmlEncode="false"/>
18 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
19 <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
20
21 </Columns>
22 <RowStyle ForeColor="#000066" />
23 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
24 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
25 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
26 </asp:GridView>
27 </div>
28 </form>
29</body>
30</html>
17.GridView加入自动求和求平均值小计
效果图:
解决方案:
1private double sum = 0;//取指定列的数据和,你要根据具体情况对待可能你要处理的是int
2protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
3{
4 if (e.Row.RowIndex >= 0)
5 {
6 sum += Convert.ToDouble(e.Row.Cells[6].Text);
7 }
8 else if (e.Row.RowType == DataControlRowType.Footer)
9 {
10 e.Row.Cells[5].Text = "总薪水为:";
11 e.Row.Cells[6].Text = sum.ToString();
12 e.Row.Cells[3].Text = "平均薪水为:";
13 e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
14 }
15}
2protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
3{
4 if (e.Row.RowIndex >= 0)
5 {
6 sum += Convert.ToDouble(e.Row.Cells[6].Text);
7 }
8 else if (e.Row.RowType == DataControlRowType.Footer)
9 {
10 e.Row.Cells[5].Text = "总薪水为:";
11 e.Row.Cells[6].Text = sum.ToString();
12 e.Row.Cells[3].Text = "平均薪水为:";
13 e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
14 }
15}
后台全部代码:
前台:唯一的花头就是设置ShowFooter="True" ,否则默认表头为隐藏的!
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11using System.Drawing;
12public partial class Default7 : System.Web.UI.Page
13{
14 SqlConnection sqlcon;
15 SqlCommand sqlcom;
16 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 bind();
22 }
23 }
24 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
25 {
26 GridView1.EditIndex = e.NewEditIndex;
27 bind();
28 }
29 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
30 {
31 sqlcon = new SqlConnection(strCon);
32 string sqlstr = "update 飞狐工作室 set 姓名='"
33 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
34 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
35 + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
36 sqlcom = new SqlCommand(sqlstr, sqlcon);
37 sqlcon.Open();
38 sqlcom.ExecuteNonQuery();
39 sqlcon.Close();
40 GridView1.EditIndex = -1;
41 bind();
42 }
43 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
44 {
45 GridView1.EditIndex = -1;
46 bind();
47 }
48 public void bind()
49 {
50 string sqlstr = "select top 5 * from 飞狐工作室";
51 sqlcon = new SqlConnection(strCon);
52 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
53 DataSet myds = new DataSet();
54 sqlcon.Open();
55 myda.Fill(myds, "飞狐工作室");
56 GridView1.DataSource = myds;
57 GridView1.DataKeyNames = new string[] { "身份证号码" };
58 GridView1.DataBind();
59 sqlcon.Close();
60 }
61 private double sum = 0;//取指定列的数据和
62 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
63 {
64 if (e.Row.RowIndex >= 0)
65 {
66 sum += Convert.ToDouble(e.Row.Cells[6].Text);
67 }
68 else if (e.Row.RowType == DataControlRowType.Footer)
69 {
70 e.Row.Cells[5].Text = "总薪水为:";
71 e.Row.Cells[6].Text = sum.ToString();
72 e.Row.Cells[3].Text = "平均薪水为:";
73 e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
74 }
75 }
76}
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11using System.Drawing;
12public partial class Default7 : System.Web.UI.Page
13{
14 SqlConnection sqlcon;
15 SqlCommand sqlcom;
16 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 bind();
22 }
23 }
24 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
25 {
26 GridView1.EditIndex = e.NewEditIndex;
27 bind();
28 }
29 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
30 {
31 sqlcon = new SqlConnection(strCon);
32 string sqlstr = "update 飞狐工作室 set 姓名='"
33 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
34 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
35 + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
36 sqlcom = new SqlCommand(sqlstr, sqlcon);
37 sqlcon.Open();
38 sqlcom.ExecuteNonQuery();
39 sqlcon.Close();
40 GridView1.EditIndex = -1;
41 bind();
42 }
43 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
44 {
45 GridView1.EditIndex = -1;
46 bind();
47 }
48 public void bind()
49 {
50 string sqlstr = "select top 5 * from 飞狐工作室";
51 sqlcon = new SqlConnection(strCon);
52 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
53 DataSet myds = new DataSet();
54 sqlcon.Open();
55 myda.Fill(myds, "飞狐工作室");
56 GridView1.DataSource = myds;
57 GridView1.DataKeyNames = new string[] { "身份证号码" };
58 GridView1.DataBind();
59 sqlcon.Close();
60 }
61 private double sum = 0;//取指定列的数据和
62 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
63 {
64 if (e.Row.RowIndex >= 0)
65 {
66 sum += Convert.ToDouble(e.Row.Cells[6].Text);
67 }
68 else if (e.Row.RowType == DataControlRowType.Footer)
69 {
70 e.Row.Cells[5].Text = "总薪水为:";
71 e.Row.Cells[6].Text = sum.ToString();
72 e.Row.Cells[3].Text = "平均薪水为:";
73 e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
74 }
75 }
76}
前台:唯一的花头就是设置ShowFooter="True" ,否则默认表头为隐藏的!
1<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowEditing="GridView1_RowEditing"
2 OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" >
3 <FooterStyle BackColor="White" ForeColor="#000066" />
4 <Columns>
5 <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
6 <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
7 <asp:BoundField DataField="姓名" HeaderText="姓名" />
8 <asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
9 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
10 <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
11 <asp:BoundField DataField="起薪" HeaderText="起薪" />
12
13 </Columns>
14 <RowStyle ForeColor="#000066" />
15 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
16 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
17 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
18</asp:GridView>
2 OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" >
3 <FooterStyle BackColor="White" ForeColor="#000066" />
4 <Columns>
5 <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
6 <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
7 <asp:BoundField DataField="姓名" HeaderText="姓名" />
8 <asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
9 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
10 <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
11 <asp:BoundField DataField="起薪" HeaderText="起薪" />
12
13 </Columns>
14 <RowStyle ForeColor="#000066" />
15 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
16 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
17 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
18</asp:GridView>
18.GridView数据导入Excel/Excel数据读入GridView
效果图:
解决方案:
页面增加一个按钮,单击事件添加如下方法:
1protected void Button1_Click(object sender, EventArgs e)
2{
3 Export("application/ms-excel", "学生成绩报表.xls");
4}
5private void Export(string FileType, string FileName)
6{
7 Response.Charset = "GB2312";
8 Response.ContentEncoding = System.Text.Encoding.UTF7;
9 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
10 Response.ContentType = FileType;
11 this.EnableViewState = false;
12 StringWriter tw = new StringWriter();
13 HtmlTextWriter hw = new HtmlTextWriter(tw);
14 GridView1.RenderControl(hw);
15 Response.Write(tw.ToString());
16 Response.End();
17}
18//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
19public override void VerifyRenderingInServerForm(Control control)
20{
21}
2{
3 Export("application/ms-excel", "学生成绩报表.xls");
4}
5private void Export(string FileType, string FileName)
6{
7 Response.Charset = "GB2312";
8 Response.ContentEncoding = System.Text.Encoding.UTF7;
9 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
10 Response.ContentType = FileType;
11 this.EnableViewState = false;
12 StringWriter tw = new StringWriter();
13 HtmlTextWriter hw = new HtmlTextWriter(tw);
14 GridView1.RenderControl(hw);
15 Response.Write(tw.ToString());
16 Response.End();
17}
18//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
19public override void VerifyRenderingInServerForm(Control control)
20{
21}
还有由于是文件操作所以要引入名称空间IO和Text
后台代码:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11using System.Drawing;
12using System.IO;
13using System.Text;
14public partial class Default7 : System.Web.UI.Page
15{
16 SqlConnection sqlcon;
17 SqlCommand sqlcom;
18 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
19 protected void Page_Load(object sender, EventArgs e)
20 {
21 if (!IsPostBack)
22 {
23 bind();
24 }
25 }
26
27 public void bind()
28 {
29 string sqlstr = "select top 5 * from 飞狐工作室";
30 sqlcon = new SqlConnection(strCon);
31 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
32 DataSet myds = new DataSet();
33 sqlcon.Open();
34 myda.Fill(myds, "飞狐工作室");
35 GridView1.DataSource = myds;
36 GridView1.DataKeyNames = new string[] { "身份证号码" };
37 GridView1.DataBind();
38 sqlcon.Close();
39 }
40 protected void Button1_Click(object sender, EventArgs e)
41 {
42 Export("application/ms-excel", "学生成绩报表.xls");
43 }
44 private void Export(string FileType, string FileName)
45 {
46 Response.Charset = "GB2312";
47 Response.ContentEncoding = System.Text.Encoding.UTF7;
48 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
49 Response.ContentType = FileType;
50 this.EnableViewState = false;
51 StringWriter tw = new StringWriter();
52 HtmlTextWriter hw = new HtmlTextWriter(tw);
53 GridView1.RenderControl(hw);
54 Response.Write(tw.ToString());
55 Response.End();
56 }
57 public override void VerifyRenderingInServerForm(Control control)
58 {
59 }
60}
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11using System.Drawing;
12using System.IO;
13using System.Text;
14public partial class Default7 : System.Web.UI.Page
15{
16 SqlConnection sqlcon;
17 SqlCommand sqlcom;
18 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
19 protected void Page_Load(object sender, EventArgs e)
20 {
21 if (!IsPostBack)
22 {
23 bind();
24 }
25 }
26
27 public void bind()
28 {
29 string sqlstr = "select top 5 * from 飞狐工作室";
30 sqlcon = new SqlConnection(strCon);
31 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
32 DataSet myds = new DataSet();
33 sqlcon.Open();
34 myda.Fill(myds, "飞狐工作室");
35 GridView1.DataSource = myds;
36 GridView1.DataKeyNames = new string[] { "身份证号码" };
37 GridView1.DataBind();
38 sqlcon.Close();
39 }
40 protected void Button1_Click(object sender, EventArgs e)
41 {
42 Export("application/ms-excel", "学生成绩报表.xls");
43 }
44 private void Export(string FileType, string FileName)
45 {
46 Response.Charset = "GB2312";
47 Response.ContentEncoding = System.Text.Encoding.UTF7;
48 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
49 Response.ContentType = FileType;
50 this.EnableViewState = false;
51 StringWriter tw = new StringWriter();
52 HtmlTextWriter hw = new HtmlTextWriter(tw);
53 GridView1.RenderControl(hw);
54 Response.Write(tw.ToString());
55 Response.End();
56 }
57 public override void VerifyRenderingInServerForm(Control control)
58 {
59 }
60}
前台:
1<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3"
2 BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" >
3 <FooterStyle BackColor="White" ForeColor="#000066" />
4 <Columns>
5 <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
6 <asp:BoundField DataField="姓名" HeaderText="姓名" />
7 <asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
8 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
9 <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
10 <asp:BoundField DataField="起薪" HeaderText="起薪" />
11
12 </Columns>
13 <RowStyle ForeColor="#000066" />
14 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
15 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
16 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
17</asp:GridView>
18<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
2 BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" >
3 <FooterStyle BackColor="White" ForeColor="#000066" />
4 <Columns>
5 <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
6 <asp:BoundField DataField="姓名" HeaderText="姓名" />
7 <asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
8 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
9 <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
10 <asp:BoundField DataField="起薪" HeaderText="起薪" />
11
12 </Columns>
13 <RowStyle ForeColor="#000066" />
14 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
15 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
16 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
17</asp:GridView>
18<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
读取Excel数据的代码:这个很简单的
1private DataSet CreateDataSource()
2{
3 string strCon;
4 strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;";
5 OleDbConnection olecon = new OleDbConnection(strCon);
6 OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
7 DataSet myds = new DataSet();
8 myda.Fill(myds);
9 return myds;
10}
11protected void Button1_Click(object sender, EventArgs e)
12{
13 GridView1.DataSource = CreateDataSource();
14 GridView1.DataBind();
15}
2{
3 string strCon;
4 strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;";
5 OleDbConnection olecon = new OleDbConnection(strCon);
6 OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
7 DataSet myds = new DataSet();
8 myda.Fill(myds);
9 return myds;
10}
11protected void Button1_Click(object sender, EventArgs e)
12{
13 GridView1.DataSource = CreateDataSource();
14 GridView1.DataBind();
15}