zoukankan      html  css  js  c++  java
  • 导出Exexcl类

    前台:

    <%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false"  CodeBehind="index1.aspx.cs" Inherits="benz_CSI.toExecl.index1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="font-weight: 700">
     
            <asp:Button ID="btnToExL" runat="server" Text="导出EXECL" onclick="btnToExL_Click" />
            <asp:GridView ID="GridView1" runat="server">
            </asp:GridView>
       
        </div>
        </form>
    </body>
    </html>

    后台:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Xml.Linq;
    namespace benz_CSI.toExecl
    {
        public partial class index1 : System.Web.UI.Page
        {
            public override void VerifyRenderingInServerForm(Control control)
            {
                //base.VerifyRenderingInServerForm(control);
            }

            JiaFaAndXD jiafa = new JiaFaAndXD();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    GridView1.DataSource = jiafa.GetList2("");
                    GridView1.DataBind();
                }
            }

            private void ToExcel(GridView gv, string name)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(name + ".xls", System.Text.Encoding.UTF8));
                Response.Charset = "gb2312";
                Response.ContentType = "application/vnd.xls";
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

                gv.AllowPaging = false;//将分页等属性取消
                gv.AllowSorting = true;//将允许排序属性取消
                gv.PagerStyle.ForeColor = System.Drawing.Color.White;
                gv.FooterStyle.ForeColor = System.Drawing.Color.White;
                gv.RenderControl(htmlWrite);

                Response.Write(stringWrite.ToString());
                Response.End();
            }

            protected void btnToExL_Click(object sender, EventArgs e)
            {
                ToExcel(GridView1, "能量值");
            }
        }
    }

  • 相关阅读:
    log4net的使用
    数据库概念及构成
    基于C#WPF框架——动画
    基于C# WPF框架的贪吃蛇
    使用Teigha.net读取CAD的常用功能模块
    Teigha.net实体属性注释
    .net(C#数据库访问) Mysql,Sql server,Sqlite,Access四种数据库的连接方式
    Delphi解析修改Json文件,基于superobject.pas(ISuperObject)
    C++结构体与Delphi结构体相互传参,结构体中包含结构体的嵌套,数组指针
    C++ Json解析CJsonObject的详细使用
  • 原文地址:https://www.cnblogs.com/wwy224y/p/execl.html
Copyright © 2011-2022 走看看