Top Banner
Open XML SDK for Microsoft Office Learn How to Create Word and Excel Documents
48

Learn How to Create Word and Excel Documents. John DeVight Herndon, Virginia Senior Principal Software Engineer at ManTech Telerik MVP .

Dec 16, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Open XML SDK for Microsoft Office

Learn How to Create Word and Excel Documents

Page 2: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

John DeVight Herndon, Virginia Senior Principal Software Engineer at

ManTech Telerik MVP www.aspnetwiki.com [email protected]

Introductions

Page 3: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

About the Open XML SDK Library (5 minutes) “Telerik Products” Demo (5 minutes) Getting Started (10 minutes)

◦ Installing the Open XML SDK Library◦ Reference the Open XML SDK Library◦ Open XML SDK 2.0 Productivity Tool

Creating Word Documents (45 minutes)◦ Using “Open XML SDK 2.0 Productivity Tool for MS Office”◦ Using Bookmarks◦ Using XSLT

Creating Excel Documents (20 minutes)◦ Using “Open XML SDK 2.0 Productivity Tool for MS Office”◦ Using the SDK Directly◦ Using SpreadsheetLight

Agenda

Page 4: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

“The Open XML SDK 2.0 simplifies the task of manipulating Open XML packages and the underlying Open XML schema elements within a package. The Open XML SDK 2.0 encapsulates many common tasks that developers perform on Open XML packages, so that you can perform complex operations with just a few lines of code.” 1

An Open XML Package is a zip file containing XML documents, images and other files needed to display the document in a Microsoft Office application.

About the Open XML SDK Library:What is it?

1. http://msdn.microsoft.com/en-us/library/office/bb448854(v=office.14).aspx

Page 5: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

“Telerik Products” Demo

Page 6: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Open XML SDK 2.0 for Microsoft Office Download

Install the “Open XML SDK” from the OpenXMLSDKv2.msi installer.

Install the “Open XML SDK Productivity Tool for Microsoft Office” from the OpenXMLSDKTool.msi installer.

Getting Started:Installing the Open XML SDK Library

Page 7: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

In the .NET application, reference:

* WordLite is a class library that I created to make it easier to work with Word Documents.

** SpreadsheetLight is freely available under the MIT License that simplifies using the Open XML SDK Library when creating Excel Documents.

Getting Started:Reference the Open XML SDK Library

Assembly Visual Studio 2012

DocumentFormat.OpenXml Assemblies -> Extensions

WindowsBase Assemblies -> Framework

* WordLite Browse

** SpreadsheetLight Browse

Page 8: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Open Microsoft Office document and generate C# code to create the same document.

View the Open XML SDK Documentation.

Getting Started:Open XML SDK 2.0 Productivity Tool

Page 9: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Open XML SDK 2.0 Productivity Tool for MS Office

Bookmarks XSLT

Creating Word Documents: Options

Page 10: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Pros◦ All the code is generated for you.◦ Fast and Easy.

Cons◦ The tool generates a lot of code.◦ Every change requires the code to be updated

and deployed.

Creating Word Documents: Using “Open XML SDK 2.0 Productivity Tool”

Page 11: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Create and Save Word Document Open the Word Document using “Open XML

SDK 2.0 Productivity Tool” In “Document Explorer”, right-click on root

and select “Reflect Code”. Create console app in Visual Studio. Add

references to required assemblies. Create new class for the “generated code”. Instantiate class and call CreatePackage

from Main.

Creating Word Documents: Using “Open XML SDK 2.0 Productivity Tool”

Page 12: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Pros◦ Easy to create and update the “template”

document without having to write code.◦ The end user could create their own “template”

documents. Cons

◦ Requires code to know about all the bookmarks.

Create Word Documents:Using Bookmarks

Page 13: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Application for police departments to create Wanted Posters.

Create Word Documents:Using Bookmarks: “Real World” Implementation

Page 14: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Create Word Document Insert Bookmark

◦ Insert -> Bookmark◦ Give the bookmark a name◦ Click “Add” button

Making Bookmarks visible◦ File -> Options

Advanced -> Show document content Show bookmarks

Create Word Documents:Using Bookmarks – Create “Template” Document

