I'm assuming here your data comes from a spreadsheet (Range object), not VBA code.
In that case, Excel will hand you a SafeArray of Variants (Variant()
in VBA terminology), not a SafeArray of doubles (Double()
), no matter what the contents of the cells happen to be. And you are correct that the array will always be two-dimensional (or it will return a single Variant if the range size is exactly 1x1).
I'm not familiar with JACOB, and yes, the documentation looks rather lacking. Could it be that getDoubles()
only works when the SafeArray is a SafeArray of Doubles? Did you try to use getVariants()
? If you get something back we'll know we're on the right track.
Keep in mind that performance-wise, you are stuck. somebody (you or the library) is going to have to write that loop. The Variants won't magically become doubles; someone will have to loop through the array, call Win32 VariantChangeType()
or an equivalent method, extract the double field from the Variant (apparently in JACOB you call changeType()
then getDouble()
), and finally put the results in a Java double array. Whether JACOB will do this for you or you have to do it yourself, I have no idea.
Not being familiar with JACOB I'm not sure I I'm making sense, but I hope this helps. Good luck.
[PS:]
I feel I need to clarify that comment on calling VariantChangeType()
. This is technically optional, but I find it generally healthy to explicitly convert variants to the data type I'm interested in via VariantChangeType()
or an equivalent method. It's too easy for numbers to be stored as strings or something else, particularly when the source is something as free-form as Excel. This API call is exactly what VB/VBA calls when you use a function like CDbl()
. Be aware that a bit of errant formatting is all it takes for you to be provided with a DATE instead of a Double.
If you are absolutely, positive beyond sure that all the cells will contain numbers (doubles) and Excel will never return to you anything else, then by all means call JACOB's getDouble()
directly and skip the conversion step.