internal static T GetFirstParent <T>(OpenXmlElement element) where T : OpenXmlElement { if (element.Parent == null) { return(default(T)); } if (element.Parent.GetType() == typeof(T)) { return(element.Parent as T); } else { return(DocumentMerge.GetFirstParent <T>(element.Parent)); } }
internal static void ExecuteSwitches(OpenXmlElement element, string[] switches) { if (switches == null || Enumerable.Count <string>((IEnumerable <string>)switches) == 0) { return; } if (Enumerable.Contains <string>((IEnumerable <string>)switches, "dp")) { Paragraph firstParent = DocumentMerge.GetFirstParent <Paragraph>(element); if (firstParent == null) { return; } firstParent.Remove(); } else if (Enumerable.Contains <string>((IEnumerable <string>)switches, "dr")) { TableRow firstParent = DocumentMerge.GetFirstParent <TableRow>(element); if (firstParent == null) { return; } firstParent.Remove(); } else { if (!Enumerable.Contains <string>((IEnumerable <string>)switches, "dt")) { return; } Table firstParent = DocumentMerge.GetFirstParent <Table>(element); if (firstParent != null) { firstParent.Remove(); } } }
public static byte[] WordDokumanOlustur(string sablon, DataSet dataset, Dictionary <string, string> degerler) { byte[] buffer = File.ReadAllBytes(sablon); string[] switches = (string[])null; using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(buffer, 0, buffer.Length); using (WordprocessingDocument docx = WordprocessingDocument.Open((Stream)memoryStream, true)) { DocumentMerge.ConvertFieldCodes((OpenXmlElement)docx.MainDocumentPart.Document); foreach (SimpleField field in docx.MainDocumentPart.Document.Descendants <SimpleField>()) { string fieldName = DocumentMerge.GetFieldName(field, out switches); if (!string.IsNullOrEmpty(fieldName) && fieldName.StartsWith("TBL_")) { TableRow firstParent1 = DocumentMerge.GetFirstParent <TableRow>((OpenXmlElement)field); if (firstParent1 != null) { Table firstParent2 = DocumentMerge.GetFirstParent <Table>((OpenXmlElement)firstParent1); if (firstParent2 != null) { string nameFromFieldName = DocumentMerge.GetTableNameFromFieldName(fieldName); if (dataset != null && dataset.Tables.Contains(nameFromFieldName) && dataset.Tables[nameFromFieldName].Rows.Count != 0) { DataTable dataTable = dataset.Tables[nameFromFieldName]; List <TableCellProperties> list1 = new List <TableCellProperties>(); List <string> list2 = new List <string>(); List <string> list3 = new List <string>(); List <SimpleField> list4 = new List <SimpleField>(); foreach (TableCell tableCell in firstParent1.Descendants <TableCell>()) { list1.Add(tableCell.GetFirstChild <TableCellProperties>()); Paragraph firstChild1 = tableCell.GetFirstChild <Paragraph>(); if (firstChild1 != null) { ParagraphProperties firstChild2 = firstChild1.GetFirstChild <ParagraphProperties>(); if (firstChild2 != null) { list3.Add(firstChild2.OuterXml); } else { list3.Add((string)null); } } else { list3.Add((string)null); } string str = string.Empty; SimpleField simpleField = (SimpleField)null; using (IEnumerator <SimpleField> enumerator = tableCell.Descendants <SimpleField>().GetEnumerator()) { if (enumerator.MoveNext()) { SimpleField current = enumerator.Current; simpleField = current; str = DocumentMerge.GetColumnNameFromFieldName(DocumentMerge.GetFieldName(current, out switches)); } } list2.Add(str); if (str != "") { list4.Add(simpleField); } } TableRowProperties firstChild = firstParent1.GetFirstChild <TableRowProperties>(); foreach (DataRow dataRow in (InternalDataCollectionBase)dataTable.Rows) { TableRow tableRow = new TableRow(); if (firstChild != null) { tableRow.Append(new OpenXmlElement[1] { (OpenXmlElement) new TableRowProperties(firstChild.OuterXml) }); } for (int index = 0; index < list1.Count; ++index) { TableCellProperties tableCellProperties = new TableCellProperties(list1[index].OuterXml); TableCell tableCell = new TableCell(); tableCell.Append(new OpenXmlElement[1] { (OpenXmlElement)tableCellProperties }); Paragraph paragraph = new Paragraph(new OpenXmlElement[1] { (OpenXmlElement) new ParagraphProperties(list3[index]) }); tableCell.Append(new OpenXmlElement[1] { (OpenXmlElement)paragraph }); try { if (!string.IsNullOrEmpty(list2[index])) { if (!dataTable.Columns.Contains(list2[index])) { throw new Exception(string.Format("Unable to complete template: column name '{0}' is unknown in parameter tables !", (object)list2[index])); } if (!dataRow.IsNull(list2[index])) { string text = dataRow[list2[index]].ToString(); paragraph.Append(new OpenXmlElement[1] { (OpenXmlElement)DocumentMerge.GetRunElementForText(text, list4[index]) }); } } } catch { } tableRow.Append(new OpenXmlElement[1] { (OpenXmlElement)tableCell }); } firstParent2.Append(new OpenXmlElement[1] { (OpenXmlElement)tableRow }); } firstParent1.Remove(); } } } } } foreach (SimpleField field in docx.MainDocumentPart.Document.Descendants <SimpleField>()) { string fieldName = DocumentMerge.GetFieldName(field, out switches); if (!string.IsNullOrEmpty(fieldName) && fieldName.StartsWith("TBL_")) { TableRow firstParent1 = DocumentMerge.GetFirstParent <TableRow>((OpenXmlElement)field); if (firstParent1 != null) { Table firstParent2 = DocumentMerge.GetFirstParent <Table>((OpenXmlElement)firstParent1); if (firstParent2 != null) { string nameFromFieldName = DocumentMerge.GetTableNameFromFieldName(fieldName); if ((dataset == null || !dataset.Tables.Contains(nameFromFieldName) || dataset.Tables[nameFromFieldName].Rows.Count == 0) && Enumerable.Contains <string>((IEnumerable <string>)switches, "dt")) { firstParent2.Remove(); } } } } } DocumentMerge.FillWordFieldsInElement(docx, degerler, (OpenXmlElement)docx.MainDocumentPart.Document); ((OpenXmlPartRootElement)docx.MainDocumentPart.Document).Save(); foreach (HeaderPart headerPart in docx.MainDocumentPart.HeaderParts) { DocumentMerge.ConvertFieldCodes((OpenXmlElement)headerPart.Header); DocumentMerge.FillWordFieldsInElement(docx, degerler, (OpenXmlElement)headerPart.Header); ((OpenXmlPartRootElement)headerPart.Header).Save(); } foreach (FooterPart footerPart in docx.MainDocumentPart.FooterParts) { DocumentMerge.ConvertFieldCodes((OpenXmlElement)footerPart.Footer); DocumentMerge.FillWordFieldsInElement(docx, degerler, (OpenXmlElement)footerPart.Footer); ((OpenXmlPartRootElement)footerPart.Footer).Save(); } } memoryStream.Seek(0L, SeekOrigin.Begin); return(memoryStream.ToArray()); } }