我要把数据表中的一些信息导出到Word文档去。
生成Word的代码已经OK了,现在的问题就是无法将Word的内容下载到客户端去。
生成的Word文件名为word1.doc。以下是我用的下载到客户端的代码:
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=word1.doc");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-word";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Response.Write(oStringWriter.ToString());
Response.End(); 然而在各个客户端执行后却无法下载下来,word1.doc的内容都显示在服务器端了。谁能有好办法?谢谢!
生成Word的代码已经OK了,现在的问题就是无法将Word的内容下载到客户端去。
生成的Word文件名为word1.doc。以下是我用的下载到客户端的代码:
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=word1.doc");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-word";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Response.Write(oStringWriter.ToString());
Response.End(); 然而在各个客户端执行后却无法下载下来,word1.doc的内容都显示在服务器端了。谁能有好办法?谢谢!
用LinkButton或《a》都可以
{
string Pa=page.Server.MapPath(@"Template/"+File+"");
page.Response.AppendHeader("Content-Disposition","attachment;filename="+File+"");
page.Response.ContentType ="application/ms-word";
page.Response.WriteFile(Pa);
page.Response.End();
}
或通过Hyperlink打开word文件
然后获取文件地址调用下面方法 下载 #region 下载服务器上的文件
/// <summary>
/// 下载服务器上的文件
/// </summary>
/// <param name="PageResponse">程序中可以设置参数:HttpResponse ht=Page.Response;</param>
/// <param name="serverPath">服务器上的文件路径</param>
public void DownloadFile(HttpResponse response, string serverPath)
{
FileStream fs = null;
try
{
fs = File.OpenRead(serverPath);
byte[] buffer = new byte[1024];
long count = 1024;
response.Buffer = true;
response.AddHeader("Connection", "Keep-Alive");
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(serverPath));//下载时要保存的默认文件名
response.AddHeader("Content-Length", fs.Length.ToString());
while (count == 1024)
{
count = fs.Read(buffer, 0, 1024);
response.BinaryWrite(buffer);
}
}
catch
{
}
finally
{
fs.Close();
}
}
#endregion
就会直接在打开页面里打开文档
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=word1.doc"); 2. 格式的问题
你这样输出的doc下载下来之后可能无法打开
生成word方法请查看
http://support.microsoft.com/kb/316384
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=word1.doc"); 2. 格式的问题
你这样输出的doc下载下来之后可能无法打开
生成word方法请查看
http://support.microsoft.com/kb/316384
放到按钮事件里试下
我以前做生成excel的没出现过你这情况啊
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
//这里的filename可以输出时自定义,不一定用原来的.
Response.TransmitFile(filePath_ystp);
Response.Flush();
Response.Close();用这个试试
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();这个我用过了 可以的
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();