Page 15: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Demo Creating Bookmarks in a Word Document

Create Word Documents:Using Bookmarks

Page 16: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Header and Footer Bookmarks Create Header

◦ Insert -> Header◦ Insert Bookmark

Insert -> Bookmark Give the bookmark a name Click “Add” button

◦ Design -> Close Header and Footer

Create Word Documents:Using Bookmarks

Page 17: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Open a word document with Bookmarks. Replace bookmarks with text. Replace image. Get the header and replace bookmark with

text. Get the footer and replace bookmark with

text.

Create Word Documents:Using Bookmarks and WordLite

Page 18: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

using (WordDocument doc = new WordDocument()){

doc.Open(@"C:\docs\mytemplate.docx");

doc.Bookmarks["Name"].ReplaceWithText("Kendo UI");

doc.ReplaceImage("image1", @"C:\images\kendo.png");

var bookmarks = doc.Header("header2").Bookmarks;bookmarks["Header"].ReplaceWithText("My Header");

doc.Close();buffer = doc.GetBuffer();

}

Response.Buffer = true;Response.AddHeader("Content-Disposition", "attachment; filename=Product.docx");return File(buffer, "application/vnd.openxmlformats-

officedocument.wordprocessingml.document", "Product.docx");

Create Word Documents:Using Bookmarks and WordLite

Page 19: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Identifying the header and footer◦ Open the document in the “Open XML SDK 2.0

Productivity Tool”.◦ Open each header and footer and look for the

bookmark. Identifying the image name (option 1)

◦ Open the document in the “Open XML SDK 2.0 Productivity Tool”

◦ Locate the image. Identifying the image name (option 2)

◦ Change the extension on the Word document to .zip◦ Extract the contents of the .zip file and look at each

image.

Create Word Documents: Using Bookmarks

Page 20: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Review WordController.ProductBookmarks Controller Action.

Create Word Documents: Using Bookmarks and WordLite

Page 21: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Add bookmark to the document. Replace bookmark with image.

doc.Bookmarks["Image"].ReplaceWithImage(@“C:\Images", “kendo.png”, "image/png");

Review WordController. ProductBookmarksAddImage Controller Action.

Create Word Documents: Using Bookmarks and WordLite

Page 22: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Demo Allowing End User to Create Templates

Create Word Documents: Using Bookmarks

Page 23: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Pros◦ Templates can be changed without having to

recompile.◦ .NET code is simplified.◦ Good approach when the document format does

not change much over time. Cons

◦ Templates can get a bit complicated to manipulate.

Creating Word Documents: Using XSLT

Page 24: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Creating Government Contracting Documents

Creating Word Documents: Using XSLT : “Real World” Implementation

Page 25: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

An Open XML Package is a zip file containing XML documents, images and other files needed to display the document in a Microsoft Office application.

Creating Word Documents:Review Open XML Package Definition

Page 26: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Create Word Document◦ Put “placeholders” for values◦ Save document

Create XSLT file◦ Rename .docx to .zip◦ Open zip file and get the word/document.xml file◦ Rename with .xslt extension◦ Replace:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>◦ With:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match=“/">◦ Add to the end of the file:

</xsl:template> </xsl:stylesheet>

Creating Word Documents:Using XSLT

Page 27: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

products.xml

<productList> <products> <product id=“…“ name=“…“ summary=“...“ image=“…“ price=“…“ overview=“..." /> </products></productList>

XPath editor - http://qutoric.com/xmlquire/ XPath examples:

◦ Select product names◦ Select product where id = 5◦ Select products that have widgets

Creating Word Documents:Using XSLT

Page 28: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

<xsl:value-of select=“”/> <xsl:if test=“”></xsl:if> <xsl:for-each select=“”></xsl:for-each>

Creating Word Documents:Using XSLT

Page 29: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Demo Implementing XSL Template.

Review WordController.ProductXslt Controller Action.

Creating Word Documents:Using XSLT

Page 30: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Adding C# Support to XSLT◦ xmlns:msxsl="urn:schemas-microsoft-com:xslt“◦ xmlns:cs="urn:custom-csharp“

<msxsl:script language="C#" implements-prefix="cs">

<![CDATA[ public string FormatPrice(int price) { return string.Format("{0:c0}", price); } ]]> </msxsl:script>

Creating Word Documents:Using XSLT

Page 31: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Demo Implementing C# Support to XSLT

Review WordController.ProductXsltWithScript Controller Action.

Creating Word Documents:Using XSLT

Page 32: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Adding Header and Footer◦ Create document with header and footer◦ Rename .docx to .zip◦ Open zip file and get the word/header.xml file and

word/footer.xml file◦ Rename with .xslt extension◦ Replace:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>◦ With:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match=“/">◦ Add to the end of the file:

</xsl:template> </xsl:stylesheet>

Creating Word Documents:Using XSLT

Page 33: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Demo Locating the Header and Footer

Review WordController.ProductXsltWithHeaderFooter Controller Action.

Creating Word Documents:Using XSLT

Page 34: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Combining XSLT and Bookmarks Adding Image

Review WordController.ProductXsltAddImage Controller Action.

Creating Word Documents:Using XSLT

Page 35: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Open XML SDK 2.0 Productivity Tool for MS Office

Using the SDK Using SpreadsheetLight

Creating Excel Documents: Options

Page 36: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Why? Learn how the SDK works.

Creating Excel Documents: “Open XML SDK 2.0 Productivity Tool”

Page 37: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Create and Save Excel Document Open the Excel Document using “Open XML

SDK 2.0 Productivity Tool” In “Document Explorer”, right-click on root

and select “Reflect Code”. Create console app in Visual Studio. Add

references to required assemblies. Create new class for the “generated code”. Instantiate class and call CreatePackage

from Main.

Creating Excel Documents: “Open XML SDK 2.0 Productivity Tool”

Page 38: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Code Project article: Creating basic Excel workbook with Open XML by Mika Wendelius

Extended Excel.cs to:◦ CreateWorkbook using a Stream◦ Add additional styles for headers

Creating Excel Documents:Using the SDK

Page 39: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Demo Using SDK

Review ExcelController.ProductWidgetsUsingSdk Controller Action.

Creating Excel Documents:Using the SDK

Page 40: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

http://spreadsheetlight.com/ “SpreadsheetLight is an open source

spreadsheet library/component for .NET Framework written in C#. It is freely available and uses the MIT License. It generates Open XML spreadsheets that are compatible with Microsoft Excel 2007/2010/2013 (and even LibreOffice Calc).”

Creating Excel Documents:Using SpreadsheetLight

Page 41: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Features used:◦ Setting Document Properties◦ Rename Worksheet◦ Set Column Heading Styles◦ Set Column Widths◦ Format Currency Cells◦ Define Formulas◦ Create Charts◦ Protect Worksheet

Creating Excel Documents:Using SpreadsheetLight

Page 42: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Demo using SpreadsheetLight

Review ExcelController.ProductWidgetsSpreadsheetLight Controller Action.

Review ExcelController.SalesByYear Controller Action.

Creating Excel Documents:Using SpreadsheetLight

Page 43: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Adding a Chart◦ SLChart

CreateChart – define the range for the data to be used.

SetChartType – set the type of chart to create SetChartPosition – where the chart will appear in the

worksheet SetChartStyle – style for the chart. SLDocument.InsertChart – insert the chart into the

current worksheet.

Creating Excel Documents:Using SpreadsheetLight

Page 44: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Demo creating Chart

Review ExcelController.SalesByYearWithChart Controller Action.

Creating Excel Documents:Using SpreadsheetLight

Page 45: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Where can I find the presentation and source code?◦ Email me at: [email protected]◦ Come to the front and add your name and email

address to the list.◦ www.aspnetwiki.com. Will be posted this

weekend.

Presentation and Source Code

Page 46: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Questions

Page 47: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

Creating MVC Extensions for the jQuery UI Library that can be used with the Razor and ASPX (WebForms) View Engines.

Creating mobile application games using the “Crafty” JavaScript Gaming Engine and PhoneGap.

Future Presentations

Page 48: Learn How to Create Word and Excel Documents.  John DeVight  Herndon, Virginia  Senior Principal Software Engineer at ManTech  Telerik MVP  .

John DeVight www.aspnetwiki.com [email protected]

Thank You