我要把数据表中的一些信息导出到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的内容都显示在服务器端了。谁能有好办法?谢谢!

解决方案 »

  1.   

    只要有文件的地址,直接连接就可以下载了
    用LinkButton或《a》都可以
      

  2.   

      HttpContext.Current.Response.WriteFile(path);换这个试试 .不知道是不是这个问题
      

  3.   

    public void FileToWord(System.Web.UI.Page page,string strP,string File)
    {
    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文件
      

  4.   

    先把文件生成到服务器上一个目录下
    然后获取文件地址调用下面方法 下载    #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
      

  5.   

    你把Response.AppendHeader("Content-Disposition","attachment;filename=word1.doc"); 去掉
    就会直接在打开页面里打开文档
      

  6.   

    1. 下载的问题
    Response.ContentType = "application/octet-stream";
    Response.AppendHeader("Content-Disposition","attachment;filename=word1.doc"); 2. 格式的问题
    你这样输出的doc下载下来之后可能无法打开
    生成word方法请查看
    http://support.microsoft.com/kb/316384
      

  7.   

    1. 下载的问题 
    Response.ContentType = "application/octet-stream"; 
    Response.AppendHeader("Content-Disposition","attachment;filename=word1.doc"); 2. 格式的问题 
    你这样输出的doc下载下来之后可能无法打开 
    生成word方法请查看 
    http://support.microsoft.com/kb/316384 
      

  8.   

    13楼真逗
    放到按钮事件里试下
    我以前做生成excel的没出现过你这情况啊
      

  9.   

       Response.Clear();
            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();用这个试试
      

  10.   

    System.IO.FileInfo file = new System.IO.FileInfo(这里填写路径);
    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();这个我用过了 可以的
      

  11.   

    嗨,搞定了,你道怎地?原来是页面中加了Ajax(防止闪烁)代码所致,去掉后就可以了。But thank you all the same!
      

  12.   

    System.IO.FileInfo file = new System.IO.FileInfo(这里填写路径); 
    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();