asp中如何将数据库的内容导出到word?
2个回答
2013-11-09
展开全部
// <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();
}
// 获取指定网页内容
// </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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网上搜pageoffice,有很多它的示例代码的,你参考下啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询