Tuesday, May 24, 2011

Create Open XML Word document in F#

I needed a word document. I created it with the Open XML SDK and F# and posted the snippet at fssnip.net.

1: //reference to the Open Office SDK
2: #r @"C:\Program Files (x86)\Open XML SDK\V2.0\lib\DocumentFormat.OpenXml.dll"
3: //reference to the package
4: #r "WindowsBase"
5:
6: open DocumentFormat.OpenXml
7: open DocumentFormat.OpenXml.Wordprocessing
8: open DocumentFormat.OpenXml.Packaging
9:
10: let testString = "This is a test"
11: let printXml text =
12: printfn "xml: %s" text
13:
14: let createBody (text:string) =
15: let text = new Text(text)
16: let run = new Run()
17: run.AppendChild(text) |> ignore
18: let para = new Paragraph()
19: para.AppendChild(run)|> ignore
20: let body = new Body()
21: body.AppendChild(para)|> ignore
22: body
23:
24: printXml (createBody testString).InnerXml
25:
26: let createDocument (text:string) =
27: let body = createBody text
28: let doc = new Document()
29: doc.AppendChild(body) |> ignore
30: doc
31:
32: printXml (createDocument testString).InnerXml
33:
34: let createWordprocessingDocument (filepath:string) text=
35: using (WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document)) (fun doc ->
36: let mainPart = doc.AddMainDocumentPart();
37: mainPart.Document <- createDocument text
38: )
39:
40: let result3 = createWordprocessingDocument @"D:\Tmp\test1.docx" testString
val testString : string

Full name: Snippet.testString

type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
val printXml : string -> unit

Full name: Snippet.printXml
val text : string

type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
val printfn : Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val createBody : string -> 'a

Full name: Snippet.createBody
Multiple items
val string : 'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------

type string = System.String

Full name: Microsoft.FSharp.Core.string

type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
val text : 'a
namespace Microsoft.FSharp.Text
val run : 'a
val ignore : 'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val para : 'a
val body : 'a
val createDocument : string -> 'a

Full name: Snippet.createDocument
val doc : 'a
val createWordprocessingDocument : string -> 'a -> 'b

Full name: Snippet.createWordprocessingDocument
val filepath : string

type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
val using : 'T -> ('T -> 'U) -> 'U (requires 'T :> System.IDisposable)

Full name: Microsoft.FSharp.Core.Operators.using
val doc : 'a (requires 'a :> System.IDisposable)

type: 'a
implements: System.IDisposable
val mainPart : 'a
val result3 : 'a

Full name: Snippet.result3

It is a direct transformation of the C# code from the help file of the Open XML SDK:

public static void CreateWordprocessingDocument(string filepath)
{
    // Create a document by supplying the filepath. 
    using (WordprocessingDocument wordDocument =
        WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
    {
        // Add a main document part. 
        MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
 
        // Create the document structure and add some text.
        mainPart.Document = new Document();
        Body body = mainPart.Document.AppendChild(new Body());
        Paragraph para = body.AppendChild(new Paragraph());
        Run run = para.AppendChild(new Run());
        run.AppendChild(new Text("Create text in body - CreateWordprocessingDocument"));
    }
}

2 comments:

BitWarrior said...

Will it work on mono for Linux?

BitWarrior said...
This comment has been removed by the author.

Total Pageviews