当前位置:编程学习 > asp >>

报表导出模块实现

表格导出的方法至少有三种,第一种是利用Excel支持HTML,直接利用控件的RenderControl()方法,将控件的HTML代码以Excel类型输出到客户端;第二种利用Excel的com接口库来完成;第三种利用数据访问对象来操作Excel文件,以达到导出Excel的效果。
以下是第一種的方法。
[csharp]
//表格输出 
 
 public void Report(System.WEB.UI.Pagepage,Repeater rpt,string title,string type) 
 
 { 
 
    //清除缓冲区内的所有内容输出 
 
    HttpContext.Current.Response.Clear(参数){……}; 
 
    //设置输出流的HTTP字符 
 
        HttpContext.Current.Response.Charset ="uft-8"; 
 
        //将HTTP头添加到输出流 
 
    HttpContext.Current.Response.AddHeader("Content-Disposition", 
 
    "attachment;filename="+HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddHHMMss")+        ".xls",Encodin g.UTF8).ToString(参数){……}); 
 
    //设置输出流的HTTPMIME类型 
 
    HttpContext.Current.Response.ContentType ="application/ms-excel"; 
 
    //设置当前页请求结束时该页是否保持其视图状态以及它包含的任何服务器控件的视图状态 
 
    page.EnableViewState = false; 
 
    System.IO.StringWriteroStringWriter = new System.IO.StringWriter(参数){……}; 
 
    System.WEB.UI.HtmlTextWriter oHtmlTextWriter=  
 
    new System.WEB.UI.HtmlTextWriter(oStringWriter); 
 
    //将服务器控件的内容输出到提供的HtmlTextWriter对象中 
 
    rpt.RenderControl(oHtmlTextWriter); 
 
    //将信息写入到HTTP响应输出流 
 
    HttpContext.Current.Response.Write("<html><head><metahttp-equiv=Content-Type        content=\"text/html;cha  rset=utf-8\"></head><body><table><b>"+标题   +"</b></td></tr></table><table>"+"<thead>表头</thead><tbody>"); 
 
    string temp = oStringWriter.ToString(参数){……}; 
 
    HttpContext.Current.Response.Write(temp); 
 
    HttpContext.Current.Response.Write("</tbody></table></body></html>"); 
 
    //输出到客户端 
 
    HttpContext.Current.Response.End(){……}; 
 

前台asp.net页面中涉及的html:
<table><thead>表头</thead><tbody>Repeater控件</tbody></table>
 

 

摘自 chenpeggy

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,