public JsonResult CreateWordTmpl(string TmplCode) { foreach (var code in TmplCode.Split(',')) { var form = entities.Set <S_UI_Form>().Where(c => c.Code == code).OrderByDescending(c => c.VersionStartDate).FirstOrDefault(); if (form == null) { throw new BusinessException(string.Format("表单定义不存在!")); } if (string.IsNullOrWhiteSpace(form.Layout)) { throw new BusinessException(string.Format("表单定义尚未完成")); } var word = entities.Set <S_UI_Word>().Where(c => c.Code == code).OrderByDescending(c => c.VersionStartDate).FirstOrDefault(); //if (word != null) // throw new BusinessException(string.Format("Word导出定义已经存在:{0}", code)); if (word != null) { continue; } word = new S_UI_Word(); word.ID = FormulaHelper.CreateGuid(); word.Code = form.Code; word.Name = form.Name; word.ConnName = form.ConnName; word.TableNames = form.TableName; word.CategoryID = form.CategoryID; word.Description = "FormWord"; //根据此值判断是否由表单定义生成的Word导出 word.SQL = string.Format("select * from {0}", form.TableName); word.VersionNum = form.VersionNum; word.VersionStartDate = form.VersionStartDate; word.VersionDesc = form.VersionDesc; entities.Set <S_UI_Word>().Add(word); var doc = CreateDoc(code); string path = HttpContext.Server.MapPath("/WordTemplate"); int? versionNum = 1; if (form.VersionNum != null) { versionNum = form.VersionNum; } path += "\\" + form.Code + "_" + versionNum + ".docx"; doc.Save(path, Aspose.Words.SaveFormat.Docx); entities.SaveChanges(); } return(Json("")); }
public JsonResult SaveVersion(string wordID, string formData) { string newFormID = FormulaHelper.CreateGuid(); var wordInfo = entities.Set <S_UI_Word>().SingleOrDefault(c => c.ID == wordID); var newWordInfo = new S_UI_Word(); FormulaHelper.UpdateModel(newWordInfo, wordInfo); newWordInfo.ID = newFormID; newWordInfo.CreateTime = DateTime.Now; newWordInfo.ModifyTime = DateTime.Now; newWordInfo.VersionNum = entities.Set <S_UI_Word>().Count(c => c.Code == wordInfo.Code) + 1; //if (wordInfo.VersionNum != null) // newWordInfo.VersionNum = wordInfo.VersionNum + 1; //else // newWordInfo.VersionNum = entities.Set<S_UI_Word>().Count(c => c.Code == wordInfo.Code) + 1; var dic = JsonHelper.ToObject(formData); if (dic["VersionStartDate"].ToString() != "") { newWordInfo.VersionStartDate = DateTime.Parse(dic["VersionStartDate"].ToString()); wordInfo.VersionEndDate = newWordInfo.VersionStartDate; } else { throw new BusinessException("必须输入版本开始时间!"); } newWordInfo.VersionDesc = dic["VersionDesc"].ToString(); entities.Set <S_UI_Word>().Add(newWordInfo); entities.SaveChanges(); return(Json(newWordInfo)); }
public JsonResult CreateWordTmpl(string tmplCode) { var form = entities.Set <S_UI_Form>().SingleOrDefault(c => c.Code == tmplCode); if (form == null) { throw new BusinessException(string.Format("表单定义不存在:{0}", tmplCode)); } var word = entities.Set <S_UI_Word>().SingleOrDefault(c => c.Code == tmplCode); if (word != null) { throw new BusinessException(string.Format("Word导出定义已经存在:{0}", tmplCode)); } word = new S_UI_Word(); word.ID = FormulaHelper.CreateGuid(); word.Code = form.Code; word.Name = form.Name; word.ConnName = form.ConnName; word.CategoryID = form.CategoryID; word.Description = "FormWord"; //根据此值判断是否由表单定义生成的Word导出 word.SQL = string.Format("select * from {0}", form.TableName); entities.Set <S_UI_Word>().Add(word); #region 创建Word导出模板 var baseEntities = FormulaHelper.GetEntities <BaseEntities>(); var uiForm = baseEntities.Set <S_UI_Form>().SingleOrDefault(c => c.Code == tmplCode); var formItems = JsonHelper.ToObject <List <Dictionary <string, string> > >(uiForm.Items); Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc); string layout = form.Layout; Regex reg = new Regex("\\{[^\\{]*\\}", RegexOptions.IgnorePatternWhitespace); #region 处理layout中的子表 layout = reg.Replace(layout, (Match m) => { string text = m.Value.Trim('{', '}', ' '); var item = formItems.SingleOrDefault(c => c["Name"] == text); if (item == null) { return(m.Value); } else if (item["ItemType"] == "AuditSign") { string tmplName = "auditSignTmpl"; string signTitle = "签字"; string width = "100%"; if (item.ContainsKey("Settings") && item["Settings"] != "") { var _dic = JsonHelper.ToObject <Dictionary <string, string> >(item["Settings"]); if (_dic.ContainsKey("tmplName") && _dic["tmplName"] != "") { tmplName = _dic["tmplName"]; } if (_dic.ContainsKey("signTitle") && _dic["signTitle"] != "") { signTitle = _dic["signTitle"]; } if (_dic.ContainsKey("width") && _dic["width"] != "") { width = _dic["width"]; } } if (tmplName == "auditSignTmpl") { StringBuilder sb = new StringBuilder(); sb.Append("{TableStart:" + item["Code"] + "}"); sb.Append("<span>意见:{Field:SignComment}</span>"); sb.Append("<div>" + signTitle + ":{Field:ExecUserID}日期:{Field:SignTime}</div>"); sb.Append("{TableEnd:" + item["Code"] + "}"); return(sb.ToString()); } else if (tmplName == "auditSignSingleTmpl") { StringBuilder sb = new StringBuilder(); sb.AppendLine("<table style=\"width:" + width + "\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">"); sb.AppendLine("<tr><td>"); sb.AppendLine(signTitle + "{" + item["Code"] + ":ExecUserID}"); sb.AppendLine("</td></tr><tr><td>"); sb.AppendLine("日期:{" + item["Code"] + ":SignTime}"); sb.AppendLine("</td></tr></table>"); return(sb.ToString()); } else { return(""); } } else if (item["ItemType"] == "SubTable") { var _dic = JsonHelper.ToObject(item["Settings"]); var subItemList = JsonHelper.ToObject <List <FormItem> >(_dic["listData"].ToString()); if (subItemList.Count == 0) { return(m.Value); } StringBuilder sb = new StringBuilder(); sb.AppendLine("<table style=\"width:100%\" bordercolor=\"#000000\" cellspacing=\"0\" cellpadding=\"2\" border=\"1\">"); sb.AppendLine("<tr>"); foreach (var subItem in subItemList) { sb.AppendFormat("<td width=\"{1}\">{0}</td>", subItem.Name, subItem.width); } sb.AppendLine("</tr>"); sb.AppendLine("<tr>"); for (int i = 0; i < subItemList.Count; i++) { var subItem = subItemList[i]; sb.Append("<td>"); if (i == 0)//子表开始 { sb.Append("{TableStart:" + item["Code"] + "}"); } sb.Append("{Field:" + subItem.Code + "}"); if (i == subItemList.Count - 1) //子表结束 { sb.Append("{TableEnd:" + item["Code"] + "}"); } sb.Append("</td>"); } sb.AppendLine("</tr>"); sb.AppendLine("</table>"); return(sb.ToString()); } else { return(m.Value); } }); #endregion layout = reg.Replace(layout, (match) => { if (match.Value.Contains("TableStart:") || match.Value.Contains("TableEnd:")) { return(match.Value); } else { return("<span>" + match.Value + "</span>"); } }); builder.InsertHtml(layout); doc.Range.Replace(reg, new ReplaceToField(formItems), false); string path = HttpContext.Server.MapPath("/WordTemplate"); path += "\\" + tmplCode + ".docx"; doc.Save(path, Aspose.Words.SaveFormat.Docx); #endregion entities.SaveChanges(); return(Json("")); }