Monday, June 6, 2011
Create Open XML Spreadsheet in F# (update)
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")
Wednesday, May 25, 2011
Create Open XML Spreadsheet in F#
Tuesday, May 24, 2011
Create Open XML Word document in F#
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
Full name: Snippet.printXml
type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Full name: Snippet.createBody
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
Full name: Microsoft.FSharp.Core.Operators.ignore
Full name: Snippet.createDocument
Full name: Snippet.createWordprocessingDocument
type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
Full name: Microsoft.FSharp.Core.Operators.using
type: 'a
implements: System.IDisposable
Full name: Snippet.result3
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"));
}
}
Monday, September 29, 2008
F# Winform
When you want to make a F# winform application and you don’t want to open an extra console when you start the application.
How can you do this (F# version 1.9.6.2)?
1. Create a F# application:
[Edit- thanks Brian ]
2. Open the project properties
3. In the application tab change the output type
4. Add reference to the System.Windows.Forms
5. Add your code.
6. Run your app
7. Close.
8. Add your own details.
Monday, September 22, 2008
Creating a parser with F#
I want to write my own html-parser. That is one of the reasons I am learning F#. I have read all the documentation that is available in the three published F# books and tried to build some simple parsers myself and downloaded the examples of Chris Smith.
This resulted in a naïve development approach: start with lexer, than create the parser, compose the AST and finally build your application that consumes the AST.
One of the lessons learned: when you want to create a parser you have to start with analyses of the text type. In case you can step through the text and can determine the action after each token a lexer is what you need. In case the action is determined by server tokens you need a parser.
Example of program that needs a lexer:
- A program that replacing digits by roman numerals.
- A simple encryptor
Example of program that needs a lexer and a parser:
- Xml-parser
- Code