GridView导出为Excel

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data.SqlClient;
using TB06030E.BEM;
using System.Drawing;
using System.Reflection;
using System.Web.SessionState;

public partial class jdwl_SelectAdmin_PrintTables_PrintStuInfo : System.Web.UI.Page
{

        BEM_tbOpenClass bem = new BEM_tbOpenClass();

        protected void Page_Load(object sender, EventArgs e)
        {

            GridView1.DataSource = Bind();
            GridView1.DataBind();
        }

        private DataTable Bind()
        {
            string sql = "";

            sql = "Select * from Customers";
            string DBConfig_sql = "server=10.0.12.98; database=ndwuli;uid=sa;pwd=sa";
            DataSet ds = new DataSet();
            SqlConnection sqlCon = new SqlConnection(DBConfig_sql);
            SqlCommand sqlCmd = new SqlCommand(sql, sqlCon);
            SqlDataAdapter sqlAd = new SqlDataAdapter();
            sqlAd.SelectCommand = sqlCmd;
            sqlAd.Fill(ds);

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("sno", typeof(string)));      //数据库中列次序为准
            dt.Columns.Add(new DataColumn("classkind", typeof(string)));

            dt.Columns.Add(new DataColumn("11", typeof(int)));
            dt.Columns.Add(new DataColumn("12", typeof(int)));
            dt.Columns.Add(new DataColumn("13", typeof(int)));
            dt.Columns.Add(new DataColumn("14", typeof(int)));
            dt.Columns.Add(new DataColumn("15", typeof(int)));

            DataRow dr;
            int k = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dr = dt.NewRow();

                for (int j = 0; j < 7; j++)
                    dr[j] = ds.Tables[0].Rows[i][j].ToString();


                dr[1] = " ";
                if (i % 8 == 0)
                {
                    k++;
                    dr[1] = "物理系" + k + "组"; //数据库中1为准
                }
                dt.Rows.Add(dr);

            }
            return dt;
        }

         public override void VerifyRenderingInServerForm( Control control )
         {

         }

          protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )
          {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
              e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
            }
          }


        protected void Button1_Click(object sender, EventArgs e)
        {
       
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
            // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.GridView1.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();

        }


}

你可能感兴趣的:(GridView)