Using Spreadsheet Functions with Excel 2007

From IronPython Cookbook

Code submitted by Stefan Thomasson. This was tested with Excel 2007, but might also work with Excel 2003.

import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
from Microsoft.Office.Interop import Excel

app = Excel.ApplicationClass()
x = app.WorksheetFunction.Power(2,12)
print x #results in 4096 as we would expect it so we now have access to Excel functions

sum = app.WorksheetFunction.Sum(2,12)
print sum #results in 14 but often we want to feed an array I suppose and ordinary Lists and Tuples are rejected

from System import Array
intArray = Array[int](range(10))
rsum = app.WorksheetFunction.Sum(intArray)
print rsum #results in 45 and I am now happy as I can access the Excel functions at will and now there is a solid starting point


Back to Contents.

TOOLBOX
LANGUAGES