Open XML SDK 2.0 August CTP, Videos, and 50+ Open XML Code Samples

Zeyad Rajabi announced this week the availability of the new Open XML SDK 2.0 August CTP. As mentioned by Zeyad, changes to this CTP include:

Add semantic level validation support

Add markup compatibility/extensibility support

General improvements based on your feedback

If you are into Open XML Development, you will also be interested in a few more videos and code samples.

dd183105.video_30(en-us,MSDN.10)[2] Open XML SDK 2.0 Visual How-to Series

We have released some videos, walkthroughs, and code sample downloads in the last couple weeks:

Visual How To: Creating a PowerPoint 2007 Presentation from a Folder of Images by Using the Open XML SDK 2.0 for Microsoft Office
Visual How To: Merging Simple Content from Excel 2007 Workbooks and Worksheets by Using the Open XML SDK 2.0 for Microsoft Office
Visual How To: Using the Open XML SDK 2.0 Classes Versus Using .Net XML Services
Visual How To: Merging PowerPoint 2007 Decks Together by Using the Open XML SDK 2.0 for Microsoft Office
Visual How To: Generating Documents with Headers and Footers in Word 2007 by Using the Open XML SDK 2.0 for Microsoft Office
Visual How To: Coding a Long-Running Operation Page in SharePoint Server 2007
Visual How To: Embedding Documents in Word 2007 by Using the Open XML SDK 2.0 for Microsoft Office
Visual How To: Adding Images to Documents in Word 2007 by Using the Open XML SDK 2.0 for Microsoft Office

More to come soon.

dd183105.development_30(en-us,MSDN.10)[2] Open XML SDK 2.0 for Microsoft Office Code Snippets for Visual Studio 2008

I am also glad to announce that yesterday we published 50+ Open XML code samples packaged as Visual Studio code snippets. The code samples were created by Ken Getz as an update to the previous Open XML Visual Studio 2005 code snippets released a couple years ago. The previous snippets provided samples on manipulating document parts and packages using the System.IO.Packaging namespace. This new set provides more code samples that show how to manipulate document parts and packages using the Open XML SDK 2.0. The snippets were tested and work well with the August CTP released this week.

You can download the snippets here:

https://www.microsoft.com/downloads/details.aspx?FamilyID=78bea298-a3f9-44cf-bde0-b4f30dc986df&displaylang=en

The snippets in this download use the Open XML SDK 2.0 to accomplish many tasks involving Microsoft Excel 2007, Microsoft PowerPoint 2007, and Microsoft Word 2007 documents.

You can use the enclosed code snippets with the Microsoft Visual Studio® 2008 Code Snippet Manager. Each snippet provides unique functionality that you can reuse within an application. This download provides snippets written in Microsoft Visual Basic.NET® and Microsoft C#® development languages.

Here’s a list of all the snippets included in this package:

Microsoft Office Excel Snippets

Excel: Add custom UI

Add custom ribbon markup to a specified workbook.

Excel: Delete comments by user

Delete comments from a workbook, given an author name. Pass an empty author name to delete all comments.

Excel: Delete row

Given a document name, a worksheet name, and a one-based row index, delete a row from the worksheet.

Excel: Delete worksheet

Delete the specified sheet from within the specified workbook.

Excel: Delete XL4 macro sheets

Given a document name delete all the XL4 macro sheets.

Excel: Export chart

Given a workbook and the name of a chart, export the chart to an XML file.

Excel: Get all sheets

Retrieve a List of all the sheets in a workbook.

Excel: Get cell for reading

Given a document name, a worksheet name, and a cell name, retrieve a reference to the cell for reading. Raise an exception of the cell doesn't exist.

Excel: Get cell for writing

Given a spreadsheet document, a sheet name and an address, return a reference to a cell ready to accept a value. Create the cell if necessary.

Excel: Get cell format

