当前位置: 代码迷 >> ASP.NET >> 请问:asp.net 导出word文件思路
  详细解决方案

请问:asp.net 导出word文件思路

热度:4452   发布时间:2013-02-26 00:00:00.0
请教:asp.net 导出word文件思路
我要从数据库取数据,写到word文件里,请教高手指点.多谢了!

------解决方案--------------------------------------------------------
/// <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("\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(); 
}



------解决方案--------------------------------------------------------
可以先绑定datagrid再进行导出导出操作会方便些吧
  相关解决方案