none
HelloWorld example in WorkbookML and SpreadsheetML RRS feed

  • Question

  • I want to make a simple HelloWorld example in WorkbookML and SpreadsheetML, but I am facing some trouble with getting the code neat for the latter. The trouble is nesting constructors to create the DOM neatly as with the WorkbookML example. I am using the Open XML SDK 2.0. Any suggestions?



    using DocumentFormat.OpenXml.Wordprocessing;
    
    
    
    using DocumentFormat.OpenXml.Packaging;
    
    
    
    using DocumentFormat.OpenXml;
    
    
    
    using DocumentFormat.OpenXml.Spreadsheet;
    
    
    
    namespace Word_Excel_Dyn
    
    
    
    {
    
    
    
    class Program
    
    
    
    {
    
    
    
    static void Main(string[] args)
    
    
    
    {
    
    
    
    Program p = new Program();
    
    
    
    p.HelloWorldDocx("Hi_World.docx");
    
    
    
    p.HelloWorldXlsx("Hi_World.xlsx");
    
    
    
    }
    
    
    
    public void HelloWorldDocx(string docName)
    
    
    
    {
    
    
    
    // Create a Wordprocessing document. 
    
    
    
    using (WordprocessingDocument package = WordprocessingDocument.Create(docName, WordprocessingDocumentType.Document))
    
    
    
    {
    
    
    
    // Add a new main document part. 
    
    
    
    package.AddMainDocumentPart();
    
    
    
    // Create the Document DOM. 
    
    
    
    package.MainDocumentPart.Document =
    
    
    
    new Document(
    
    
    
    new Body(
    
    
    
    new Paragraph(
    
    
    
    new DocumentFormat.OpenXml.Wordprocessing.Run(
    
    
    
    new DocumentFormat.OpenXml.Wordprocessing.Text("Hello World!")))));
    
    
    
    // Save changes to the main document part. 
    
    
    
    package.MainDocumentPart.Document.Save();
    
    
    
    }
    
    
    
    }
    
    
    
    public void HelloWorldXlsx(string docName)
    
    
    
    {
    
    
    
    // Create a Wordprocessing document. 
    
    
    
    using (SpreadsheetDocument package = SpreadsheetDocument.Create(docName, SpreadsheetDocumentType.Workbook))
    
    
    
    {
    
    
    
    // Add a new workbook part. 
    
    
    
    package.AddWorkbookPart();
    
    
    
    // Create the Workbook DOM. 
    
    
    
    package.WorkbookPart.Workbook =
    
    
    
    (new Workbook
    
    
    
    (new Worksheet
    
    
    
    (new SheetData
    
    
    
    (new Row
    
    
    
    (new Cell
    
    
    
    (new InlineString
    
    
    
    (new DocumentFormat.OpenXml.Spreadsheet.Run
    
    
    
    (new DocumentFormat.OpenXml.Spreadsheet.Text("Hello World")))))))));
    
    
    
    // Save changes to the workbook part. 
    
    
    
    package.WorkbookPart.Workbook.Save();
    
    
    
    }
    
    
    
    }
    
    
    
    }
    
    
    
    }
    
    
    
    

     

     

     

    Monday, July 13, 2009 12:24 PM

