• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#页面转成word,execl

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 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         

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap