public static void AppendCategoryText(string filepath, string categoryName) { try { Aspose.Words.DocumentBuilder db = new Aspose.Words.DocumentBuilder(destdoc); db.MoveToDocumentEnd(); string categoryText = "<div style='font-weight:bold;text-decoration:underline;width:800px;text-align: center;'>Category " + categoryName + "</div>"; if (categoryName == "F") { categoryText = "<div style='font-weight:bold;text-decoration:underline;width:800px;text-align: center;'>Category " + categoryName + " [Independent Unit]</div>"; } db.InsertBreak(BreakType.PageBreak); db.InsertHtml(categoryText, false); } catch (Exception ex) { ex = new Exception("Report -> AppendCategoryText: \r\n\r\n", ex); LogManager.WriteErrorLog(ex); destdoc = null; throw ex; } GC.Collect(); }
public static void AppendJuryTabularDetail(EnumerableRowCollection <DataRow> dataRows, string filepath, string productName) { try { Aspose.Words.DocumentBuilder db = new Aspose.Words.DocumentBuilder(destdoc); db.MoveToDocumentEnd(); StringBuilder sb = new StringBuilder(); sb.Append("<html><head><style>table, th, td {border: 1px solid black;border-collapse: collapse;text-align:center;font-size:15px;}</style></head><body>"); sb.Append("<table cellpadding='2' style='color:black;width:100%'><tbody>"); sb.AppendFormat("<tr><td style='font-weight:bold;font-size:17px;' colspan='3'>{0}</td></tr>", productName); sb.AppendFormat("<tr><td style='font-weight:bold;'>Sr. No.</td><td style='font-weight:bold;'>Applicant</td><td style='font-weight:bold;'>Total Score</td></tr>"); var count = 1; for (var index = 65; index <= 71; index++) { Char alphabet = (Char)index; var data = dataRows.Where(dr => dr.Field <string>("ClassificationName") == alphabet.ToString()).ToList(); if (data != null && data.Count > 0) { sb.AppendFormat("<tr><td colspan='3'>Turnover Classification {0}</td></tr>", alphabet); for (var j = 0; j < data.Count(); j++) { sb.AppendFormat("<tr><td>{0}</td><td style='font-weight:bold;'>{1}</td><td style='font-weight:bold;'>{2}</td></tr>", count, data[j]["OrganisationName"], data[j]["Score"]); count++; } } } sb.AppendLine("</tbody></table></body></html>"); db.InsertBreak(BreakType.PageBreak); db.InsertHtml(sb.ToString(), false); } catch (Exception ex) { ex = new Exception("Report -> AppendJuryTabularDetail: \r\n\r\n", ex); LogManager.WriteErrorLog(ex); destdoc = null; throw ex; } GC.Collect(); }
/// <summary> /// html转换成Word /// </summary> /// <param name="htmlLabel"></param> public bool TransToWord(string htmlLabel, string title, string outPath) { try { title = FilterPath(title); string fullPath = outPath + title + ".doc";//word文件保存路径 htmlLabel = StringConvert(htmlLabel); htmlLabel = $"<!DOCTYPE html><html><head></head><body><div>{htmlLabel}</div></body></html>"; // File.WriteAllText(fullPath, htmlLabel); Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc); builder.InsertHtml(htmlLabel); doc.Save(fullPath); return(true); } catch (Exception e) { WriteLog(e); return(false); } }
private Aspose.Words.Document CreateDoc(string code) { var baseEntities = FormulaHelper.GetEntities <BaseEntities>(); var uiForm = baseEntities.Set <S_UI_Form>().Where(c => c.Code == code).OrderByDescending(c => c.VersionStartDate).FirstOrDefault(); 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 = uiForm.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 #region 操作列表 if (System.Configuration.ConfigurationManager.AppSettings["Flow_WordExportComment"] == "True") { StringBuilder sb = new StringBuilder(); sb.AppendLine("<table style=\"width:100%\" bordercolor=\"#000000\" cellspacing=\"0\" cellpadding=\"2\" border=\"1\">"); sb.Append(@" <tr> <td width='100'>环节名称</td> <td width='150'>接收人部门</td> <td width='60'>审批人</td> <td width='100'>操作</td> <td width='110'>审批时间</td> <td>审批意见</td> </tr>"); sb.AppendLine("<tr>"); sb.Append(@" <td width='100'>{TableStart:FlowComments}{Field:StepName}</td> <td width='150'>{Field:TaskUserDept}</td> <td width='60'>{Field:ExecUserName}</td> <td width='100'>{Field:ExecRoutingName}</td> <td width='110'>{Field:ExecTime}</td> <td>{Field:ExecComment}{TableEnd:FlowComments}</td> "); sb.AppendLine("</tr>"); sb.AppendLine("</table>"); layout += sb.ToString(); } #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); return(doc); }
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("")); }