Answers

  • I answered my own question - here is the code

    using System.Linq;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Spreadsheet;
    using DocumentFormat.OpenXml.Wordprocessing;
    
    namespace Word_Excel_Dyn
    {
      class Program
      {
        static void Main(string[] args)
        {
          Program p = new Program();
          p.HelloWorldDocx("Hi_World.docx");
          p.HelloWorldXlsx("Hi_World.xlsx");
        }
    
        public void HelloWorldDocx(string docName)
        {
          // Create a Wordprocessing document. 
          using (WordprocessingDocument package = WordprocessingDocument.Create(docName, WordprocessingDocumentType.Document))
          {
            // Add a new main document part. 
            package.AddMainDocumentPart();
    
            // Create the Document DOM. 
            package.MainDocumentPart.Document =
              new Document(
                new Body(
                  new Paragraph(
                    new DocumentFormat.OpenXml.Wordprocessing.Run(
                      new DocumentFormat.OpenXml.Wordprocessing.Text("Hello World!")))));
    
            // Save changes to the main document part. 
            package.MainDocumentPart.Document.Save();
          }
        }
        public void HelloWorldXlsx(string docName)
        {
          // Create a Wordprocessing document. 
          using (SpreadsheetDocument package = SpreadsheetDocument.Create(docName, SpreadsheetDocumentType.Workbook))
          {
            // Add a new workbook part. 
            package.AddWorkbookPart();
            package.WorkbookPart.Workbook = new Workbook();
    
            // Add a new worksheet part.
            package.WorkbookPart.AddNewPart<WorksheetPart>();
    
            //Create the Spreadsheet DOM.
            package.WorkbookPart.WorksheetParts.First().Worksheet =
              new Worksheet(
                new SheetData(
                  new Row(
                    new Cell(
                      new InlineString(
                        new DocumentFormat.OpenXml.Spreadsheet.Text("Hello World!"))) { DataType = CellValues.InlineString })));
    
    
            // Save changes to the spreadsheet part.
            package.WorkbookPart.WorksheetParts.First().Worksheet.Save();
    
            // create the worksheet to workbook relation
            package.WorkbookPart.Workbook.AppendChild(new Sheets());
            package.WorkbookPart.Workbook.GetFirstChild<Sheets>().AppendChild(new Sheet()
                    {
                      Id = package.WorkbookPart.GetIdOfPart(package.WorkbookPart.WorksheetParts.First()),
                      SheetId = 1,
                      Name = "Hello World!"
                    });
    
            package.WorkbookPart.Workbook.Save();
          }
        }
      }
    }
    • Marked as answer by Jakob Flygare Wednesday, July 29, 2009 12:35 PM
    Wednesday, July 29, 2009 12:32 PM

All replies

  • Hi Jakob,
    Are you talking about code convention?
    I think one convention is nesting "new" inside "new"; the other is using Append().
    btw, do you get the codes from OpenXmlDocumentExplorer?
    Thursday, July 16, 2009 4:36 AM
  • I am talking about how to make a neat example in SpreadsheetML similar to the one in WordprocessingML with nested constructors (nesting "new" inside "new").
    Monday, July 20, 2009 3:36 PM
  • I answered my own question - here is the code

    using System.Linq;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Spreadsheet;
    using DocumentFormat.OpenXml.Wordprocessing;
    
    namespace Word_Excel_Dyn
    {
      class Program
      {
        static void Main(string[] args)
        {
          Program p = new Program();
          p.HelloWorldDocx("Hi_World.docx");
          p.HelloWorldXlsx("Hi_World.xlsx");
        }
    
        public void HelloWorldDocx(string docName)
        {
          // Create a Wordprocessing document. 
          using (WordprocessingDocument package = WordprocessingDocument.Create(docName, WordprocessingDocumentType.Document))
          {
            // Add a new main document part. 
            package.AddMainDocumentPart();
    
            // Create the Document DOM. 
            package.MainDocumentPart.Document =
              new Document(
                new Body(
                  new Paragraph(
                    new DocumentFormat.OpenXml.Wordprocessing.Run(
                      new DocumentFormat.OpenXml.Wordprocessing.Text("Hello World!")))));
    
            // Save changes to the main document part. 
            package.MainDocumentPart.Document.Save();
          }
        }
        public void HelloWorldXlsx(string docName)
        {
          // Create a Wordprocessing document. 
          using (SpreadsheetDocument package = SpreadsheetDocument.Create(docName, SpreadsheetDocumentType.Workbook))
          {
            // Add a new workbook part. 
            package.AddWorkbookPart();
            package.WorkbookPart.Workbook = new Workbook();
    
            // Add a new worksheet part.
            package.WorkbookPart.AddNewPart<WorksheetPart>();
    
            //Create the Spreadsheet DOM.
            package.WorkbookPart.WorksheetParts.First().Worksheet =
              new Worksheet(
                new SheetData(
                  new Row(
                    new Cell(
                      new InlineString(
                        new DocumentFormat.OpenXml.Spreadsheet.Text("Hello World!"))) { DataType = CellValues.InlineString })));
    
    
            // Save changes to the spreadsheet part.
            package.WorkbookPart.WorksheetParts.First().Worksheet.Save();
    
            // create the worksheet to workbook relation
            package.WorkbookPart.Workbook.AppendChild(new Sheets());
            package.WorkbookPart.Workbook.GetFirstChild<Sheets>().AppendChild(new Sheet()
                    {
                      Id = package.WorkbookPart.GetIdOfPart(package.WorkbookPart.WorksheetParts.First()),
                      SheetId = 1,
                      Name = "Hello World!"
                    });
    
            package.WorkbookPart.Workbook.Save();
          }
        }
      }
    }
    • Marked as answer by Jakob Flygare Wednesday, July 29, 2009 12:35 PM
    Wednesday, July 29, 2009 12:32 PM