strangelights.com

Main F# Site

Edit Edit
Print Print
Recent Changes Recent Changes
Subscriptions Subscriptions
Lost and Found Lost and Found
Find References Find References
Rename Rename
Search
List all versions List all versions
F Sharp Excel Automation
.
Summary

Here is an example of an Excel "pipe", e.g. excel used as a data sink:

Example usage :

        let e = excel ;;
        let wb = workbook e;;
        let ws = sheet wb;;
        let rng = new Random();;
        (Array.init 10 (fun i -> rng.NextDouble()) ) |> xl_dump ws 10 10 ;;  

Main program :

        #r "Microsoft.Office.Interop.Excel.dll";;
        open System
        open System.Reflection (* For Missing.Value and BindingFlags *) 
        open System.Runtime.InteropServices (* For COMException *) 
        open Microsoft.Office.Interop.Excel 


        let excel =
                let app = new ApplicationClass() in
                let _ =  app.Visible <- true in
                app;;


        let workbook (excel : ApplicationClass) = 
                let workbooks = excel.Workbooks in
                let wb  = workbooks.Add(XlWBATemplate.xlWBATWorksheet) in
                wb;;


        let sheet (workbook :  Workbook) =
                let sheets = workbook.Worksheets in
                let sh = (sheets.Item(1) :?> _Worksheet) in
                sh;;


        let xl_dump (worksheet :  _Worksheet) x1 y1 data   =
                let cell1 = worksheet.Cells.Item(x1,y1) in
                let size = Array.length data in
                let cellN = worksheet.Cells.Item(x1, y1+size-1) in
                let target_range = worksheet.Range(cell1,cellN) in
                let _ = target_range.Value2 <- data in
        ();;
Welcome to F Sharp Wiki, view the HomePage

This site supports the new NoFollow anti-spam initiative.

Recent Topics

Copyright 2005, Robert Pickering (Others where credited) | Terms of Use