GridView数据导入Excel

来源:互联网 发布:flash player mac官方 编辑:程序博客网 时间:2024/04/20 02:45
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml" > 
  4. <head id="Head1" runat="server"> 
  5.     <title>GridView数据导入Excel</title>
  6. </head> 
  7. <body> 
  8.     <form id="form1" runat="server"> 
  9.     <div> 
  10.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333" PageSize="3" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">  
  11.             <FooterStyle BackColor="White" ForeColor="#000066" /> 
  12.             <Columns> 
  13.                 <asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" />
  14.                 <asp:BoundField DataField="Name" HeaderText="用户姓名" /> 
  15.                 <asp:BoundField DataField="Sex" HeaderText="性别" /> 
  16.                 <asp:BoundField DataField="Birthday" HeaderText="出生日期" DataFormatString="{0:yyyy年M月dd日}" HtmlEncode="False" />
  17.                 <asp:BoundField DataField="Salary" HeaderText="薪水" />
  18.                 <asp:BoundField DataField="Address" HeaderText="家庭住址" /> 
  19.                 <asp:BoundField DataField="PostCode" HeaderText="邮政编码" />
  20.             </Columns> 
  21.             <RowStyle ForeColor="#000066" /> 
  22.             <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> 
  23.             <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> 
  24.             <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> 
  25.         </asp:GridView>
  26.         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
  27.     </div>           
  28.     </form> 
  29. </body> 
  30. </html>
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Data.SqlClient;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Text;
  14. public partial class _Default : System.Web.UI.Page
  15. {
  16.     SqlConnection sqlConn;
  17.     SqlCommand sqlComm;
  18.     string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
  19.     protected void Page_Load(object sender, EventArgs e)
  20.     {
  21.         if (!IsPostBack)
  22.         {
  23.             databind();
  24.         }
  25.     }
  26.     private void databind()
  27.     {
  28.         string strSql = "select * from myDt";
  29.         sqlConn = new SqlConnection(strConn);
  30.         SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
  31.         DataSet ds = new DataSet();
  32.         sqlConn.Open();
  33.         sqlDa.Fill(ds, "myDt");
  34.         GridView1.DataSource = ds;
  35.         GridView1.DataKeyNames = new string[] { "ID" };
  36.         GridView1.DataBind();
  37.         for (int i = 0; i < GridView1.Rows.Count; i++)
  38.         {
  39.             DataRowView drv = ds.Tables["myDt"].DefaultView[i];
  40.             string strSalary = Convert.ToString(drv["Salary"]);
  41.             if (Convert.ToDouble(strSalary) < 2500)
  42.             {
  43.                 GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
  44.             }
  45.         }
  46.         sqlConn.Close();
  47.     }
  48.     double sum = 0;
  49.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  50.     {
  51.         if (e.Row.RowIndex >= 0)
  52.         {
  53.             sum += Convert.ToDouble(e.Row.Cells[4].Text);
  54.         }
  55.         else if(e.Row.RowType==DataControlRowType.Footer)
  56.         {
  57.             e.Row.Cells[3].Text = "总薪水为:";
  58.             e.Row.Cells[4].Text = sum.ToString();
  59.             e.Row.Cells[5].Text = "平均薪水为:";
  60.             e.Row.Cells[6].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
  61.         }
  62.     }
  63.     private void Export(string strFileType, string strFileName)
  64.     {
  65.         Response.AppendHeader("Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8).ToString());
  66.         Response.ContentType = strFileType;
  67.         this.EnableViewState = false;
  68.         StringWriter sw = new StringWriter();
  69.         HtmlTextWriter htw = new HtmlTextWriter(sw);
  70.         GridView1.RenderControl(htw);
  71.         Response.Write(sw.ToString());
  72.         Response.End();
  73.     }
  74.     public override void VerifyRenderingInServerForm(Control control)
  75.     {
  76.     }
  77.     protected void Button1_Click(object sender, EventArgs e)
  78.     {
  79.         Export("application/ms-excel""学生成绩报表.xls");
  80.     }
  81. }