Html静态页面获取

1.静态页面的获取:

 1 /// <summary>
 2 /// 获取weburl输出内容
 3 /// </summary>
 4 /// <param name="url">weburl</param>
 5 /// <returns>输出内容</returns>
 6 public static string GetPage(string url)
 7 {
 8 WebResponse result = null;
 9 try
10 {
11 WebRequest req = WebRequest.Create(new Uri(url));
12 result = req.GetResponse();
13 
14 var receivedStream = result.GetResponseStream();
15 var sr = new System.IO.StreamReader(receivedStream,Encoding.GetEncoding("gb2312"));// GetEncoding( GetContentType(result.ContentType).FirstOrDefault().Key)
16 var str="";
17 while (sr.Peek() != -1)
18 str += sr.ReadLine();
19 
20 return str;
21 }
22 catch (Exception ex)
23 {
24 return "";
25 }
26 finally
27 {
28 //ensure that network resources are not wasted
29 if (result != null)
30 result.Close();
31 }
32 }

2.以保存文件方式输出字符串:

1 System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=result.doc");
2 System.Web.HttpContext.Current.Response.ContentType = "application/ms-word";
3 System.Web.HttpContext.Current.Response.Charset = "utf-8";
4 System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
5 Response.Write(str);
6 Response.End();

3.Word文档创建

 1 using MSWord = Microsoft.Office.Interop.Word;           
 2             object path;                      //声明文件路径变量
 3             string wordstr;                   //声明word文档内容
 4             MSWord.Application wordApp;       //声明word应用程序变量
 5             MSWord.Document worddoc;          //声明word文档变量    
 6 
 7             //初始化变量
 8             object Nothing = Missing.Value;                       //COM调用时用于占位
 9             object format = MSWord.WdSaveFormat.wdFormatDocument; //Word文档的保存格式
10             wordApp = new MSWord.ApplicationClass();              //声明一个wordAPP对象
11             worddoc = wordApp.Documents.Add(ref Nothing, ref Nothing,
12                 ref Nothing, ref Nothing);
13 
14             //向文档中写入内容
15             worddoc.Paragraphs.Last.Range.Text = "wordstr";
16 
17             //保存文档
18             path = Server.MapPath("~/htmlToword/test.doc");          //设置文件保存路劲
19             worddoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing,
20                 ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
21                 ref Nothing, ref Nothing, ref Nothing, ref Nothing);
22 
23             //关闭文档
24             worddoc.Close(ref Nothing, ref Nothing, ref Nothing);  //关闭worddoc文档对象
25             wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);   //关闭wordApp组对象    

 

转载于:https://www.cnblogs.com/Kuleft/p/5000906.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值