asp.net页面内容导出word_廖锂_新浪博客

/// <summary>
  /// 获取指定远程网页内容
  /// </summary>
  /// <param name="strUrl">所要查找的远程网页地址</param>
  /// <param name="timeout">超时时长设置,一般设置为8000</param>
  /// <param name="enterTy
/// <summary>
  /// 获取指定远程网页内容
  /// </summary>
  /// <param name="strUrl">所要查找的远程网页地址</param>
  /// <param name="timeout">超时时长设置,一般设置为8000</param>
  /// <param name="enterType">是否输出换行符,0不输出,1输出文本框换行</param>
  /// <param name="EnCodeType">编码方式</param>
  /// <returns></returns>
  private string GetRequestString( int timeout, int enterType, Encoding EnCodeType)

  {
   string strResult;
   try

   {


    string url = HttpContext.Current.Request.Url.AbsoluteUri;
    HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
    myReq.Timeout = timeout;
    //myReq.Method="POST";
    myReq.ContentType = "application/x-www-form-urlencoded";
    //Stream myStream = myReq.GetRequestStream();
    WebResponse HttpWResp = myReq.GetResponse();
    Stream myStream = HttpWResp.GetResponseStream() ;
    StreamReader sr = new StreamReader(myStream , EnCodeType);
    StringBuilder strBuilder = new StringBuilder();
    while (-1 != sr.Peek())

    {
     strBuilder.Append(sr.ReadLine());
     if(enterType==1)
     {
      strBuilder.Append(" ");
     }
    }
    strResult = strBuilder.ToString();
   }
   catch(Exception ex)

   {
    strResult = "请求错误:" + ex.Message;
   }

   return strResult ;
  }
   //调用此方法导出为word文档,传入参数为this.Page
  public void getWordFile(System.Web.UI.Page Page)
  {
 
   HttpResponse resp=Page.Response;
   resp.Clear();
   resp.Buffer= true;
   resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
   resp.AddHeader("Content-disposition","attachment; filename="+HttpUtility.UrlEncode("1.doc",Encoding.UTF8 ));    
   string context=GetRequestString(8000,1,Encoding.GetEncoding("GB2312"));//调用GetRequestString方法获取指定远程网页内容
   resp.ContentType="application/msword";
   resp.Write(context);
   resp.End();
  }

已投稿到:
郑重声明:资讯 【asp.net页面内容导出word_廖锂_新浪博客】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——