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. python中写shell(转)

    python中写shell,亲测可用,转自stackoverflow To run a bash script, copy from stackoverflow def run_script(scri ...

  2. 卷积神经网络和CIFAR-10:Yann LeCun专访 Convolutional Nets and CIFAR-10: An Interview with Yann LeCun

    Recently Kaggle hosted a competition on the CIFAR-10 dataset. The CIFAR-10 dataset consists of 60k 3 ...

  3. sleep和wait到底什么区别

    wait是在当前线程持有wait对象锁的情况下,暂时放弃锁,并让出CPU资源,并积极等待其它线程调用同一对象的notify或者notifyAll方法.注意,即使只有一个线程在等待,并且有其它线程调用了 ...

  4. How to fix broken packages?(转)

    Try Code: sudo apt-get update to update your package list.Then Code: sudo apt-get autoclean to clean ...

  5. flex 4 布局样式

    Flex 4 样式与布局 第一篇 Flex 4 与自定义布局(Layout) Flex 4/Spark组件架构的新功能之一是可以定制一个容器的布局而不必改变容器本身.您需要做的就是定义一个自定义布局. ...

  6. c#操作txt

    C#追加文件 StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt"); sw ...

  7. 在linux系统下检查postgresql数据库安装,登录数据库及简单的查看数据库

    1.    检查Linux系统是否安装数据库 首先查看自己的系统是否安装了postgresql数据库命令如下: rpm -qa | grep postgresql 如果没有显示查询结果(如下图所示)说 ...

  8. mysql 语句大全

    1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...

  9. 关于c#字典key不存在的测试

    之前一直隐约记得没有创建key会报异常,测试了下. 测试结果: 写入值,如果不存在key,会自动创建. 取值,如果不存在key,会报异常. 一般用c#提供了尝试取值方法,不过有out参数,考虑写扩展 ...

  10. GMT and CST

    GMT(Greenwich Mean Time) 代表格林尼治标准时间 而CST却同时可以代表如下 4 个不同的时区: Central Standard Time (USA) UT-6:00    C ...