简介:

在本文中我们将具体介绍如何将ASP.Net 2.0下的GridView导出为Excel.

本文的焦点是Gridview导为Excel功能和它的数据绑定只出口示范功能.

本文代码可以用来导出为Excel的功能但不局限于这个部分,还可以在很多项目中使用。

 

Step 1: Setup your web page with the Gridview

这里我假定你满足:在一个页面中有个名为GridView1GridView。在GridView中我们绑定了一个名为ContactPhoneSQL数据库。接下来的代码就是如何将GridView导出为Excel并且不依赖于具体的数据绑定还具有场景自改变能力。

ContactPhone Table Structure:

Column Name

Type

ContactID

Int (Identity)

FName

Varchar(50)

LName

Varchar(50)

ContactPhone

Varchar(20)

Step: The Actual Export

这段代码是直接输出为Excel的,你也可以改变content-dispositionContentType以输出不同的类型。

 

 1将ASP.Net 2.0下的GridView导出为Excel.(转)string attachment = "attachment; filename=Contacts.xls";
 2将ASP.Net 2.0下的GridView导出为Excel.(转)
 3将ASP.Net 2.0下的GridView导出为Excel.(转)Response.ClearContent();
 4将ASP.Net 2.0下的GridView导出为Excel.(转)
 5将ASP.Net 2.0下的GridView导出为Excel.(转)Response.AddHeader("content-disposition", attachment);
 6将ASP.Net 2.0下的GridView导出为Excel.(转)
 7将ASP.Net 2.0下的GridView导出为Excel.(转)Response.ContentType = "application/ms-excel";
 8将ASP.Net 2.0下的GridView导出为Excel.(转)
 9将ASP.Net 2.0下的GridView导出为Excel.(转)StringWriter sw = new StringWriter();
10将ASP.Net 2.0下的GridView导出为Excel.(转)
11将ASP.Net 2.0下的GridView导出为Excel.(转)HtmlTextWriter htw = new HtmlTextWriter(sw);
12将ASP.Net 2.0下的GridView导出为Excel.(转)
13将ASP.Net 2.0下的GridView导出为Excel.(转)GridView1.RenderControl(htw);
14将ASP.Net 2.0下的GridView导出为Excel.(转)
15将ASP.Net 2.0下的GridView导出为Excel.(转)Response.Write(sw.ToString());
16将ASP.Net 2.0下的GridView导出为Excel.(转)
17将ASP.Net 2.0下的GridView导出为Excel.(转)Response.End(); 
18将ASP.Net 2.0下的GridView导出为Excel.(转)
19将ASP.Net 2.0下的GridView导出为Excel.(转)



如果你运行以上代码,将返回一个HttpException

'GridView1'是一个类型为'GridView'的控件,必须为其添加一个runat=server标记.

为避免这个错误,我们添加以下代码:

 

1将ASP.Net 2.0下的GridView导出为Excel.(转)public override void VerifyRenderingInServerForm(Control control)
2将ASP.Net 2.0下的GridView导出为Excel.(转)
3

 

Step : Convert the contents

如果GridView中有其它控件,比如CheckboxesDropdownlists,我们需要将它转换为其相关的值,以下递归就用于导出Excel前的准备工作,将各类控件转换为其相关值.

 

 1将ASP.Net 2.0下的GridView导出为Excel.(转)private void PrepareGridViewForExport(Control gv)
 2将ASP.Net 2.0下的GridView导出为Excel.(转)
 3

Code Listing:

Image: Page Design
将ASP.Net 2.0下的GridView导出为Excel.(转)

Image : Sample in action

将ASP.Net 2.0下的GridView导出为Excel.(转)

Image: Export to Excel button is clicked
将ASP.Net 2.0下的GridView导出为Excel.(转)

Image: GridView contents exported to Excel

将ASP.Net 2.0下的GridView导出为Excel.(转)


 

ExcelExport.aspx




ExcelExport.aspx.cs 

 

 

Implementation Options:

 

通常情况,在输出函数中开发人员都会面临一个错误,典型的就是"RegisterForEventValidation can only be called during Render();"

 

访问者通常会在评论中提出一些好的建议.我特别要强调的是开发者需要重写VerifyRenderingInServerForm方法,该方法描述如下:

  • Step 1:实现导出功能的上述功能.
  • Step 2:重写一个VerifyRenderingInServerForm的空方法.
  • Step 3:修改ExportGridView函数,在绿色高亮代码部创建HtmlForm【原句为:The code highlighted in green creates and HtmlForm on the fly,在翻译HtmlForm on the fly时遇到了一些困难,on the fly未翻译,请各位高手指教】,在导出girdview之前,添加gridview 到新的form并且render它(取代原来的render实现)

 

 1将ASP.Net 2.0下的GridView导出为Excel.(转)private void ExportGridView()
 2将ASP.Net 2.0下的GridView导出为Excel.(转)
 3

 

这样实施有个优势,就是可将其设置为复用代码类库,不用每次去复写基类的方法.

 

Note to readers:

Thank you for your comments and feedback! Happy coding!!!

 

ASP.Net 2.0: Export GridView to Excel - Part II

该文中将会在导出Excel GridView引入Hyperlink,以至于需要使用更多的反射来重新设计原来的逻辑.


相关文章:

  • 2021-11-15
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2021-06-27
猜你喜欢
  • 2021-10-23
  • 2022-12-23
相关资源
相似解决方案