php 2007-12-17 17:11
ASP.NET2.0导出页面对象到WORD,EXCEL等文件
这段时间在做一个.NET的网站,收获良多,其中大多来自网友们的原创,学习之余,汗颜发点小心得与各位探讨.由于做的项目很多地方要求将一些页面导出为WORD,或者EXCEL文件.于是网上找了很多方法,总结了一些网友的经验,写了一个通用的页面导出函数(vb.net).通过制定pageobject可以很精细的控制导出整个页面或者一部分.
[color=#000000]#[color=#0000ff]Region[/color] [color=#ff00ff]"导出页面对象到excel和word等文件"[/color]
[color=#0000cc]'[/color][color=#0000cc]'[/color][color=#0000cc]'[/color] [color=#0000cc]<[/color]summary[color=#0000cc]>[/color]
[color=#0000cc]'[/color][color=#0000cc]'[/color][color=#0000cc]'[/color] 导出到页面对象到excel和word等文件
[color=#0000cc]'[/color][color=#0000cc]'[/color][color=#0000cc]'[/color] [color=#0000cc]<[/color][color=#0000cc]/[/color]summary[color=#0000cc]>[/color]
[color=#0000cc]'[/color][color=#0000cc]'[/color][color=#0000cc]'[/color] [color=#0000cc]<[/color]param name[color=#0000cc]=[/color][color=#ff00ff]"fileType"[/color][color=#0000cc]>[/color]导出类型(excel,word ,txt,html 或其他浏览器可直接支持文档类型)[color=#0000cc]<[/color][color=#0000cc]/[/color]param[color=#0000cc]>[/color]
[color=#0000cc]'[/color][color=#0000cc]'[/color][color=#0000cc]'[/color] [color=#0000cc]<[/color]param name[color=#0000cc]=[/color][color=#ff00ff]"fileName"[/color][color=#0000cc]>[/color]要导出的文件名[color=#0000cc]([/color]包括扩展名)[color=#0000cc]<[/color][color=#0000cc]/[/color]param[color=#0000cc]>[/color]
[color=#0000cc]'[/color][color=#0000cc]'[/color][color=#0000cc]'[/color] [color=#0000cc]<[/color]param name[color=#0000cc]=[/color][color=#ff00ff]"pageObject"[/color][color=#0000cc]>[/color]要导出的页面对象[color=#0000cc]([/color][color=#0000cc]'[/color]Me表示输出本页所有对象,你也可以绑定gridview[color=#0000cc],[/color]table[color=#0000cc],[/color]td[color=#0000cc],[/color][color=#ff0000]form[/color][color=#0000cc],[/color]或其他支持obj[color=#0000cc].[/color]RenderControl[color=#0000cc]([/color][color=#0000cc])[/color]属性的控件[color=#0000cc])[/color][color=#0000cc]<[/color][color=#0000cc]/[/color]param[color=#0000cc]>[/color]
[color=#0000cc]'[/color][color=#0000cc]'[/color][color=#0000cc]'[/color] [color=#0000cc]<[/color]remarks[color=#0000cc]>[/color][color=#0000cc]<[/color][color=#0000cc]/[/color]remarks[color=#0000cc]>[/color]
[color=#0000ff]Public[/color] [color=#0000ff]Sub[/color] exportFile[color=#0000cc]([/color][color=#0000ff]ByVal[/color] fileType [color=#0000ff]As[/color] [color=#0000ff]String[/color][color=#0000cc],[/color] [color=#0000ff]ByVal[/color] fileName [color=#0000ff]As[/color] [color=#0000ff]String[/color][color=#0000cc],[/color] [color=#0000ff]ByVal[/color] pageObject [color=#0000ff]As[/color] Object[color=#0000cc])[/color]
HttpContext[color=#0000cc].[/color]Current[color=#0000cc].[/color][color=#ff0000]Response[/color][color=#0000cc].[/color]Clear[color=#0000cc]([/color][color=#0000cc])[/color]
HttpContext[color=#0000cc].[/color]Current[color=#0000cc].[/color][color=#ff0000]Response[/color][color=#0000cc].[/color]Buffer [color=#0000cc]=[/color] [color=#0000ff]True[/color]
HttpContext[color=#0000cc].[/color]Current[color=#0000cc].[/color][color=#ff0000]Response[/color][color=#0000cc].[/color]Charset [color=#0000cc]=[/color] [color=#ff00ff]"utf-8"[/color]
[color=#0000cc]'[/color]下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
[color=#0000cc]'[/color]filename[color=#0000cc]=[/color]FileFlow[color=#0000cc].[/color]xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:[color=#0000cc].[/color]doc [color=#0000cc].[/color]xls [color=#0000cc].[/color]txt [color=#0000cc].[/color]htm
HttpContext[color=#0000cc].[/color]Current[color=#0000cc].[/color][color=#ff0000]Response[/color][color=#0000cc].[/color]AppendHeader[color=#0000cc]([/color][color=#ff00ff]"Content-Disposition"[/color][color=#0000cc],[/color] [color=#ff00ff]"attachment;filename="[/color] [color=#0000cc]&[/color] fileName[color=#0000cc])[/color]
HttpContext[color=#0000cc].[/color]Current[color=#0000cc].[/color][color=#ff0000]Response[/color][color=#0000cc].[/color]ContentEncoding [color=#0000cc]=[/color] System[color=#0000cc].[/color]Text[color=#0000cc].[/color]Encoding[color=#0000cc].[/color]GetEncoding[color=#0000cc]([/color][color=#ff00ff]"utf-8"[/color][color=#0000cc])[/color]
[color=#0000cc]'[/color][color=#ff0000]Response[/color][color=#0000cc].[/color]ContentType指定文件类型 可以为application[color=#0000cc]/[/color]ms[color=#0000cc]-[/color]excel application[color=#0000cc]/[/color]ms[color=#0000cc]-[/color]word application[color=#0000cc]/[/color]ms[color=#0000cc]-[/color]txt application[color=#0000cc]/[/color]ms[color=#0000cc]-[/color]html 或其他浏览器可直接支持文档
HttpContext[color=#0000cc].[/color]Current[color=#0000cc].[/color][color=#ff0000]Response[/color][color=#0000cc].[/color]ContentType [color=#0000cc]=[/color] [color=#ff00ff]"application/ms-"[/color] [color=#0000cc]&[/color] fileType
pageObject[color=#0000cc].[/color]EnableViewState [color=#0000cc]=[/color] [color=#0000ff]False[/color]
[color=#0000cc]'[/color]定义一个输入流
[color=#0000ff]Dim[/color] oStringWriter [color=#0000ff]As[/color] StringWriter [color=#0000cc]=[/color] [color=#0000ff]New[/color] System[color=#0000cc].[/color]IO[color=#0000cc].[/color]StringWriter[color=#0000cc]([/color][color=#0000cc])[/color]
[color=#0000ff]Dim[/color] oHtmlTextWriter [color=#0000ff]As[/color] HtmlTextWriter [color=#0000cc]=[/color] [color=#0000ff]New[/color] System[color=#0000cc].[/color]Web[color=#0000cc].[/color]UI[color=#0000cc].[/color]HtmlTextWriter[color=#0000cc]([/color]oStringWriter[color=#0000cc])[/color]
[color=#0000cc]'[/color]将目标数据绑定到输入流输出
pageObject[color=#0000cc].[/color]RenderControl[color=#0000cc]([/color]oHtmlTextWriter[color=#0000cc])[/color]
HttpContext[color=#0000cc].[/color]Current[color=#0000cc].[/color][color=#ff0000]Response[/color][color=#0000cc].[/color]Write[color=#0000cc]([/color]oStringWriter[color=#0000cc].[/color]ToString[color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc])[/color]
HttpContext[color=#0000cc].[/color]Current[color=#0000cc].[/color][color=#ff0000]Response[/color][color=#0000cc].[/color][color=#0000ff]End[/color][color=#0000cc]([/color][color=#0000cc])[/color]
[color=#0000ff]End[/color] [color=#0000ff]Sub[/color]
#[color=#0000ff]End[/color] [color=#0000ff]Region[/color]
[/color]
下面是调用示例:在要导出的页面调用exportfile方法,将页面中ID=Table1的table导出到word文件11.doc中
[color=#000000][color=#0000ff]Protected[/color] [color=#0000ff]Sub[/color] Button2_Click[color=#0000cc]([/color][color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] Object[color=#0000cc],[/color] [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System[color=#0000cc].[/color]EventArgs[color=#0000cc])[/color] [color=#0000ff]Handles[/color] Button2[color=#0000cc].[/color]Click
ctrl[color=#0000cc].[/color]exportFile[color=#0000cc]([/color][color=#ff00ff]"word"[/color][color=#0000cc],[/color] [color=#ff00ff]"11.doc"[/color][color=#0000cc],[/color] Me[color=#0000cc].[/color]Table1[color=#0000cc])[/color]
[color=#0000ff]End[/color] [color=#0000ff]Sub[/color]
[color=#0000ff]Public[/color] [color=#0000ff]Overrides[/color] [color=#0000ff]Sub[/color] verifyrenderinginserverform[color=#0000cc]([/color][color=#0000ff]ByVal[/color] control [color=#0000ff]As[/color] Control[color=#0000cc])[/color]
[color=#0000ff]End[/color] [color=#0000ff]Sub[/color][/color]
其中重载了verifyrenderinginserverform这个函数(注意函数体是空的),否则可能出现错误提示(原因未知,望大牛指点)。
[color=red]类型“XXXXX”的控件“xxxxxxx”必须放在具有 runat=server 的窗体标记内.[/color]
[[i] 本帖最后由 php 于 2007-12-17 17:13 编辑 [/i]]