Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)delvpublished in(发表于) 2014/1/24 9:08:23 asp.net页面导出为Word文档_[Asp.Net教程]
/// /// 获取指定远程网页内容 /// /// 所要查找的远程网页地址 /// 超时时长设置,一般设置为8000 /// 是否输出换行符,0不输出,1输出文本框换行 /// 编码方式 /// 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("\r\n"); } } 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(); }来源:CSDN
赞