1.aspx頁面需要添加:EnableEventValidation="false"

實例:<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false"  CodeFile="DefCheckDate.aspx.cs"
    Inherits="WebAdmin_CustomRegister_ShortOverFlow_DefCheckDate" %>

2.後台代碼(實例):

public void btn_ExcelClick(object sender, EventArgs e)
    {
        if (Gdv_Sof.Rows.Count <= 0)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('沒有數據!')</script>");
            return;
        }
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=CheckDate.xls");
        Response.ContentType="application/excel";
        Response.Charset = "Big5";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("Big5");
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        Gdv_Sof.Columns[0].Visible = false;
        Gdv_Sof.Columns[1].Visible = false;
        Gdv_Sof.HeaderRow.Controls.Clear();
         Gdv_Sof.AllowPaging = false;
        Gdv_Sof.AllowSorting = false;
        BindGridView();
        Gdv_Sof.RenderControl(htw);
        Response.Write(sw);
        Response.End();
        Gdv_Sof.AllowPaging = true;
        Gdv_Sof.AllowSorting = true;
        BindGridView();
     
 
    }

//此方法一定要有VerifyRenderingInServerForm
    public override void VerifyRenderingInServerForm(Control control)
    {
     }

3.導出的數據格式設置:例如頁面為005,可是導出卻為5,就需要添加以下樣式vnd.ms-excel.numberFormat:@");

實例:

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

GridView導出Excel的更多相关文章

  1. GridView導出Excel 解決亂碼問題

    Response.Clear(); Response.Charset = "gb2312"; Response.Buffer = true; Response.AddHeader( ...

  2. C# gridview分頁導出excel

    #region 导出Excel方法 //导出到Excel按钮 protected void btnExport_Click(object sender, EventArgs e) { Export(& ...

  3. C#根據當前DataGridView查詢數據導出Excel

    private void btnsuggestinfo_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.C ...

  4. 導出Excel方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  5. Saiku更改導出文件的文件名(十九)

    Saiku更改導出文件的文件名 Saiku查询完数据之后,可以以excel,pdf,csv等格式将数据导出,这里我们来讲一下怎么更改导出的文件名. 找到对应的导出方法所在的js文件: saiku-se ...

  6. asp.net教程:GridView导出到Excel或Word文件

    asp.net教程:GridView导出到Excel或Word文件</ br> 在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户) ...

  7. DevExpress GridControl GridView 导出到 Excel 类

    说明: 1>GridView 导出到 Excel (如果分页,只导出当前页数据) 2>GridView 导出到 Excel 3>方法2可以参考DataTable 导出到 Excel ...

  8. 【asp.net】将GridView数据导出Excel

    概要: 中午睡了一会,醒来的时候看到老师叫我去办公室,需求是这样的,把excel表中的每个同学,判断图片目录中是否有对应的照片(图片的名字用的学号或身份证号码) 没有对应图片的学生记录,存入自己的数据 ...

  9. GridView导出成Excel字符"0"丢失/数字丢失的处理方式 收藏

    GridView导出成Excel字符"0"丢失/数字丢失的处理方式 收藏 GridView 导出成Excel文件,这个代码在网上比较多.但是发现存在一个问题,导出的数据中如果有&q ...

随机推荐

  1. Win XP 如何禁用屏保

    如果你试过 “在桌面空白处点击右键-[属性]-[屏幕保护程序],选择[无],点击[确定]”后,当时是可以去掉屏保.但如果重启计算机或者从待机状态唤醒后,屏保依然会出现,那么你可以试试下面的方法. 首先 ...

  2. R之pryr

    1. Pryr安装 由于项目pryr,还没有发布到CRAN,仅支持从github安装.要使用devtools包来通过github来安装,在https://github.com/hadley/pryr中 ...

  3. centos 升级GCC/G++

    #get rep yum install centos-release-scl-rh #yum install centos-release-scl # install g++ 5.2.1 yum - ...

  4. asp天猫自主发码的请求

    这几天在做天猫自主发码.实现了通知和核销部门.其他的部分待后续实现. 值得注意的是consume回调中,要加入sign_method=md5

  5. C#图片切割、图片压缩、缩略图生成

    C#图片切割.图片压缩.缩略图生成的实现代码 /// 图片切割函数  /// </summary>  /// <param name="sourceFile"&g ...

  6. hdu 1520 Anniversary party || codevs 1380 树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. .NET 实现异步处理的集中方式

    对于异步,相信大家都不十分陌生.准确点来说就是方法执行后立即返回,待到执行完毕会进行通知.就是当一个任务在执行的时候,尤其是需要耗费很长的时间进行处理的任务,如果利用单线程进行操作的话,势必造成界面的 ...

  8. gitflow workflow

    https://www.atlassian.com/git/tutorials/comparing-workflows/forking-workflow Dream big, work smart,  ...

  9. Asp.Net正则获取页面a标签里的内容

    Asp.Net正则获取页面a标签里的内容 string url = "http://www.114369.com"; string html = MyCLib.NetClass.S ...

  10. spring事务传播属性与隔离级别

    一.Propagation (事务的传播属性) Propagation : key属性确定代理应该给哪个方法增加事务行为.这样的属性最重要的部份是传播行为. 有以下选项可供使用: PROPAGATIO ...