zoukankan      html  css  js  c++  java
  • GridView導出Excel

    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:@");
            }
        
        }

    多一分冷靜,少一分浮躁
  • 相关阅读:
    Merge Intervals
    Jump Game
    微信小程序----button组件
    微信小程序----日期时间选择器(自定义精确到分秒或时段)(MUI日期时间)
    禁止搜索引擎抓取robots.txt文件设置方法
    [转载]【转】代码的版权声明怎么写
    微信小程序----picker选择器(picker、省市区选择器)(MUI选择器)
    微信小程序----模板(template)
    nginx安全配置
    微信小程序----icon组件
  • 原文地址:https://www.cnblogs.com/AnnyGird-LiMing/p/4955608.html
Copyright © 2011-2022 走看看