/ Published in: C#
After importing tables from source document to target document, use this snippet to import table styles missing in the target document to enable correct formatting of the table.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
internal static XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; internal static void ImportTableStyles(string sourcefilepath, string destinationfilepath) { using (var repeaterSourceDocument = WordprocessingDocument.Open(sourcefilepath, true)) { XDocument source_style_doc; var repeaterSourceDocumentPackagePart = repeaterSourceDocument.MainDocumentPart.OpenXmlPackage.Package.GetPart(new Uri("/word/styles.xml", UriKind.Relative)); //Get styles.xml source_style_doc = XDocument.Load(tr); var tableStylesFromRepeaterSource = source_style_doc.Descendants(w + "style").Where(x => x.Attribute(w + "type").Value == "table").Select(x => x).ToList(); using (var targetFileToImportTableStyles = WordprocessingDocument.Open(destinationfilepath, true)) { XDocument dest_style_doc; var destpart = targetFileToImportTableStyles.MainDocumentPart.OpenXmlPackage.Package.GetPart(new Uri("/word/styles.xml", UriKind.Relative)); //Get styles.xml dest_style_doc = XDocument.Load(trd); //Add all the style elements from source document styles.xml foreach (var styleelement in tableStylesFromRepeaterSource) { if (!dest_style_doc.Elements(XName.Get("styles", w.NamespaceName)).Any(x => (string)x.Attribute("styleId") == (string)styleelement.Attribute("styleId"))) { dest_style_doc.Element(XName.Get("styles", w.NamespaceName)).Add(styleelement); } } //Save the style.xml of targetFile dest_style_doc.Save(tw, SaveOptions.None); } } }
URL: http://www.adigopula.co.uk/post/Importing-Table-Styles-in-Word-Document-using-OpenXML-sdk.aspx