.NET中如何实现在页面中的word文档通过Button按钮下载?

解决方案 »

  1.   

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
             <div>
                <table align="center" cellpadding="0" cellspacing="0" border="0">
                    <tr>
                        <td align="center">
                            <asp:Button ID="cmdOpen" runat="server" Text="在线打开" CommandName="open" OnCommand="Button_Click" />&nbsp;
                            <asp:Button ID="cmdSave" runat="server" Text="本地保存" CommandName="save" OnCommand="Button_Click" />
                            <asp:DropDownList ID="listType" runat="server" >
                                <asp:ListItem Value="excel">Excel</asp:ListItem>
                                <asp:ListItem Value="word">Word</asp:ListItem>
                             </asp:DropDownList><br />
                            <br />
                            数据源:<br />
                            <br />
                            <asp:GridView ID="myGW" runat="server" >
                            </asp:GridView>
                        </td>
                    </tr>
                </table>
            </div>
        </form>
    </body>
    </html>using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillGridView();
        }
        private void OutPut(string fileType, string strType)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", fileType);
            Response.ContentType = strType;
            this.EnableViewState = false;
            System.IO.StringWriter swOut = new System.IO.StringWriter();
            HtmlTextWriter hTw = new HtmlTextWriter(swOut);
            myGW.RenderControl(hTw);
            Response.Write(swOut.ToString());
            Response.End();
        }
        protected void Button_Click(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "save":
                    switch (listType.SelectedValue)
                    {
                     case "excel":
                            OutPut("attachment;filename=out.xls", "application/ms-excel");
                            break;
                        case "word":
                            OutPut("attachment;filename=out.doc", "application/ms-word");
                            break;
                    }
                    break;
                case "open":
                    switch (listType.SelectedValue)
                    {
                        case "excel":
                            OutPut("online;filename=out.xls", "application/ms-excel");
                            break;
                        case "word":
                            OutPut("online;filename=out.doc", "application/ms-word");
                            break;
                    }
                    break;
            }
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
        }
        private void FillGridView()
        {
            string strConn = "server=.;database=sbskweb;user id=sa;password=sa";
            SqlConnection conn = new SqlConnection(strConn);
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            string strSql = "SELECT top 10 序号=Rowid,父节点=FatherID,模块名称=Categoryname FROM Web_Category";
            comm.CommandText = strSql;
            conn.Open();
            this.myGW.DataSource = comm.ExecuteReader();
            this.myGW.DataBind();
            conn.Close();
        }
    }
      

  2.   

    string fileName = "";//客户端保存的文件名
                string filePath = Server.MapPath("");//路径            FileInfo fileInfo = new FileInfo(filePath);
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.ContentType = "application/octet-stream";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.WriteFile(fileInfo.FullName);
                Response.Flush();
                Response.End();
      

  3.   

    <!--
    浏览器默认是可以直接打开图片和文本文件的,flash等媒体文件是需要浏览器插件机制的。
    通过学习http协议后,有了一定理解。对于word文档等文件,默认请求时会弹出“文件另存为”的下载框。
    当在IIS中配置了MIME类型关系映射后,可以关联某个文件类型与程序软件,访问该资源后会默认使用该软件打开。
    例如,可是设置关联“*.abs”文件为媒体,访问该资源的时候windows会默认使用meadia player打开。
    设置“*.doc”文件与“msword”程序相关联,访问该类型的资源时,就会默认使用msword打开
    在IIS中可以直接配置,使用Tomcat服务器的话,在其安装目录下的web.xml文件中配置
    -->
      

  4.   

    <button onclick="location='test.doc';" value="下载文档" />
      

  5.   

    <button onclick="location='test.doc';" value="下载文档" />这种很直接