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

时间: 2024-10-27 17:55:50

GridView導出Excel的相关文章

table導出excel | 後臺

第一種也是比較好的方法: String str=afwtHtml; FileWriter writer; try { writer = new FileWriter("D:\\logs\\nihao2.xls"); writer.write(str); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } return "ExportFinished"; 第二

導出Excel方法

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; namespace WmsClient { public class Excel { public stat

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

private void btnsuggestinfo_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("單號", typeof(string)); dt.Columns.Add("項次", typeof(decimal)); dt.Columns.Add("項次預約數量", typeof(decimal)); dt.Column

導出到Excel

void clicked(){ #Excel HWND HWND; SysExcelApplication excel; SysExcelWorkbooks books; SysExcelWorkbook book; SysExcelWorksheets sheets; SysExcelWorksheet sheet; SysExcelRange columns; SysExcelRange column; SysExcelCells cells; SysExcelCell cell; SysE

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

Saiku更改導出文件的文件名 Saiku查询完数据之后,可以以excel,pdf,csv等格式将数据导出,这里我们来讲一下怎么更改导出的文件名. 找到对应的导出方法所在的js文件: saiku-server\tomcat\webapps\ROOT\js\saiku\views\WorkspaceToolbar.js 这里分别有三个方法导出不同形式的文件: export_xls export_csv export_pdf 我们主要更改里面的filename即可啦! 可以自定义filename,我

DevExpress GridControl GridView 导出到 Excel 类

说明: 1>GridView 导出到 Excel (如果分页,只导出当前页数据) 2>GridView 导出到 Excel 3>方法2可以参考DataTable 导出到 Excel 自定义类如下: #region GridView 导出到 Excel Method First /// <summary> /// GridView 导出到 Excel (如果分页,只导出当前页数据) /// </summary> /// <param name="gv

GridView内容导出Excel文件

首先,将数据库中的内容绑定到GridView中,这个方法随意,这里采用ADO.NET实现. protected void Page_Load(object sender, EventArgs e) { SqlDataAdapter sda = new SqlDataAdapter("select * from tb_StuResult", sqlcon); DataSet ds = new DataSet(); sqlcon.Open(); sda.Fill(ds); sqlcon.C

GridView导出成Excel字符&quot;0&quot;丢失/数字丢失的处理方式 收藏

GridView导出成Excel字符"0"丢失/数字丢失的处理方式 收藏 GridView 导出成Excel文件,这个代码在网上比较多.但是发现存在一个问题,导出的数据中如果有"012457890"的内容,用Excel打开后就变成 了"12457890",少了前面的0;原因是Excel把它当作数字来格式化了,就把"0"给去掉了.   解决思路:在Excel中作一个包含有"012457890"的内容,设定单元

gridview导出到excel

public static void gvexcel(GridView grv, string strcells) { string style = @"<style> .text { mso-number-format:\@; } </script> ";//解决0缺失问题 StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter