Inserting newlines in Word using OpenXML

To insert newlines, you have to add a Break instance to the Run. Example: run.AppendChild(new Text(“Hello”)); run.AppendChild(new Break()); run.AppendChild(new Text(“world”)); The XML produced will be something like: <w:r> <w:t>Hello</w:t> <w:br/> <w:t>world</w:t> </w:r>

Applying % number format to a cell value using OpenXML

WorkbookStylesPart sp = workbookPart.AddNewPart<WorkbookStylesPart>(); Create a stylesheet, sp.Stylesheet = new Stylesheet(); Create a numberingformat, sp.Stylesheet.NumberingFormats = new NumberingFormats(); // #.##% is also Excel style index 1 NumberingFormat nf2decimal = new NumberingFormat(); nf2decimal.NumberFormatId = UInt32Value.FromUInt32(3453); nf2decimal.FormatCode = StringValue.FromString(“0.0%”); sp.Stylesheet.NumberingFormat.Append(nf2decimal); Create a cell format and apply the numbering format id cellFormat = new CellFormat(); cellFormat.FontId = 0; … Read more

OPENXML with xmlns:dt

Is there a particular reason that you need to use OPENXML to do this? You can easily get the information with a XQUERY in 2005 like this: declare @xmldata xml set @xmldata=”<data xmlns=”http://www.aaa.com/master_browse_response” xmlns:dt=”http://www.aaa.com/DataTypes”> <products> <product> <product_id>121403</product_id> <countries> <dt:country>GBR</dt:country> <dt:country>USA</dt:country> </countries> </product> </products> </data>” ;WITH XMLNAMESPACES ( DEFAULT ‘http://www.aaa.com/master_browse_response’, ‘http://www.aaa.com/DataTypes’ as dt ) SELECT x.c.value(‘(../../product_id)[1]’, … Read more

Using OpenXmlReader

I think you took the wrong WorksheetPart for reading the rows. The line workbookPart.WorksheetParts.First(); gets the first WorksheetPart of the collection which must not necessarily be the first worksheet as you see it in Microsoft Excel. So, iterate through all WorksheetParts and you should see some output on your console window. static void ReadExcelFileSAX(string fileName) … Read more

Replace bookmark text in Word file using Open XML SDK

Here’s my approach after using you guys as inspiration: IDictionary<String, BookmarkStart> bookmarkMap = new Dictionary<String, BookmarkStart>(); foreach (BookmarkStart bookmarkStart in file.MainDocumentPart.RootElement.Descendants<BookmarkStart>()) { bookmarkMap[bookmarkStart.Name] = bookmarkStart; } foreach (BookmarkStart bookmarkStart in bookmarkMap.Values) { Run bookmarkText = bookmarkStart.NextSibling<Run>(); if (bookmarkText != null) { bookmarkText.GetFirstChild<Text>().Text = “blah”; } }