/// <summary> /// 应用表格文本模板 /// </summary> /// <param name="tableContent"></param> /// <param name="stringTemplates"></param> private void ApplyTableToStringTemplate(XWPFTable tableContent, IReadOnlyCollection <StringTemplateModel> stringTemplates) { foreach (XWPFTableRow row in tableContent.Rows) { foreach (XWPFTableCell cell in row.GetTableCells()) { IBodyElement bodyElement = cell.BodyElements[0]; if (!(bodyElement is XWPFParagraph paragraph)) { continue; } string oldText = paragraph.ParagraphText; if (string.IsNullOrEmpty(oldText)) { continue; } string newText = oldText; foreach (StringTemplateModel template in stringTemplates) { string key = "{$" + template.Key + "}"; if (!newText.Contains(key)) { continue; } newText = newText.Replace(key, template.Value); } if (newText != oldText) { paragraph.ReplaceText(oldText, newText); } } } }
public void TestAddTableToFootnote() { XWPFTable table = footnote.CreateTable(); Assert.IsNotNull(table); XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut); XWPFFootnote testFootnote = docIn.GetFootnoteByID(footnoteId); XWPFTable testTable = testFootnote.GetTableArray(0); Assert.IsNotNull(testTable); table = footnote.CreateTable(2, 3); Assert.AreEqual(2, table.NumberOfRows); Assert.AreEqual(3, table.GetRow(0).GetTableCells().Count); // If the table is the first body element of the footnote then // a paragraph with the footnote reference should have been // added automatically. Assert.AreEqual(3, footnote.BodyElements.Count, "Expected 3 body elements"); IBodyElement testP1 = footnote.BodyElements[0]; Assert.IsTrue(testP1 is XWPFParagraph, "Expected a paragraph, got " + testP1.GetType().Name); XWPFRun r1 = ((XWPFParagraph)testP1).Runs[0]; Assert.IsNotNull(r1); Assert.IsTrue(r1.GetCTR().GetFootnoteRefList().Count > 0, "No footnote reference in testP1"); Assert.IsNotNull(r1.GetCTR().GetFootnoteRefArray(0), "No footnote reference in testP1"); }
private int GetPosOfBodyElement(IBodyElement needle) { BodyElementType elementType = needle.ElementType; for (int index = 0; index < this.bodyElements.Count; ++index) { IBodyElement bodyElement = this.bodyElements[index]; if (bodyElement.ElementType == elementType && bodyElement.Equals((object)needle)) { return(index); } } return(-1); }
public void AppendBodyElementText(StringBuilder text, IBodyElement e) { if (e is XWPFParagraph) { AppendParagraphText(text, (XWPFParagraph)e); } else if (e is XWPFTable) { appendTableText(text, (XWPFTable)e); } else if (e is XWPFSDT) { text.Append(((XWPFSDT)e).Content.Text); } }
/// <summary> /// 获取单元格内容 /// </summary> /// <param name="rowIndex"></param> /// <param name="colIndex"></param> /// <param name="tableContent"></param> /// <param name="value"></param> /// <param name="setCell"></param> /// <returns></returns> private XWPFParagraph GetCellContent(int rowIndex, int colIndex, IBodyElement tableContent, string value, Func <int, int, XWPFParagraph, XWPFRun> setCell) { var ctP = new CT_P(); var paragraph = new XWPFParagraph(ctP, tableContent.Body); if (setCell == null) { paragraph.VerticalAlignment = TextAlignment.CENTER; paragraph.Alignment = ParagraphAlignment.CENTER; XWPFRun run = paragraph.CreateRun(); run.SetText(value); } else { setCell.Invoke(rowIndex, colIndex, paragraph); } return(paragraph); }
private int GetBodyElementSpecificPos(int pos, List <IBodyElement> list) { if (list.Count == 0 || pos < 0 || pos >= this.bodyElements.Count) { return(-1); } IBodyElement bodyElement = this.bodyElements[pos]; if (bodyElement.ElementType != list[0].ElementType) { return(-1); } for (int index = Math.Min(pos, list.Count - 1); index >= 0; --index) { if (list[index] == bodyElement) { return(index); } } return(-1); }
private void AppendBodyElementText(StringBuilder text, IBodyElement e, bool isLast) { if (e is XWPFParagraph) { text.Append(((XWPFParagraph)e).Text); if (isLast == false) { text.Append('\t'); } } else if (e is XWPFTable) { XWPFTable eTable = (XWPFTable)e; foreach (XWPFTableRow row in eTable.Rows) { foreach (XWPFTableCell cell in row.GetTableCells()) { IList <IBodyElement> localBodyElements = cell.BodyElements; for (int i = 0; i < localBodyElements.Count; i++) { bool localIsLast = (i == localBodyElements.Count - 1) ? true : false; AppendBodyElementText(text, localBodyElements[i], localIsLast); } } } if (isLast == false) { text.Append('\n'); } } else if (e is XWPFSDT) { text.Append(((XWPFSDT)e).Content.Text); if (isLast == false) { text.Append('\t'); } } }
/// <summary> /// 获得表名和列表 /// </summary> /// <param name="tableContent"></param> /// <param name="rowIndex"></param> /// <returns></returns> private (string tableName, Dictionary <int, string> colNames) GetTableNameAndColName(XWPFTable tableContent, int rowIndex) { XWPFTableRow row = tableContent.GetRow(rowIndex); List <XWPFTableCell> cells = row.GetTableCells(); if (cells.Count <= 0 || cells[0].BodyElements.Count <= 0) { return(null, null); } string tableName = string.Empty; var colNames = new Dictionary <int, string>(); for (var index = 0; index < cells.Count; index++) { IBodyElement bodyElement = cells[index].BodyElements[0]; if (!(bodyElement is XWPFParagraph paragraph)) { continue; } string value = paragraph.ParagraphText; if (!value.VerifyRegex("^{\\$.+\\..+\\}$")) { continue; } string[] tableNameAndColumnName = value.Substring(2, value.Length - 3).Split('.'); if (tableNameAndColumnName.Length != 2) { continue; } tableName = tableNameAndColumnName[0]; if (colNames.ContainsKey(index)) { continue; } colNames.Add(index, tableNameAndColumnName[1]); } return(tableName, colNames); }
IHtmlElement IContentModel<IHtmlElement, IBodyElement>.this[IBodyElement child] { get { return this[child]; } }
private int GetPosOfBodyElement(IBodyElement needle) { BodyElementType type = needle.ElementType; IBodyElement current; for (int i = 0; i < bodyElements.Count; i++) { current = bodyElements[(i)]; if (current.ElementType == type) { if (current.Equals(needle)) { return i; } } } return -1; }
IHtmlElement IContentModel <IHtmlElement, IBodyElement> .this[IBodyElement child] { get { return(this[child]); } }
private void AppendBodyElementText(StringBuilder text, IBodyElement e, bool isLast) { if (e is XWPFParagraph) { text.Append(((XWPFParagraph)e).Text); if (isLast == false) { text.Append('\t'); } } else if (e is XWPFTable) { XWPFTable eTable = (XWPFTable)e; foreach (XWPFTableRow row in eTable.Rows) { foreach (XWPFTableCell cell in row.GetTableCells()) { IList<IBodyElement> localBodyElements = cell.BodyElements; for (int i = 0; i < localBodyElements.Count; i++) { bool localIsLast = (i == localBodyElements.Count - 1) ? true : false; AppendBodyElementText(text, localBodyElements[i], localIsLast); } } } if (isLast == false) { text.Append('\n'); } } else if (e is XWPFSDT) { text.Append(((XWPFSDT)e).Content.Text); if (isLast == false) { text.Append('\t'); } } }