Given a document name, a worksheet name, and a cell name, return the CellFormat instance associated with the cell.

Excel: Get cell value

Given a document name, a worksheet name, and a cell name, get the value of the cell.

Excel: Get cell value given row and column

Rertrieve a cell value given its row and column numbers, or a row number and column name.

Excel: Get column header

Given a document name, a worksheet name, and a cell name, get the column of the cell and return the content of the first cell in that column.

Excel: Get defined names

Given a document name, return a dictionary of defined names.

Excel: Get hidden rows or columns

Given a document name, and a worksheet name, return a list of either hidden rows or columns.

Excel: Get hidden worksheets

Retrieve a list of all the hidden worksheets in a workbook.

Excel: Get style border

Retrieve information about a cell's border.

Excel: Get style border info

Get style border information.

Excel: Get style fill

Retrieve information about a cell's fill style.

Excel: Get style fill information

Retrieve specific font formatting information about a cell.

Excel: Get style font information

Retrieve specific font formatting information about a cell.

Excel: Insert Custom XML

Insert a custom XML part into a workbook.

Excel: Insert header or footer

Insert a header or footer into a workbook.

Excel: Insert number into cell

Given a file, a sheet, and a cell, insert a specified numeric value.

Excel: Insert string into cell

Given a document name, a worksheet name, a cell name, and a value, insert the text into the specified cell.

Excel: Insert string into cell

Insert a string into a specified cell.

Excel: Set recalc option

Given a file name, set the recalculation behavior of the workbook. Return the previous calc mode.

Excel: Worksheet part by name

Retrieve an entire worksheet part, given its name.

Microsoft Office PowerPoint Snippets

PowerPoint: Add comment

Add a comment to the first slide in a presentation.

PowerPoint: Delete all comments, by author

Delete all comments in a PowerPoint presentation for a specific author. Pass an empty string for the author name to delete all comments.

PowerPoint: Delete slide by title

Given a presentation and a slide title, delete the slide.

PowerPoint: Get List of Slide Titles

Given a presentation file, retrieve a generic list of strings containing the slide titles. Some slide titles might be empty strings.

PowerPoint: Get slide count

Given a file name, retrieve the number of slides in the presentation.

PowerPoint: Get slide index, by title

Find the zero-based index of a slide within a presentation, given its title.

PowerPoint: Reorder slides

Given a PPT deck, an original position, and a new position, attempt to place the slide in the original position into the new position within the deck.

PowerPoint: Replace image on slide

Given a presentation, a slide title, and an image file, replace the first image on the selected slide with the new image.

PowerPoint: Replace slide title

Given a presentation, a slide title, and a new slide title, find the slide, and modify its title.

Microsoft Office Word Snippets

Word: Accept all revisions

Given a document name and an author name, accept all revisions by the specified author. Pass an empty string for the author to accept all revisions.

Word: Add Table

Add a table, including text from an array, to the end of a document.

Word: Convert DOCM to DOCX

Convert a macro-enabled document to a standard document.

Word: Delete all comments

Given a document name and an author name, delete all comments by the specified author. Pass an empty string for the author to accept all revisions.

Word: Delete headers and footers

Delete headers and footers from a document.

Word: Delete hidden text

Delete hidden text from a document.

Word: Extract Styles

Extract the Styles part from a document, so you can insert it into another document.

Word: Get application property

Retrieve the value of an application property from a document.

Word: Get Content Control

Retrieve the markup for a specific content control.

Word: Get core property

Retrieve the value of a core property from a document.

Word: Get custom property

Retrieve a custom property for a document.

Word: Replace the styles part

Replace the entire styles part with a styles part extracted from another document.

Word: Retrieve comments

Retrieve all the comments from a document in an XDocument instance.

Word: Retrieve Table of Contents

Retrieve the table of contents markup, if it exists.

Word: Set application property

Given a document name, a property to set, and a value, update the document.

Word: Set core property

