0

I'm using PTVS 2.1 Beta 2 with IronPython 2.7.4 + Excel Interop to access Excel ranges. How can I view/print the data contained in the Excel ranges? For example if I take a range from A1 to D4, how can I view the data contained within the 4x4 2D array (16 cells):

xlrange = worksheet.Range["A1", "D4"] # 4x4 2D array.
print xlrange.Value2 # Does not display the contents of the 2D array.
Ari
  • 4,121
  • 8
  • 40
  • 56

1 Answers1

0

Range.Value (or Value2) will return an array, which will become a regular .NET array (i.e. something derived from System.Array) once it crosses the interop boundary. Those do not know how to print themselves. However, they can be trivially converted to a Python collection, which can. E.g.:

print list(xlrange.Value2)

This will flatten the array, however. If you want to convert it to a list of lists, you'll need to manually iterate over it and build the list yourself, e.g.:

a = xlrange.Value2
print [[a[i, j] for j in range(0, a.GetLength(1))] for i in range(0, a.GetLength(0))]
Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289