当前位置:  编程技术>.net/c#/asp.net

GridView导出为Excel数据的实现代码

    来源: 互联网  发布时间:2014-08-30

    本文导语:  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;...

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();
        }
}


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 利用sender的Parent获取GridView中的当前行(获取gridview的值)
  • DevExpress实现GridView当无数据行时提示消息
  • GridView添加滚动条的二种方法
  • 编辑gridview的小例子
  • GridView控件列上格式化时间的用法举例
  • 为GridView添加复选框的方法
  • asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(一)
  • gridview更新时获取不到textbox中新值的解决方法
  • gridview的buttonfield获取该行的索引值(实例讲解)
  • asp.net GridView删除对话框的二个方法
  • asp.net GridView用法笔记
  • GridView控件事件详细解析
  • asp.net遍历文件夹下所有子文件夹并绑定到gridview上的方法
  • Android之ScrollView嵌套ListView和GridView冲突的解决方法
  • gridview 行选添加颜色和事件
  • 动态改变gridview列宽度函数分享
  • asp.net GridView控件的几个事件的用法详解
  • ASP.NET4 GridView的四种排序样式详解
  • GridView中点击CheckBox选中一行来改变此行的颜色
  • GridView 超链接使用方法简介


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3