07 noviembre 2008

The requested range is not a valid sheet location

I tried the example in MSDN for setting a range of an Excel by using the Excel Web Service. A simplified way of this example is calling the web service in this way:

object[] o1 = new object[1];

object[] o2 = new object[1];

o1[0] = 1;

o2[1] = o1;

excel.SetRangeA1(sessionId, "Hoja1", "c2:c2", o1);

Notice that the last parameter must be an array with an array in each position (a jagged array).

If you add a reference in Visual Studio 2008 this does not work as the last parameter must be of type of ArrayOfAnyType. If you try the following:

ArrayOfAnyType aoat = new ArrayOfAnyType();

aoat.Add(1); // we add an object

excel.SetRangeA1(sessionId, "Hoja1", "c2:c2", aoat);object[] o1 = new object[1];

This will generate an exception: The requested range is not a valid sheet location. The solution is it needs the second dimension:

ArrayOfAnyType aoat = new ArrayOfAnyType();

ArrayOfAnyType aoat2 = new ArrayOfAnyType();



excel.SetRangeA1(sessionId, "Hoja1", "c2:c2", aoat);

This code will work fine.

No hay comentarios: