第一步:项目引用 Aspose.Words.dll
第二步:调用下列接口
public void WorldSeve(Model.Work.wfPropertyDecoReform reformModel, int CreateById, Model.Person.pmAccount account) { #region 1.初始化模板变量 string tmppath = System.Web.HttpContext.Current.Server.MapPath("违章整改模板.docx");//本地world 模板 Document doc = new Document(tmppath); //载入模板 DocumentBuilder builder = new DocumentBuilder(doc); #endregion #region 处理其他标签 doc.Range.Bookmarks["name"].Text =CommonInfo.pmUserInfoName(CreateById);-- 这里的 name 是你在world 模板上定义的标签名称 doc.Range.Bookmarks["room"].Text = reformModel.RoomName.ToString(); doc.Range.Bookmarks["reason"].Text = reformModel.Reason + ""; doc.Range.Bookmarks["days"].Text = reformModel.ReformDates + ""; doc.Range.Bookmarks["y1"].Text = Convert.ToDateTime(reformModel.PutDate).ToString("yyyy"); doc.Range.Bookmarks["m1"].Text = Convert.ToDateTime(reformModel.PutDate).ToString("MM"); doc.Range.Bookmarks["d1"].Text = Convert.ToDateTime(reformModel.PutDate).ToString("dd"); doc.Range.Bookmarks["y2"].Text = Convert.ToDateTime(reformModel.CreateDate).ToString("yyyy"); doc.Range.Bookmarks["m2"].Text = Convert.ToDateTime(reformModel.CreateDate).ToString("MM"); doc.Range.Bookmarks["d2"].Text = Convert.ToDateTime(reformModel.CreateDate).ToString("dd"); string fpath = System.Web.HttpContext.Current.Server.MapPath("doc"); string fname = DateTime.Now.ToString("yyyyMMddHHmmss") + "违章管理.doc"; if (!System.IO.Directory.Exists(fpath)) System.IO.Directory.CreateDirectory(fpath); if (System.IO.File.Exists(fpath + "/" + fname)) System.IO.File.Delete(fpath + "/" + fname); doc.Save(fpath + "/" + fname); //System.IO.FileInfo file = new System.IO.FileInfo(fpath + "/" + fname); //System.Web.HttpContext.Current.Response.Clear(); //System.Web.HttpContext.Current.Response.Charset = "utf-8"; //System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; //System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpContext.Current.Server.UrlEncode(fname)); //System.Web.HttpContext.Current.Response.AddHeader("Conten-Length", file.Length.ToString()); //System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream"; //System.Web.HttpContext.Current.Response.WriteFile(file.FullName); //System.Web.HttpContext.Current.Response.End(); Model.Work.wfFiles fiel = new Model.Work.wfFiles(); fiel.FileName = "违章整改模板.docx"; fiel.FilePath = fname; fiel.BelongId = reformModel.Id; fiel.BelongTable = "wfPropertyDecoReform"; fiel.Extend = ".doc"; fiel.FileType = 102; fiel.State = (int)StateEnum.State.Enable; fiel.RoleId = Convert.ToInt32(CommonInfo.UserSysId(account.Id)); fiel.CompanyId = Convert.ToInt32(CommonInfo.pmCompanyId(account.Id)); fiel.CreateBy = account.AccountName; fiel.CreateById = account.Id; fiel.CreateDate = DateTime.Now; IBLL.Factory.wfFilesCreate().Add(fiel); #endregion }