Overview of the Open XML SDK for JavaScript API

Return to the
Open XML SDK for JavaScript
Developer Center
There are essentially three ‘classes’ in the API.  Of
course, JavaScript does not have classes as such, but there are three types of
objects in the API that can be described as classes:

Opening a Word Document, a Spreadsheet, or a Presentation

To open an Open XML file, you pass either a binary document
that is encoded in base64 ASCII, or you pass a Flat OPC document as a string. 
For instance, to open a DOCX that is encoded with base64 ASCII, the code would
look as follows:

// Open a document that is stored as a base64 string.
var doc = new openXml.OpenXmlPackage(document_base64);

Opening a document that is stored as XML in the Flat OPC
form is exactly the same:

// Open a document that is stored as XML in the Flat OPC format.
var doc = new openXml.OpenXmlPackage(document_flatOpc);

There are several ways to get your hands on an base64
encoded DOCX, XLSX, or PPTX:

  1. You can enable the end-user to click on a small flash control (that looks like a button), which loads the file from the file system and passes the base64 encoded ASCII to your JavaScript code.
  2. You can manually convert a binary document to base64 encoded ASCII, and then transform the long string into code to initialize a JavaScript variable. This is a convenient way to store a template document or spreadsheet, which you will then modify programmatically and then enable the end-user to save locally.
  3. You can request the document from the web server using an httpRequest. The ajax method of jQuery is a convenient way to do this.
  4. When using Node.js, you can simply open the DOCX, XLSX, or PPTX. Node.js doesn’t have or need the same restrictions as JavaScript code that is running in the browser.
  5. When writing an Office Client App for Word 2013, or when writing a SharePoint 2013 App, you can retrieve documents from document libraries.
  6. Windows 8 ‘Store’ applications have their own means for finding and opening documents. I’ll cover these techniques when I write the Windows 8 blog-posts and screen-casts.

Once you have opened the document, you typically will ‘dot’
into one of the convenience functions that retrieve the main part of the
package.  For example, if you have opened a WordprocessingML document,
you ‘dot’ into the mainDocumentPart function:

var doc = new openXml.OpenXmlPackage(openXmlDocument);
var mainPart = doc.mainDocumentPart();

After you have navigated to the part that you want to query
or modify, you then call the getXDocument method, which retrieves the XDocument
object (from LINQ to XML for JavaScript).  You can either query the document or
you can modify the document.  The following code shows opening a document,
creating a new paragraph element, finding the w:body part, finding the
last paragraph in the w:body part, and then inserting the new paragraph
element after the last paragraph.

// Open the document, which is stored as a base64 string.
var doc = new openXml.OpenXmlPackage(openedFileData);

// Create a paragraph.
var p = new XElement(W.p,
    new XElement(W.r,
        new XElement(W.t, "Hello Open XML World")));

// Append the paragraph to the document.
var body = doc.mainDocumentPart().getXDocument().root.element(W.body);
var lastPara = body.elements(W.p).lastOrDefault();
if (lastPara !== null) {
    lastPara.addAfterSelf(p);
}

When you modify the XDocument object that you
retrieve using the getXDocument method, your changes will be
automatically included when you serialize the document to either a DOCX in
base64 encoded ASCII, or when you serialize to XML in the Flat OPC format.

Once you have opened a document, spreadsheet, or
presentation, you can also modify the package in the typical ways you need to
do with Open XML – you can add or delete relationships, and add or delete
parts.

When you have made all desired modifications to the package,
you can then serialize to XML in the Flat OPC form:

var flatOpcString = doc.saveToFlatOpc();

You can also, of course, serialize to a binary DOCX, XLSX,
or PPTX in base64 encoded ASCII:

var b64string = doc.saveToBase64();

There are several ways to save the Flat OPC or the base64
encoded DOCX, XLSX, or PPTX:

  1. You can enable the end-user to click on a small flash control (that looks like a button), which opens up a File Save-As dialog. The user can navigate to their desired location and save the DOCX or Flat OPC to their local storage.
  2. You can use httpRequest to send the document back to your web server.
  3. If using Node.Js, you can simply write code to save to the file system.
  4. When writing a Word 2013 App, you can replace the selection with the newly serialized Flat OPC.
  5. When writing an Office Client or SharePoint App, you can serialize to a document library.