Tuesday, June 7, 2011

Create Open XML Word document in F# (update)

This time I will create a word document (open xml) in F# without using the Open XML SDK and using XML libraries. Things have been intentionally kept simple.

I start by modelling a simple word document:

There are Two types of paragraphs: normal and bold ones:
  1. //reference to the package
  2. #r "WindowsBase"
  3.  
  4. open System
  5. open System.IO
  6. open System.IO.Packaging
  7.  
  8. type Paragraph =
  9.     | Normal of string
  10.     | Bold of string
 
And a document is a list of paragraphs:
  1. type Document =
  2.     | Paragraphs of Paragraph list
 
This is the test:
  1. //test
  2. let list = [Normal("test1"); Normal("test2"); Bold("test3"); Normal("test4")]
  3. let doc = Paragraphs(list)

 

Next we have the to create an xml version of document:






  1. //helpers

  2.  

  3. // http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx

  4. let encodedXml (text: string) =

  5.     text.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;")

  6.  

  7. let xml1 = "<node>it's my \"node\" & i like it<node>"

  8. let encode = encodedXml xml1

  9.  

  10. let addString acc item = acc + "\r\t" + item

  11.  

  12. let createParagraphNormal text =

  13.     "<w:p>

  14.       <w:r>

  15.         <w:t>" + encodedXml text +

  16.         "</w:t>

  17.       </w:r>

  18.     </w:p>"

  19.  

  20. let createParagraphBold text =

  21.     "<w:p>

  22.       <w:r>

  23.         <w:rPr>

  24.          <w:b />

  25.         </w:rPr>

  26.         <w:t>" + encodedXml text +

  27.         "</w:t>

  28.       </w:r>

  29.     </w:p>"

  30.  

  31. let createParagraph = function

  32.     | Normal(text) -> createParagraphNormal text

  33.     | Bold (text) -> createParagraphBold text

  34.  

  35. let createParagraphs = function

  36.     | Paragraphs(list) -> list|> List.map createParagraph |> List.reduce addString

  37.  

  38. //test

  39. let p = createParagraphs doc

  40.  

  41. let createDocument doc =

  42.     let startDocument = @"<?xml version=""1.0"" encoding=""utf-8""?>

  43.                     <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">

  44.                       <w:body>"

  45.     let endDocument = "  </w:body>

  46.                         </w:document>"

  47.     let content = createParagraphs doc

  48.     startDocument + content + endDocument





 

Then we have to create a package, add the document and save it to disk:






  1. //add the document to package and save

  2. let createFile doc (fileName:string) =

  3.     using (Package.Open(fileName, FileMode.Create, FileAccess.ReadWrite))(fun package ->

  4.         let uri = new Uri("/word/document.xml", UriKind.Relative )

  5.         let partDocumentXML =

  6.             package.CreatePart(

  7.                                 uri,

  8.                                 "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" )

  9.     

  10.         using(new StreamWriter(partDocumentXML.GetStream(FileMode.Create, FileAccess.Write)))(fun stream ->

  11.             doc |> createDocument |> stream.Write

  12.         )

  13.  

  14.         package.CreateRelationship(

  15.                                     uri,

  16.                                     TargetMode.Internal,

  17.                                     "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",

  18.                                     "rId1") |> ignore

  19.     )

  20.  

  21. //test

  22. let fileName = @"D:\Tmp\test.docx"

  23.  

  24. createFile doc fileName;;




 
This is the result:

 

I have created a map from twitter to my document model






  1. type UserStatus =

  2.     { UserName : string;

  3.       ProfileImage : string;

  4.       Status : string;

  5.       StatusDate : DateTime }

  6.  

  7. let mapUserStatus (us:UserStatus) =

  8.     let date = "Date: " + us.StatusDate.ToShortDateString()  + " time: " + us.StatusDate.ToLongTimeString()

  9.     [Normal(date); Bold(us.UserName); Normal(us.Status); Normal("")]

  10.  

  11. let addUserStatus acc item = item @ acc

  12.  

  13. let result = xml|> parseTweetXml |> List.map mapUserStatus|> List.reduce addUserStatus





 


This results in the following document:









Remark: I had to Google “Weiner” to understand the jokes.

Monday, June 6, 2011

Create Open XML Spreadsheet in F# (update)

I have updated the code snippet for the creation of a spreadsheet in F#. I rewritten it in a more functional way an I have added an test with data.

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.Packaging
 8: open DocumentFormat.OpenXml.Spreadsheet
 9:
