F# and Excel

Add a reference to the solution:
Project menu > Add Reference. On the COM tab, locate Microsoft Excel 16.0 Object Library. That’s the reference which contains Microsoft.Office.Interop.Excel.dll.
Also, add a reference to the office library Microsoft Office 16.0 Object Library.

#if INTERACTIVE
#r "Microsoft.Office.Interop.Excel"
#endif

open System
open Microsoft.Office.Interop.Excel

let excel = new ApplicationClass(Visible = false)
let workbook = excel.Workbooks.Open (templateFile)
//let workbook = excel.Workbooks.Add (xlWBATemplate.xlWBATWorksheet)
let worksheet = (workbook.Worksheets.[1] :?> Worksheet)

excel.Visible <- true
excel.Application.ScreenUpdating <- false

worksheet.Range("A9","E9").Value2 <- [| "This"; "is"; "my"; "test"; ":-)" |]

workbook.SaveAs(outputFile)

workbook.Close()
excel.Workbooks.Close()
excel.Application.Quit()

releaseObject excel

Leave a Reply

Your email address will not be published. Required fields are marked *