//按钮生成事件 protected void Button1_Click(object sender, EventArgs e) { string number = "2017101600001";//编号 //number是对应word中的number标签 string createdate = "2017-10-16";//日期 //createdate是对应word中的number标签 string title = "文章标题文章标题";//标题 //title 是对应word中的title 标签 string username = "管理员姓名";//名称 //username 是对应word中的username 标签 OutWord(number, createdate, title, username); //生成事件 } //生成word文档 public void OutWord(string number, string createdate, string title, string username) { //定义word文档,含标签 string docmb = System.Web.HttpContext.Current.Server.MapPath("/upload/template.doc"); Document doc = new Document(docmb); //引用Aspose.Words.dll第三方类 doc.Range.Bookmarks["number"].Text = number; //写入标签中的值 doc.Range.Bookmarks["createdate"].Text = createdate; doc.Range.Bookmarks["title"].Text = title; doc.Range.Bookmarks["username"].Text = username; string Create = DateTime.Now.ToString("yyyyMMddhhmmss"); string savepath = System.Web.HttpContext.Current.Server.MapPath("/upload/网上信息公开申请表.doc");//新文档路径 doc.Save(savepath);//保存文件 FileDown(savepath);//下载 } /// <summary> /// 文件下载 /// </summary> /// <param name="filepath"></param> public void FileDown(string filepath) { FileInfo fi = new FileInfo(filepath); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Buffer = false; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filepath), System.Text.Encoding.UTF8)); //输入文档流 HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString()); HttpContext.Current.Response.ContentType = "application/octet-stream"; //定义格式 HttpContext.Current.Response.WriteFile(filepath); //下载文件 HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); }