10: let createSpreadsheet (filepath:string) (sheetName:string) (sheetData:SheetData) =
11:     // Create a spreadsheet document by supplying the filepath.
12:     // By default, AutoSave = true, Editable = true, and Type = xlsx.
13:    
14:     using (SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook)) (fun spreadsheetDocument ->
15:
16:     // Add a WorkbookPart to the document.
17:     let workbookPart = spreadsheetDocument.AddWorkbookPart(Workbook = new Workbook())
18:
19:     // Add a WorksheetPart to the WorkbookPart.
20:     // http://stackoverflow.com/questions/5702939/unable-to-append-a-sheet-using-openxml-with-f-fsharp
21:     let worksheetPart = workbookPart.AddNewPart<WorksheetPart>()
22:     
23:     worksheetPart.Worksheet <- new Worksheet(sheetData:> OpenXmlElement)
24:
25:     // Add Sheets to the Workbook.
26:     let sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets())
27:
28:     // Append a new worksheet and associate it with the workbook.
29:     let sheet = new Sheet(  Id =  StringValue(spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart)),
30:                             SheetId =  UInt32Value(1u),
31:                             Name = StringValue(sheetName)
32:                             )
33:     [sheet :> OpenXmlElement] |> sheets.Append
34:     )
35:
36: //helpers
37: let createCellReference (header:string) (index:int) =
38:     StringValue(header + string(index))
39:
40: let createNumberCell number (header:string) (index:int) =
41:     let cell = new Cell(DataType = EnumValue(CellValues.Number), CellReference = createCellReference header index)
42:     let value = new CellValue(Text = number.ToString())
43:     value |> cell.AppendChild|> ignore
44:     cell :> OpenXmlElement
45:
46: let createTextCell text (header:string) (index:int) =
47:     let cell = new Cell(DataType = EnumValue(CellValues.InlineString), CellReference = createCellReference header index)
48:     let inlineString = new InlineString()
49:     let t = new Text(Text = text)
50:     t |> inlineString.AppendChild |> ignore
51:     inlineString |> cell.AppendChild|> ignore
52:     cell :> OpenXmlElement
53:
54: let createContentRow (text, (number1:int), (number2:int), (index:int)) =
55:     let row = new Row(RowIndex = UInt32Value(uint32(index)))
56:     let cell1 = createTextCell text "A" index
57:     let cell2 = createNumberCell number1 "B" index
58:     let cell3 = createNumberCell number2 "C" index
59:     cell1 |> row.Append
60:     cell2 |> row.Append
61:     cell3 |> row.Append
62:     row :> OpenXmlElement
63:
64: //test
65: let createTestSheetData =
66:     let sheetData = new SheetData()
67:     ("test1", 123, 456, 1) |> createContentRow |> sheetData.AppendChild |> ignore
68:     ("test2", 35, 1231, 2) |> createContentRow |> sheetData.AppendChild |> ignore
69:     ("test3", 345, 21, 3) |> createContentRow |> sheetData.AppendChild |> ignore
70:     sheetData
71:
72: let testData = createTestSheetData
73: let result = createSpreadsheet @"D:\Tmp\test.xlsx" "test" testData;;
74:
75:

Tuesday, May 31, 2011

Create in F# XDocument with XDeclaration and namespace

In this example I will create the following XML in F#:
<?xml version="1.0" encoding="utf-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:body>
    <w:p>
      <w:r>
        <w:t>This is a test</w:t>
      </w:r>
    </w:p>
  </w:body>
</w:document>
 
Transferring C#-code is not straightforward because the constructor of XDocument does not accept the following code: new  XDocument(new XDeclaration("1.0""utf-8""true"), new XElement("document"))

//First create some helpers:

let xnameNs str ns = XName.Get(str, ns)
let xname str =
    xnameNs str "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
 
let ns = XNamespace.Get("http://schemas.openxmlformats.org/wordprocessingml/2006/main")
 
let dec = new XDeclaration("1.0""utf-8""true")
 
let el =
    new XElement(xname "document",
        new XAttribute(XNamespace.Xmlns + "w", ns),
            new XElement(xname "body",
                new XElement(xname "p",
                    new XElement(xname "r",
                        new XElement(xname "t""This is a test")
                        ))))
 
//Add them together:
 
let document = 
    let doc =new XDocument(dec)
    doc.AddFirst(el)
    doc

//And test:
 
document.Save(@"D:\Tmp\test1.xml")

Total Pageviews