assembling documents on sharepoint 2010 sites by merging content from excel

Upload: sreedhar-konduru

Post on 04-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    1/21

    Assembling Documents on SharePoint 2010 Sites byMerging Content from Excel, PowerPoint, and WordSummary: Combining data and images from various Microsoft Office programs to create a singledocument is a frequently requested scenario. Doing this from documents stored in SharePoint Foundation2010 has several advantages. Learn to create Word documents in a document library by mergingcomponents from Excel, PowerPoint, and other Word documents. (25 printed pages)

    Applies to: Microsoft Excel 2010 | Microsoft Office 2010 | Microsoft PowerPoint 2010 | Microsoft Word2010 | Microsoft SharePoint Server 2010Published: March 2010Provided by: Zeyad Rajabi , Microsoft Corporation | Frank Rice , Microsoft Corporation

    Contents

    Document Assembly with Office 2010 and SharePoint 2010

    Scenario Overview

    Solution Overview

    o Step 1 Create the Template

    o Step 2 Set Up a Document Set in SharePoint 2010

    o Step 3 Create a Web Part with an Assemble Documents Command

    o Step 4 Find the Content Controls

    o Step 5 Merge the Content Together

    o Step 6 Offer the Assembled Document to the User

    o Step 7 - Sign the Project

    Results

    Conclusion

    Additional Resources

    Download code

    Document Assembly with Office 2010 and SharePoint 2010

    http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspxhttp://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspxhttp://blogs.msdn.com/user/Profile.aspx?UserID=3233http://blogs.msdn.com/user/Profile.aspx?UserID=3233http://blogs.msdn.com/user/Profile.aspx?UserID=3233http://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_DocumentAssemblyhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_DocumentAssemblyhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_TheScenariohttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_TheScenariohttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_TheSolutionhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_TheSolutionhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_CreatetheTemplatehttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_CreatetheTemplatehttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_CreatetheTemplatehttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_CreatetheTemplatehttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_SetUpDocumentSetSharePoint2010http://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_SetUpDocumentSetSharePoint2010http://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_SetUpDocumentSetSharePoint2010http://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_SetUpDocumentSetSharePoint2010http://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_CreateWebPartwithAssembleDocumentsCommandhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_CreateWebPartwithAssembleDocumentsCommandhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_CreateWebPartwithAssembleDocumentsCommandhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_CreateWebPartwithAssembleDocumentsCommandhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_FindContentControlshttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_FindContentControlshttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_FindContentControlshttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_FindContentControlshttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_MergeContentTogetherhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_MergeContentTogetherhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_MergeContentTogetherhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_MergeContentTogetherhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_OfferAssembledDocumenttoUserhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_OfferAssembledDocumenttoUserhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_OfferAssembledDocumenttoUserhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_OfferAssembledDocumenttoUserhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_SigntheProjecthttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_SigntheProjecthttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_TheResultshttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_TheResultshttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_Conclusionhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_Conclusionhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_AdditionalResourceshttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_AdditionalResourceshttp://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=odcoffice14ta&ReleaseId=4029http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=odcoffice14ta&ReleaseId=4029http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=odcoffice14ta&ReleaseId=4029http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=odcoffice14ta&ReleaseId=4029http://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_AdditionalResourceshttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_Conclusionhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_TheResultshttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_SigntheProjecthttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_OfferAssembledDocumenttoUserhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_MergeContentTogetherhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_FindContentControlshttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_CreateWebPartwithAssembleDocumentsCommandhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_SetUpDocumentSetSharePoint2010http://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_CreatetheTemplatehttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_TheSolutionhttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_TheScenariohttp://msdn.microsoft.com/en-us/library/ff519529.aspx#odc_office2010_DocumentAssemblyinSharePoint2010_DocumentAssemblyhttp://blogs.msdn.com/user/Profile.aspx?UserID=3233http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspx
  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    2/21

    Document assembly seems to be a hot topic these days especially when you combine it with the power ofMicrosoft SharePoint Server 2010. Prior to the 2007 Microsoft Office system, accessing text, tables,images, and other information to create documents in Microsoft Word, for example, relied on COMAutomation to access each program contributing to the merge. Automation has its share of issues such asinterruptions by pop-up dialog boxes, timing constraints, inability to scale out, and other limitations.Additionally, running Microsoft Office solutions on the server is not recommended or supported byMicrosoft.

    With the release of 2007 Microsoft Office system, the Open XML file formats present documents as acomposite of parts and relationships. For example, there are image parts, document parts, and chartsparts. This enables you to access and create documents and their component parts without usingAutomation. In addition, the Open XML SDK 2.0 for Microsoft Office enables you to create and manipulatedocuments, and expose data and other components programmatically, without using Microsoft OfficeAutomation. And because Automation is no longer required to work with Microsoft Office documents,you can now perform merges and other actions directly on the server.

    Likewise, SharePoint Server 2010 is a great a tool for storing, sharing, and controlling documents and

    providing functionality to work with documents. For example, the Document Center in SharePoint Server2010 is an area where you can collaborate and store documents in document libraries. Document librariesare containers that are already configured to use workflow, version history, and other features that areimportant for working with documents at the enterprise level.

    This article describes a rich document assembly solution which takes Microsoft Word 2010 documents,Microsoft Excel 2010 documents, and Microsoft PowerPoint 2010 documents and merges them togetherto form a final report in Word. It does all of this from a SharePoint 2010 Web Part.

    You can download the solution project from the Code Gallery .

    Scenario OverviewImagine a scenario where you work for a company that analyzes stocks and generates reports for everycompany and stock analyzed. These reports are typically very rich and usually involve more than oneperson contributing to the content. Content is separated into multiple Word documents, Excel documents,and PowerPoint documents where each document is assigned to a person. After all the content is written,the content is assembled into a final report as a Word document. The company asks you to write asolution that merges all these documents programmatically.

    Solution Overview

    This solution uses document sets , a feature in SharePoint 2010. Document sets enables you to managecollection of documents as single objects. Think of this feature as a binder of related content.

    In this solution, a custom document set includes a set of files (six in this example) that correspond to thevarious components of the final analysis report. Figure 1 shows a document set for a company calledContoso.

    http://msdn.microsoft.com/en-us/library/bb448854.aspxhttp://msdn.microsoft.com/en-us/library/bb448854.aspxhttp://msdn.microsoft.com/en-us/library/bb448854.aspxhttps://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=odcoffice14ta&ReleaseId=4029https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=odcoffice14ta&ReleaseId=4029https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=odcoffice14ta&ReleaseId=4029https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=odcoffice14ta&ReleaseId=4029http://msdn.microsoft.com/en-us/library/bb448854.aspx
  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    3/21

    Figure 1. Document set for the Contoso company sample

    Based on this scenarios, by using document sets, you follow these steps:

    1. Create a set of documents that represent the document set for this solution. One of thedocuments is a Word template that represents the appearance of the final report.

    2. Create and add a custom document set to a document library.

    3. Create a Web Part for the document library with a button labeled Assemble Documents . Thebutton merges all the content together into a final document. Add this Web Part to the documentset library.

    4. Using the Open XML SDK 2.0, open the template document from the document set and search forall content controls.

    5. For every content control you find, find the corresponding document content in the library andmerge that content into the final document.

    6. After the document assembly is complete, offer the user the ability to open or save the report.

    Step 1 Create the Template

    Setting up the correct template makes all the difference when you are creating Office document solutions.In this solution, you have a document set with six files, where one of the files is a template file. Thetemplate document represents the look of the final report. Then you use content controls within thetemplate to specify semantic regions within the document to be used to merge content together.

  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    4/21

    To make the process easier, the titles of the content controls represent the types of content to be merged.For example, a Word document, a chart from a spreadsheet, a table from a spreadsheet, and a SmartArtgraphic from a presentation. The content of a content control represents the name of the file that containsthe content to be merged. For example, Figure 2 shows the template document highlighting one of thecontent controls labeled "Word:Document" with the content set to "Introduction".

    Figure 2. Content control for information from the Word document

    This content control represents the region where you merge the Contoso - Introduction document (fromWord) into the template file. For the sake of completeness, the other content controls are labeledSpreadsheet:Chart, Spreadsheet:Table, and Presentation:SmartArt.

    Step 2 Set Up a Document Set in SharePoint 2010

  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    5/21

    Next, you must create document sets for the six documents on the SharePoint 2010 site.

    To create document sets

    1. Create your document library first. In the navigation pane, click Libraries and then click Create .

    2.

    In the Create dialog, click Document Library , type a name for the library ( Doc Set AssemblyDemo ) and then click OK .3. Next create your content type. Click Site Actions , click Site Settings , and then in the Galleries

    section, click Site content types .4. Click Create and then type a name for the content type.5. In the Select parent content type from list, select Document Set Content Types and then click

    OK .6. Now create the document set. In the Site Content Types dialog, in the Settings section, click

    Document Set settings .7. In the Create dialog, click Document Library , type a name for the library ( Doc Set Assembly

    Demo ) and then click OK .8. In the Default Content section, browse to the first document ( Analysis.docx ), select it, and then

    click Open .9. Now click Add new default content , browse to the second document ( Introduction.docx ), and

    then click Open .10. Repeat this for the remaining four documents and when you are finished, click OK .11. Click back to the document library and then under Library Tools , click Library , and then click

    Library Settings .12. In the Document Library Setting dialog under General Settings , click Advanced Settings .13. In the Content Types section, click Yes for Allow management of content types and then click

    OK .14. Back in the Document Library Setting dialog under Content Types , click Add from existing

    site content types .

    15. In the Available Site Content Types list, select your content type and then click Add .16. Add the new document set to your library. Click back to the document library and under LibraryTools , click Documents .

    17. Click the New Document list and select your content type, type a name for the new documentset, and then click OK .

    At this point, you should have a library set up with a document set content type displaying the sixdocuments that use the document set name prefix (see Figure 1).

    Step 3 Create a Web Part with an Assemble Documents Command

    To make this solution usable, you must add a command within the document library that lets you mergedocuments together. The easiest way to perform this task is to create a Web Part within the MicrosoftVisual Studio 2010 project.

    To create the Web Part project in Visual Studio 2010

    1. Start Visual Studio 2010.2. On the File menu, click New , and then Project .

  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    6/21

    3. Under Installed Templates , click SharePoint , 2010 , and then Visual Web Part .4. In the Name box, type DocumentAssembly , and then click OK .5. In the SharePoint Configuration Wizard page, select your Web application, and then click

    Finish .6. After you create the Web Part, open the VisualWebPart1.cs file and add the following references

    to the existing references at the top of the file.

    VB

    C#

    C++

    F#

    JScript

    Copy

    using System.Linq;using System.IO;using DocumentFormat.OpenXml.Packaging;using Word = DocumentFormat.OpenXml.Wordprocessing;using Draw = DocumentFormat.OpenXml.Drawing;using DocumentFormat.OpenXml.Drawing.Charts;using WP = DocumentFormat.OpenXml.Drawing.Wordprocessing;using DocumentFormat.OpenXml.Drawing.Spreadsheet;using Excel = DocumentFormat.OpenXml.Spreadsheet;using DocumentFormat.OpenXml;using PPT = DocumentFormat.OpenXml.Presentation;using Dgm = DocumentFormat.OpenXml.Drawing.Diagrams;

    7. Next, modify the CreateChildControls method as follows.

    VB

    C#

    C++

    F#

    JScript

    Copy

    protected override void CreateChildControls(){

    http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');
  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    7/21

    Control control = this .Page.LoadControl(_ascxPath);Controls.Add(control);base .CreateChildControls();Button btnSubmit = new Button();btnSubmit.Text = "Assemble Documents" ;btnSubmit.Click += new EventHandler(btnSubmit_Click);

    Controls.Add(btnSubmit);}

    This method adds a new button control where you add logic to merge the documents that arecontained in a given document set (the merge code is called from the btnSubmit_Click event).

    When you create the Web Part, add the button to the document set. The easiest way to do this task is touse Microsoft SharePoint Designer 2010.

    To add the custom Web Part to the document set library

    1. From SharePoint Designer 2010, open the SharePoint site.2. Navigate to the document library that contains the document set.3. Click All Files , Custom document library name , Forms , and then Document set name . Type

    docsethomepage.aspx .4. Click the bottom of WebPartZone_CenterMain .5. Next, click Insert , Web Part , and then select your custom Web Part.

    Figure 3. Adding the Web Part in SharePoint Designer 2010

  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    8/21

    6. Save and close SharePoint Designer 2010.

    At this point, you should see an Assemble Documents command displayed for any document set asshown in Figure 4.

    Figure 4. Assemble Documents button added to the document set

  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    9/21

    Step 4 Find the Content Controls

    Finding content controls in a document involves the following steps:

    1. Open template document from the SharePoint site.

    2. Load the document into memory.

    3. Open the document (from memory) with the Open XML SDK 2.0.

    4. Loop through all content controls within the document.

    5. For every content control, determine the title.

    6. Based on the title, perform a particular action.

    The following code performs these steps.

    VB C#C++ F# JScript Copy void btnSubmit_Click( object sender, EventArgs e)

    http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');
  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    10/21

    {SPFolder folder = SPContext.Current.ListItem.Folder;char [] splitter = { '/' };string [] folderName = folder.Name.Split(splitter);string filePrefix = @"Stock Analysis Demo/" + folderName[0] + "/" + folderName[0];

    SPFile template = folder.Files[filePrefix + " - Template.docx" ];SPFile file;byte [] byteArray = template.OpenBinary();

    using (MemoryStream mem = new MemoryStream()){

    mem.Write(byteArray, 0, ( int )byteArray.Length);

    using (WordprocessingDocument myDoc = WordprocessingDocument.Open(mem, true )){

    MainDocumentPart mainPart = myDoc.MainDocumentPart;

    foreach (Word.SdtElement sdt in mainPart.Document

    .Descendants().ToList()){Word.Alias alias = sdt.Descendants().FirstOrDefault();if (alias != null ){

    string sdtTitle = alias.Val.Value;

    if (sdtTitle == "Spreadsheet:Table" ){

    file = folder.Files[filePrefix + " - " + sdt.InnerText + ".xlsx" ];ImportTableFromSpreadsheet(mainPart, sdt, file);

    }else if (sdtTitle == "Spreadsheet:Chart" ){

    file = folder.Files[filePrefix + " - " + sdt.InnerText + ".xlsx" ];ImportChartFromSpreadsheet(mainPart, sdt, file);

    }else if (sdtTitle == "Presentation:SmartArt" ){

    file = folder.Files[filePrefix + " - " + sdt.InnerText + ".pptx" ];ImportSmartArtFromPowerPoint(mainPart, sdt, file);

    }else if (sdtTitle == "Word:Document" ){

    file = folder.Files[filePrefix + " - " + sdt.InnerText + ".docx" ];AddAltChunk(mainPart, sdt, file);

    }}

    }}

    // For the remainder of the code in this procedure, see step 6.

    }}

  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    11/21

    Step 5 Merge the Content Together

    In this solution, there are four kinds of content to be merged:

    Text from a Word document

    SmartArt graphics from a PowerPoint presentation

    Charts from an Excel spreadsheet

    Tables of data from an Excel spreadsheet

    The following sections describe these steps.

    Step 5a Merge the Word Documents Together

    By far, the easiest way to assemble Word documents is to take advantage of the altChunks object. Youcan find more information about document assembly with altChunks object in the article CreatingDocuments by Using the Open XML Format SDK 2.0 (Part 3 of 3)

    Here is the code to merge documents together in SharePoint Server 2010.

    VB C#C++ F# JScript Copy

    protected int id = 1;

    void AddAltChunk(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile filename){

    string altChunkId = "AltChunkId" + id;id++;byte [] byteArray = filename.OpenBinary();

    AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);

    using (MemoryStream mem = new MemoryStream()){

    mem.Write(byteArray, 0, ( int )byteArray.Length);mem.Seek(0, SeekOrigin.Begin);chunk.FeedData(mem);

    }

    Word.AltChunk altChunk = new Word.AltChunk();altChunk.Id = altChunkId;

    // Replace content control with altChunk information.OpenXmlElement parent = sdt.Parent;

    http://msdn.microsoft.com/en-us/library/dd469465.aspxhttp://msdn.microsoft.com/en-us/library/dd469465.aspxhttp://msdn.microsoft.com/en-us/library/dd469465.aspxhttp://msdn.microsoft.com/en-us/library/dd469465.aspxhttp://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://msdn.microsoft.com/en-us/library/dd469465.aspxhttp://msdn.microsoft.com/en-us/library/dd469465.aspx
  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    12/21

    parent.InsertAfter(altChunk, sdt);sdt.Remove();

    }

    Step 5b Import SmartArt Graphics from PowerPoint to Word

    The next step is to import the SmartArt from PowerPoint to Word. You can find more information aboutthis in the blog post Importing SmartArt from PowerPoint to Word .

    Here is the code that you need to do this task.

    Copy void ImportSmartArtFromPowerPoint(MainDocumentPart mainPart, Word.SdtElement sdt,SPFile filename){

    string docLayoutPartId = "";string docDataPartId = "";string docColorsPartId = "";string docStylePartId = "";

    byte[] byteArray = filename.OpenBinary();

    using (MemoryStream mem = new MemoryStream()){

    mem.Write(byteArray, 0, (int)byteArray.Length);

    using (PresentationDocument myPres = PresentationDocument.Open(mem, true)){

    PresentationPart presPart = myPres.PresentationPart;// Get the slide that contains the SmartArt graphic.SlidePart slide = (SlidePart)presPart.GetPartById("rId3");// Get all the appropriate parts associated with the SmartArt.DiagramLayoutDefinitionPart layoutPart =

    slide.DiagramLayoutDefinitionParts.First();DiagramDataPart dataPart = slide.DiagramDataParts.First();DiagramColorsPart colorsPart = slide.DiagramColorsParts.First();DiagramStylePart stylePart = slide.DiagramStyleParts.First();

    // Get some of the appropriate properties off the SmartArt graphic.PPT.GraphicFrame graphicFrame =

    slide.Slide.Descendants().First();PPT.NonVisualDrawingProperties drawingPr = graphicFrame

    .Descendants().First();Draw.Extents extents =

    graphicFrame.Descendants().First();

    // Import SmartArt into the Word document.// Add the SmartArt parts to the Word document.DiagramLayoutDefinitionPart docLayoutPart =

    mainPart.AddPart(layoutPart);DiagramDataPart docDataPart =

    mainPart.AddPart(dataPart);

    http://blogs.msdn.com/brian_jones/archive/2009/06/04/importing-smartart-from-powerpoint-to-word.aspxhttp://blogs.msdn.com/brian_jones/archive/2009/06/04/importing-smartart-from-powerpoint-to-word.aspxhttp://blogs.msdn.com/brian_jones/archive/2009/06/04/importing-smartart-from-powerpoint-to-word.aspxhttp://blogs.msdn.com/brian_jones/archive/2009/06/04/importing-smartart-from-powerpoint-to-word.aspx
  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    13/21

    DiagramColorsPart docColorsPart =mainPart.AddPart(colorsPart);

    DiagramStylePart docStylePart =mainPart.AddPart(stylePart);

    // Get all the relationship ids of the added parts.docLayoutPartId = mainPart.GetIdOfPart(docLayoutPart);

    docDataPartId = mainPart.GetIdOfPart(docDataPart);docColorsPartId = mainPart.GetIdOfPart(docColorsPart);docStylePartId = mainPart.GetIdOfPart(docStylePart);

    // Use the document reflector to figure out how to add a SmartArt// graphic to Word.// Change attribute values based on specifics related to the SmartArt.Word.Paragraph p = new Word.Paragraph(

    new Word.Run(new Word.Drawing(new WP.Inline(new WP.Extent() { Cx = extents.Cx, Cy = extents.Cy },new WP.EffectExtent()

    { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },new WP.DocProperties() {Id = drawingPr.Id, Name = drawingPr.Name },new WP.NonVisualGraphicFrameDrawingProperties(),new Draw.Graphic(new Draw.GraphicData(new Dgm.RelationshipIds() { DataPart = docDataPartId,

    LayoutPart = docLayoutPartId, StylePart = docStylePartId,ColorPart = docColorsPartId })

    { Uri = "http://schemas.openxmlformats.org/drawingml/2006/diagram" })){ DistanceFromTop = (UInt32Value)0U, DistanceFromBottom =

    (UInt32Value)0U,DistanceFromLeft = (UInt32Value)0U, DistanceFromRight =

    (UInt32Value)0U})));

    // Swap out the content control for the SmartArt.OpenXmlElement parent = sdt.Parent;

    parent.InsertAfter(p, sdt);sdt.Remove();

    }}

    }

    Step 5c Import the Chart from Excel to Word

    In this step, you import a chart from an Excel spreadsheet to the Word template. You can find moreinformation about doing this in the blog post Importing Charts from Spreadsheets to WordprocessingDocuments .

    Here is the same code as seen in the blog post modified to work with files that exist within SharePoint2010.

    VB C#

    http://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspxhttp://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspxhttp://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspxhttp://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspxhttp://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspxhttp://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspx
  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    14/21

    C++ F# JScript Copy void ImportChartFromSpreadsheet(MainDocumentPart mainPart, Word.SdtElement sd t,

    SPFile spreadsheetFileName){

    // Create a paragraph that has an inline drawing object.Word.Paragraph p = new Word.Paragraph();Word.Run r = new Word.Run();p.Append(r);Word.Drawing drawing = new Word.Drawing();r.Append(drawing);// These dimensions work perfectly for the template document.WP.Inline inline = new WP.Inline( new WP.Extent() { Cx = 5486400, Cy = 3200400 });byte [] byteArray = spreadsheetFileName.OpenBinary();

    using (MemoryStream mem = new MemoryStream()){

    mem.Write(byteArray, 0, ( int )byteArray.Length);

    // Open the Excel spreadsheet.using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(mem, true )){

    // Get all of the appropriate parts.WorkbookPart workbookPart = mySpreadsheet.WorkbookPart;WorksheetPart worksheetPart = XLGetWorksheetPartByName(mySpreadsheet,

    "Sheet2" );DrawingsPart drawingPart = worksheetPart.DrawingsPart;ChartPart chartPart = (ChartPart)drawingPart.GetPartById( "rId1" );

    // Clone the chart part and add it to the Word document.

    ChartPart importedChartPart = mainPart.AddPart(chartPart);string relId = mainPart.GetIdOfPart(importedChartPart);

    // The frame element contains information for the chart.GraphicFrame frame =

    drawingPart.WorksheetDrawing.Descendants().First();string chartName =

    frame.NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name;// Clone this node so that you can add it to the Word document.Draw.Graphic clonedGraphic = (Draw.Graphic)frame.Graphic.CloneNode( true );ChartReference c = clonedGraphic.GraphicData.GetFirstChild();c.Id = relId;

    // Give the chart a unique ID and name.WP.DocProperties docPr = new WP.DocProperties();docPr.Name = chartName;docPr.Id = GetMaxDocPrId(mainPart) + 1;

    // Add the chart data to the inline drawing object.inline.Append(docPr, clonedGraphic);drawing.Append(inline);

    }

    http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');
  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    15/21

    }OpenXmlElement parent = sdt.Parent;parent.InsertAfter(p, sdt);sdt.Remove();

    }

    WorksheetPart XLGetWorksheetPartByName(SpreadsheetDocument document, string sheetName){

    WorkbookPart wbPart = document.WorkbookPart;

    // Find the sheet with the supplied name, and then use that Sheet object// to retrieve a reference to the appropriate worksheet.Excel.Sheet theSheet = wbPart.Workbook.Descendants()

    .Where(s => s.Name == sheetName).FirstOrDefault();

    if (theSheet == null ){

    throw new ArgumentException( "sheetName" );

    }return (WorksheetPart)(wbPart.GetPartById(theSheet.Id));

    }

    uint GetMaxDocPrId(MainDocumentPart mainPart){

    uint max = 1;

    // Get the maximum ID value of the docPr elements.foreach (WP.DocProperties docPr in

    mainPart.Document.Descendants()){

    uint id = docPr.Id;if (id > max) max = id;

    }return max;

    }

    Step 5d Import Table Data from Excel to Word

    In this step, you import a table from Excel to the template. Here are the steps that you need to do thistask.

    1. Create a Word table with the Open XML SDK 2.0 that holds the data from Excel.

    2. Open the Excel file with the Open XML SDK 2.0.

    3. Retrieve the specific sheet that contains the table of data.

    4. In the found sheet, locate the SheetData object which contains the table of data.

    5. For every row in the SheetData object, retrieve all of the cell data.

  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    16/21

    6. After all data in the row is retrieved, create a Word row with the same data.

    7. Append the created Word row to the Word table.

    To help with these tasks, you can take advantage of some Open XML SDK 2.0 code examples. For moreinformation, see the 2007 Office System Sample: Open XML Format SDK 2.0 Code Snippets for Visual Studio2008 .

    Here is the code that you need to do these tasks.

    Copy void ImportTableFromSpreadsheet(MainDocumentPart mainPart, Word.SdtElement sdt,SPFile spreadsheetFileName){

    ArrayList cellText = new ArrayList();

    // Create a Word table.Word.Table tbl = new Word.Table();Word.TableProperties tblPr = new Word.TableProperties();Word.TableStyle tblStyle = new Word.TableStyle();tblStyle.Val = "LightShading-Accent1";tblPr.AppendChild(tblStyle);

    Word.TableWidth tblW = new Word.TableWidth();tblW.Width = 5000;tblW.Type = Word.TableWidthUnitValues.Pct;tblPr.Append(tblW);tbl.AppendChild(tblPr);byte[] byteArray = spreadsheetFileName.OpenBinary();

    using (MemoryStream mem = new MemoryStream()){

    mem.Write(byteArray, 0, (int)byteArray.Length);

    using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(mem, true)){

    WorkbookPart workbookPart = mySpreadsheet.WorkbookPart;WorksheetPart worksheetPart = XLGetWorksheetPartByName(mySpreadsheet,

    "Sheet1");

    Excel.SheetData sheetData =worksheetPart.Worksheet.GetFirstChild();

    foreach (Excel.Row r in sheetData)

    {foreach (Excel.Cell c in r){

    cellText.Add(XLGetCellValue(c, workbookPart));}Word.TableRow tr = CreateRow(cellText);tbl.Append(tr);cellText = new ArrayList();

    }

    http://www.microsoft.com/downloads/details.aspx?FamilyID=78bea298-a3f9-44cf-bde0-b4f30dc986df&displaylang=enhttp://www.microsoft.com/downloads/details.aspx?FamilyID=78bea298-a3f9-44cf-bde0-b4f30dc986df&displaylang=enhttp://www.microsoft.com/downloads/details.aspx?FamilyID=78bea298-a3f9-44cf-bde0-b4f30dc986df&displaylang=enhttp://www.microsoft.com/downloads/details.aspx?FamilyID=78bea298-a3f9-44cf-bde0-b4f30dc986df&displaylang=enhttp://www.microsoft.com/downloads/details.aspx?FamilyID=78bea298-a3f9-44cf-bde0-b4f30dc986df&displaylang=enhttp://www.microsoft.com/downloads/details.aspx?FamilyID=78bea298-a3f9-44cf-bde0-b4f30dc986df&displaylang=en
  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    17/21

    }}// Swap out the content control for the SmartArt.OpenXmlElement parent = sdt.Parent;parent.InsertAfter(tbl, sdt);sdt.Remove();

    }Word.TableRow CreateRow(ArrayList cellText){

    Word.TableRow tr = new Word.TableRow();// Create cells with simple text.foreach (string s in cellText){

    Word.TableCell tc = new Word.TableCell();Word.Paragraph p = new Word.Paragraph();Word.Run r = new Word.Run();Word.Text t = new Word.Text(s);r.AppendChild(t);

    p.AppendChild(r);tc.AppendChild(p);tr.AppendChild(tc);

    }return tr;

    }

    // Get the value of a cell, given a file name, sheet name, and address name.string XLGetCellValue(Excel.Cell c, WorkbookPart wbPart){

    string value = null;// If the cell does not exist, return an empty string.if (c != null){

    value = c.InnerText;

    // If the cell represents an integer number, you are finished.// For dates, this code returns the serialized value that// represents the date. The code handles strings and Boolean values// individually. For shared strings, the code looks up the corresponding// value in the shared string table. For Boolean values, the code converts// the value into the words TRUE or FALSE.if (c.DataType != null){

    switch (c.DataType.Value){

    case Excel.CellValues.SharedString:// For shared strings, look up the value in the shared strings table.var stringTable =

    wbPart.GetPartsOfType().FirstOrDefault();// If the shared string table is missing, something is wrong.// Just return the index that you found in the cell.// Otherwise, look up the correct text in the table.if (stringTable != null){

  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    18/21

    value =stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;

    }break;

    case Excel.CellValues.Boolean:switch (value)

    { case "0":value = "FALSE";break;

    default:value = "TRUE";break;

    }break;

    }}

    }

    return value;}

    Step 6 Offer the Assembled Document to the User

    Now offer the assembled document to the user by using an Open, Save, or Cancel dialog box as shown inFigure 5.

    Figure 5. File Download dialog presented to the user

    Here is the code example to create this dialog based on a document that is in memory.

  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    19/21

    VB C#C++ F# JScript Copy void btnSubmit_Click( object sender, EventArgs e){

    // For the code that goes here, see step 4.

    HttpResponse resp = HttpContext.Current.Response;resp.ClearContent();resp.ClearHeaders();resp.AddHeader( "Content-Disposition" , "attachment; filename=Assembled

    Document.docx" );resp.ContentEncoding = System.Text.Encoding.UTF8;resp.OutputStream.Write(mem.ToArray(), 0, ( int )mem.Length);resp.Flush();resp.Close();resp.End();

    }

    Step 7 - Sign the Project

    The final step in creating the project is to sign the project with a strong-name key file, if one does notalready exist.

    To sign the project with a strong-name key file

    1. With the DocumentAssembly node selected in Solution Explorer, right-click, and then clickProperties .

    2. In the Project Designer, click the Signing tab.3. Select the Sign the assembly check box.4. Specify a new key file. In the Choose a strong name key file drop-down list, select .

    Note that new key files are always created in the .pfx format.

    The Create Strong Name Key dialog box appears.

    5. In the Create Strong Name Key dialog box, type a name and password for the new key file, andthen click OK .

    ResultsAfter putting everything together and running the code, you have a document that contains all thecontent that is contained within the library merged into a final report as shown in Figure 6.

    Figure 6. Completed document with the merged components

    http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');
  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    20/21

    Conclusion

  • 8/13/2019 Assembling Documents on SharePoint 2010 Sites by Merging Content From Excel

    21/21

    This article describes how you can combine the power of SharePoint 2010 Web Parts and the Open XMLSDK 2.0 to simplify the assembly of documents from different parts. By using content controls and alt-chunks, it is easy to insert whole sections from other documents. I encourage you to experiment morewith the techniques described here to create your own custom solutions.

    Additional ResourcesFor more information, see the following:

    Downloadable version: Open XML SDK 2.0 for Microsoft Office

    Walkthrough: Creating a Basic Web Part

    Creating Documents by Using the Open XML Format SDK 2.0 (Part 1 of 3)

    Creating Documents by Using the Open XML Format SDK 2.0 (Part 2 of 3)

    Creating Documents by Using the Open XML Format SDK 2.0 (Part 3 of 3)

    http://www.microsoft.com/downloads/details.aspx?FamilyID=c6e744e5-36e9-45f5-8d8c-331df206e0d0&DisplayLang=enhttp://www.microsoft.com/downloads/details.aspx?FamilyID=c6e744e5-36e9-45f5-8d8c-331df206e0d0&DisplayLang=enhttp://msdn.microsoft.com/en-us/library/ms452873.aspxhttp://msdn.microsoft.com/en-us/library/ms452873.aspxhttp://msdn.microsoft.com/en-us/library/dd440953.aspxhttp://msdn.microsoft.com/en-us/library/dd440953.aspxhttp://msdn.microsoft.com/en-us/library/dd452407.aspxhttp://msdn.microsoft.com/en-us/library/dd452407.aspxhttp://msdn.microsoft.com/en-us/library/dd469465.aspxhttp://msdn.microsoft.com/en-us/library/dd469465.aspxhttp://msdn.microsoft.com/en-us/library/dd469465.aspxhttp://msdn.microsoft.com/en-us/library/dd452407.aspxhttp://msdn.microsoft.com/en-us/library/dd440953.aspxhttp://msdn.microsoft.com/en-us/library/ms452873.aspxhttp://www.microsoft.com/downloads/details.aspx?FamilyID=c6e744e5-36e9-45f5-8d8c-331df206e0d0&DisplayLang=en