Set a core Word property (like Version, or LastModifiedBy).

Word: Set custom property

Given a document name, a property name/value, and the property type, add a custom property to a document.

Word: Set print orientation

Set the print orientation for each section in a document.

Enjoy!

Comments

  • Anonymous
    September 16, 2009
    Hi Erica, Thanks, useful stuff. Let me point to an additional article on msdn on working with Open XML from VBA: Title: Editing Data in an Excel 2007 Open XML File with VBA Url: http://msdn.microsoft.com/en-us/library/dd819387.aspx

  • Anonymous
    September 25, 2009
    Hi Erica.. I'm looking for a code snippet for inserting a row in before or after a certain existing row.. Can you please provide the code snippet.. Thanks for your help. TRV

  • Anonymous
    November 24, 2009
    I have a question about: InsertCellInWorksheet I have found that if you get the cells out of row order (ie A4 before A1) then Excel throws an error when opening the resulting xlsx file. I think that the snippet needs to make sure that the row is inserted in the correct place just as it does for the Cell. Or am I missing something? I am writing directly from a memorystream to a Sharepoint CopyInto web service, so it is possible that the WorkSheet.save function is doing some final sorting that I am not triggering. Here is how I modified the snippet to work: // If the worksheet does not contain a row with the specified row index, insert one.          var theRow = sheetData.Elements<Row>().            Where(r => r.RowIndex.Value >= rowNumber).            OrderBy(r => r.RowIndex.Value)            .FirstOrDefault();          if (theRow != null && theRow.RowIndex > rowNumber)          {              nextRow = theRow;              theRow = null;          }          if (theRow == null)          {            theRow = new Row();            theRow.RowIndex = rowNumber;            if (nextRow == null)            {                sheetData.Append(theRow);            }            else            {                sheetData.InsertBefore(theRow, nextRow);            }          }

  • Anonymous
    January 26, 2010
    hi i want to bind Content Control i have added my xml into the document and thts in the folder of CustomXml this is the code for adding public void AddCustomXmlPart(string fullDocumentPath)    {        XmlDocument xmlDoc = GetXmlDocumentFromString();        if (xmlDoc != null)        {            using (Package package = Package.Open(fullDocumentPath, FileMode.Open,FileAccess.ReadWrite))            {                Uri uriPartTarget = new Uri("/customXml/ContentXml.xml", UriKind.Relative);                if (package.PartExists(uriPartTarget))                {                    package.DeletePart(uriPartTarget);                }                if (!package.PartExists(uriPartTarget))                {                    PackagePart customXml = package.CreatePart(uriPartTarget,                        "application/vnd.openxmlformats-officedocument.customXmlProperties+xml");                    using (Stream partStream = customXml.GetStream(FileMode.Create,FileAccess.ReadWrite))                    {                        xmlDoc.Save(partStream);                    }                }            }        }    } public void BindContentControls()    {        AddCustomXmlPart(Server.MapPath(Request.ApplicationPath + "/OpenDoc.docx"));        using (WordprocessingDocument theDoc = WordprocessingDocument.Open(Server.MapPath(Request.ApplicationPath + "/OpenDoc.docx"), true))        {                MainDocumentPart mainPart = theDoc.MainDocumentPart;                List<SdtElement> sdtList = mainPart.Document.Descendants<SdtElement>().ToList();                foreach (SdtElement sdt in sdtList)                {                }        }    } how can i bind content control with my custom xml????

  • Anonymous
    January 26, 2010
    private XmlDocument GetXmlDocumentFromString()    {    XmlDocument doc = new XmlDocument();    doc.Load(Server.MapPath(Request.ApplicationPath + "/ContentXml.xml"));    string xmlString = doc.OuterXml.ToString();    StringReader reader = new StringReader(xmlString);    XmlDocument xmlDoc = new XmlDocument();    xmlDoc.Load(reader);    return xmlDoc;    }