//按鈕生成事件 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(); }