c# 页面转成word,execl

 1 System.IO.StringWriter SW = new System.IO.StringWriter();
 2             System.Web.UI.HtmlTextWriter HTW = new System.Web.UI.HtmlTextWriter(SW);
 3             Page.EnableViewState = false;
 4             Page.Form.RenderControl(HTW);
 5             //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以   
 6             string pageHtml = SW.ToString();
 7             int startIndex = pageHtml.IndexOf("<div id='PrintA'>") + 17;
 8             int endIndex = pageHtml.LastIndexOf("</div>");
 9             int lenth = endIndex - startIndex;
10             pageHtml = pageHtml.Substring(startIndex, lenth);
11             pageHtml = pageHtml.Remove(pageHtml.LastIndexOf("</div>"));
12             //移除页面自动添加的hidden input等   
13             Uri MyUrl = Request.UrlReferrer;
14             pageHtml = pageHtml.Replace("../images/title.jpg", "http://" + MyUrl.Authority + "/" + MyUrl.AbsolutePath.Split('/').GetValue(1) + "/images/title.jpg").Replace("textarea", "span").Replace("href=","name=");
15             //解决图片无法显示问题   
16             Response.Buffer = true;
17             Response.Clear();
18             Response.ClearContent();
19             Response.ClearHeaders();
20             //Response.ContentType = "Response.ContentType"; 
21             if (RadioButtonList1.SelectedIndex == 0)
22             {
23                 //防止出现乱码,加上这行可以防止在只有一行数据时出现乱码
24                 HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
25                 //Word
26                 HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(lbTitle.Text + ".doc", System.Text.Encoding.UTF8));
27 
28                 Response.ContentType = "application/ms-word";
29                 //Response.ContentType是输出流的 HTTP MIME 类型   
30                 //Response.ContentType      --- word文件   
31                 //application/vnd.ms-excel --- excel文件   
32                 //...   
33                // Response.Charset = "utf-8";
34                // Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
35                 //Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(lbTitle.Text, System.Text.Encoding.UTF8) + ".doc");
36             }
37             else 
38             {
39                 //防止出现乱码,加上这行可以防止在只有一行数据时出现乱码
40                 HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
41                 //Excel            
42                 HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(lbTitle.Text + ".xls", System.Text.Encoding.UTF8));
43                 HttpContext.Current.Response.ContentType = "application/ms-excel";
44 
45 
46                 //Response.ContentType = "application/ms-excel";
47                 ////Response.ContentType是输出流的 HTTP MIME 类型   
48                 ////Response.ContentType      --- word文件   
49                 ////application/vnd.ms-excel --- excel文件   
50                 ////...   
51                 //Response.Charset = "UTF-8";
52                 //Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
53                 //Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(lbTitle.Text, System.Text.Encoding.UTF8) + ".xls");
54             }
55            
56             //attachment --- 作为附件下载   
57             //inline --- 在线打开   
58             //filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)   
59             //进行进行编码,以解决文件名乱码的问题   
60            Response.Write(pageHtml.ToString());
61            // Response.Write("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns:m='http://schemas.microsoft.com/office/2004/12/omml' xmlns='http://www.w3.org/TR/REC-html40'><body>" + pageHtml.ToString()); 
62 
63             Response.Flush();
64             Response.Close();  
65         

 

posted on 2013-03-26 10:59  沐鈅  阅读(212)  评论(0)    收藏  举报