第一种 通过网页直接生成word下载 Response.AddHeader("Content-Disposition", "p_w_upload;filename=" + HttpUtility.UrlEncode("word名称.doc", Encoding.UTF8)); Response.ContentType = "application/ms-word"; EnableViewState = false; var tw = new StringWriter(); var hw = new HtmlTextWriter(tw); tb1.RenderControl(hw);//tb1为网页table(div) 的id Response.Write(tw.ToString()); Response.End(); 第二种 word模板直接下载 string sPath = System.IO.Path.GetDirectoryName(this.Page.Request.PhysicalPath); string a =@"\wendang\销售合同.doc"; string tmpFileName = sPath + a;//@"E:\广大连锁医药\YK_ERP_HNCC\ERPCC\Order\wendang\C++程序设计04737试题及答案2008~2012.doc"; #region //方法一 FileInfo tmpFI = new FileInfo(tmpFileName); Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.AppendHeader("Content-Disposition", "p_w_upload;filename=" + HttpUtility.UrlEncode(Path.GetFileName(tmpFileName), System.Text.Encoding.UTF8)); Response.AppendHeader("Content-Length", tmpFI.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(tmpFileName); Response.Flush(); Response.End(); //方法二 Response.ContentType = "Application/msword"; Response.WriteFile(tmpFileName); Response.End(); #endregion
转载于:https://blog.51cto.com/smileface/1714649