GridView中的数据导出到Excel方法(经测试完整) (带模版列)

本文介绍了一种将ASP.NET中的GridView数据导出到Excel的方法,包括处理分页、特殊控件转换及设置响应头等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
GridView中的数据导出到Excel方法(经测试完整) 

protected void Button2_Click(object sender, EventArgs e)
    
{
        
if (this.GridView1.Rows.Count == 0)
        
{
            Response.Write(
"<script>alert('没有查找到数据,无法导出!')");
        }

        
else
        
{
            
this.GridView1.AllowPaging = false// 将有分页的GridView中的数据全部导出到Excel
            gvBond();
            export(
"application/ms-excel""工作人员.xls");
            
// 换成 export("application/ms-word", "工作人员.doc"); 那么导出的就是Word格式的了.
            this.GridView1.AllowPaging = true;
            gvBond();
        }

    }

     
public void export(string FileType, string FileName)
    
{
        
string style = @"<style>.text{mso-number-format:@}</script>";//导入到excel时,保存表里数字列中前面存在的 0 .
           PrepareGridViewForExport(Control gv)//将模版列显示出来
        Response.Clear();
        Response.Charset 
= "GB2312";
        Response.ContentEncoding 
= Encoding.UTF7;
        Response.AppendHeader(
"Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType 
= FileType;
        
this.EnableViewState = false;
        
this.GridView1.AllowPaging = false;
        System.Globalization.CultureInfo myCItrad 
= new System.Globalization.CultureInfo("ZH-CN"true);
        StringWriter sw 
= new StringWriter();
        HtmlTextWriter htw 
= new HtmlTextWriter(sw);
        
this.GridView1.RenderControl(htw);
        Response.Write(style);
        Response.Write(sw.ToString());
        
//Response.Write(dt.ToString());
        Response.End();
    }


    
public override void VerifyRenderingInServerForm(Control control)
    
{
        
//在后台中重载VerifyRenderingInServerForm()方法,否则报错为“类型"GridView"的控件"GridView1"必须放在具有 runat=server 的窗体标记内“
    }

    
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            e.Row.Cells[
3].Attributes.Add("class""text");//在数字列前存在的 0 的列中加入 class 样式 以便保存 0 
        }

    }


注:如果GricView中有分页的话,导出到Excel时就会报错.可通过修改页文件可以修正这个问题:EnableEventValidation 
= "false"

<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

如果GridView存在模板列,其中包含子控件,例如CheckBox等,导出EXCEL后就会出现该区域的不规律。所以要对模板列单独处理(转载)
 
private  void PrepareGridViewForExport(Control gv)//模式化特殊元素 flashcong
    {

        LinkButton lb 
= new LinkButton();
        Literal l 
= new Literal();
        
string name = String.Empty;

        
for (int i = 0; i < gv.Controls.Count; i++)
        
{

            
if (gv.Controls[i].GetType() == typeof(LinkButton))
            
{

                l.Text 
= (gv.Controls[i] as LinkButton).Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);

            }


            
else if (gv.Controls[i].GetType() == typeof(DropDownList))
            
{

                l.Text 
= (gv.Controls[i] as DropDownList).SelectedItem.Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);

            }


            
else if (gv.Controls[i].GetType() == typeof(CheckBox))
            
{

               l.Text 
= (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
               gv.Controls.Remove(gv.Controls[i]);
               gv.Controls.AddAt(i, l);

            }


            
else if (gv.Controls[i].GetType() == typeof(ImageButton))
            
{
              l.Text 
= "图片";
              gv.Controls.Remove(gv.Controls[i]);
              gv.Controls.AddAt(i, l);
            }


            
if (gv.Controls[i].HasControls())
            
{
                PrepareGridViewForExport(gv.Controls[i]);
            }


        }


    }
对以上特殊元素,可以替换成目标代码输出即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值