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组对象    

 

posted @ 2015-11-27 15:52  kuleft  阅读(605)  评论(0编辑  收藏  举报