Two-dimensional Array from .net

Posted by Fabian Kerkhoff on 10-Jun-2014 02:36

Hey.

I work with the Excel-Class from .net.

The Problem is that this class uses two-dimensional arrays.

How can I get me a value from this array?

I have no other possibility found to get around this otherwise.
Example:
oExcelBlatt:Cells[1,1].

Posted by Mike Fechner on 10-Jun-2014 04:52

/**********************************************************************

* Copyright (C) 2006-2012 by Consultingwerk Ltd. ("CW") -            *

* www.consultingwerk.de and other contributors as listed             *

* below.  All Rights Reserved.                                       *

*                                                                    *

*  Software is distributed on an "AS IS", WITHOUT WARRANTY OF ANY    *

*   KIND, either express or implied.                                 *

*                                                                    *

*  Contributors:                                                     *

*                                                                    *

**********************************************************************/

/*------------------------------------------------------------------------

   File        : Consultingwerk.Utilities.Support.ExcelHelper

   Purpose     : Helper classes for communicating with MS Excel through

                 COM-Interop

   Syntax      : Static methods

   Description : The GUI for .NET AVM bridge does not support accessing

                 multi-dimensional .NET Collections or Array's. These

                 helper routines are designed to workaround the ABL

                 restriction

   Author(s)   : Mike Fechner / Consultingwerk Ltd.

   Created     : Sun Feb 26 22:14:38 CEST 2012

   Notes       :

 ----------------------------------------------------------------------*/

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Office.Interop.Excel;

namespace Consultingwerk.Utilities.Support

{

   public class ExcelHelper

   {

       /*------------------------------------------------------------------------------

           Purpose: Returns a single cell value  

           Notes:  

           @param poWorksheet The reference to the Microsoft.Office.Interop.Excel.Workshop instance

           @param piRow The row index

           @param piCell The cell index

           @return The System.Object value of the cell

       ------------------------------------------------------------------------------*/

       public static System.Object GetExcelCellValue (Worksheet poWorksheet, int iRow, int iCell)

       {

           return ((Range)poWorksheet.Cells[iRow, iCell]).Value;

       }

       /*------------------------------------------------------------------------------

           Purpose: Returns multiple values from row in a Excel worksheet  

           Notes:  

           @param poWorksheet The reference to the Microsoft.Office.Interop.Excel.Workshop instance

           @param piRow The row index

           @param values An Object[] to be populated with values

       ------------------------------------------------------------------------------*/

       public static void GetExcelCellValues(Worksheet poWorksheet, int iRow, ref Object[] values)

       {

           int i;

           for (i = 0; i < values.Length; i++)

           {

               values[i] = ((Range)poWorksheet.Cells[iRow, i + 1]).Value;

           }            

       }

       /*------------------------------------------------------------------------------

           Purpose: Assigns a single cell value  

           Notes:  

           @param poWorksheet The reference to the Microsoft.Office.Interop.Excel.Workshop instance

           @param piRow The row index

           @param piCell The cell index

           @param The System.Object value that should be assigned to the cell

       ------------------------------------------------------------------------------*/

       public static void SetExcelCellValue(Worksheet poWorksheet, int iRow, int iCell, Object oValue)

       {

           ((Range)poWorksheet.Cells[iRow, iCell]).Value = oValue;

       }

       /*------------------------------------------------------------------------------

           Purpose: Assigns multiple cell values from an Object []

           Notes:  

           @param poWorksheet The reference to the Microsoft.Office.Interop.Excel.Workshop instance

           @param piRow The row index

           @param The System.Object[] with the values that should be assigned to the cells in that row

       ------------------------------------------------------------------------------*/

       public static void SetExcelCellValues(Worksheet poWorksheet, int iRow, Object[] oValues)

       {

           int i;

           for (i = 0; i < oValues.Length; i++)

           {

               ((Range)poWorksheet.Cells[iRow, i + 1]).Value = oValues[i];

           }

       }

   }

}

Posted by Mike Fechner on 10-Jun-2014 04:49

Hi Fabian,
 
this is not possible directly from ABL Code.
 
You can use .NET reflection, passing in an Array as the Index parameter to the PropertyInfo’s GetValue method
 
http://msdn.microsoft.com/en-us/library/b05d59ty(v=vs.110).aspx
 
Or you write a little helper class in C# like the one attached.
 
Mike
 
[collapse]
From: Fabian Kerkhoff [mailto:bounce-fabian63@community.progress.com]
Sent: Dienstag, 10. Juni 2014 09:37
To: TU.OE.Development@community.progress.com
Subject: [Technical Users - OE Development] Two-dimensional Array from .net
 
Thread created by Fabian Kerkhoff

Hey.

I work with the Excel-Class from .net.

The Problem is that this class uses two-dimensional arrays.

How can I get me a value from this array?

I have no other possibility found to get around this otherwise.
Example:
oExcelBlatt:Cells[1,1].
Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

All Replies

Posted by Mike Fechner on 10-Jun-2014 04:49

Hi Fabian,
 
this is not possible directly from ABL Code.
 
You can use .NET reflection, passing in an Array as the Index parameter to the PropertyInfo’s GetValue method
 
 
Or you write a little helper class in C# like the one attached.
 
Mike
[collapse]
From: Fabian Kerkhoff [mailto:bounce-fabian63@community.progress.com]
Sent: Dienstag, 10. Juni 2014 09:37
To: TU.OE.Development@community.progress.com
Subject: [Technical Users - OE Development] Two-dimensional Array from .net
 
Thread created by Fabian Kerkhoff

Hey.

I work with the Excel-Class from .net.

The Problem is that this class uses two-dimensional arrays.

How can I get me a value from this array?

I have no other possibility found to get around this otherwise.
Example:
oExcelBlatt:Cells[1,1].
Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

Posted by Mike Fechner on 10-Jun-2014 04:52

/**********************************************************************

* Copyright (C) 2006-2012 by Consultingwerk Ltd. ("CW") -            *

* www.consultingwerk.de and other contributors as listed             *

* below.  All Rights Reserved.                                       *

*                                                                    *

*  Software is distributed on an "AS IS", WITHOUT WARRANTY OF ANY    *

*   KIND, either express or implied.                                 *

*                                                                    *

*  Contributors:                                                     *

*                                                                    *

**********************************************************************/

/*------------------------------------------------------------------------

   File        : Consultingwerk.Utilities.Support.ExcelHelper

   Purpose     : Helper classes for communicating with MS Excel through

                 COM-Interop

   Syntax      : Static methods

   Description : The GUI for .NET AVM bridge does not support accessing

                 multi-dimensional .NET Collections or Array's. These

                 helper routines are designed to workaround the ABL

                 restriction

   Author(s)   : Mike Fechner / Consultingwerk Ltd.

   Created     : Sun Feb 26 22:14:38 CEST 2012

   Notes       :

 ----------------------------------------------------------------------*/

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Office.Interop.Excel;

namespace Consultingwerk.Utilities.Support

{

   public class ExcelHelper

   {

       /*------------------------------------------------------------------------------

           Purpose: Returns a single cell value  

           Notes:  

           @param poWorksheet The reference to the Microsoft.Office.Interop.Excel.Workshop instance

           @param piRow The row index

           @param piCell The cell index

           @return The System.Object value of the cell

       ------------------------------------------------------------------------------*/

       public static System.Object GetExcelCellValue (Worksheet poWorksheet, int iRow, int iCell)

       {

           return ((Range)poWorksheet.Cells[iRow, iCell]).Value;

       }

       /*------------------------------------------------------------------------------

           Purpose: Returns multiple values from row in a Excel worksheet  

           Notes:  

           @param poWorksheet The reference to the Microsoft.Office.Interop.Excel.Workshop instance

           @param piRow The row index

           @param values An Object[] to be populated with values

       ------------------------------------------------------------------------------*/

       public static void GetExcelCellValues(Worksheet poWorksheet, int iRow, ref Object[] values)

       {

           int i;

           for (i = 0; i < values.Length; i++)

           {

               values[i] = ((Range)poWorksheet.Cells[iRow, i + 1]).Value;

           }            

       }

       /*------------------------------------------------------------------------------

           Purpose: Assigns a single cell value  

           Notes:  

           @param poWorksheet The reference to the Microsoft.Office.Interop.Excel.Workshop instance

           @param piRow The row index

           @param piCell The cell index

           @param The System.Object value that should be assigned to the cell

       ------------------------------------------------------------------------------*/

       public static void SetExcelCellValue(Worksheet poWorksheet, int iRow, int iCell, Object oValue)

       {

           ((Range)poWorksheet.Cells[iRow, iCell]).Value = oValue;

       }

       /*------------------------------------------------------------------------------

           Purpose: Assigns multiple cell values from an Object []

           Notes:  

           @param poWorksheet The reference to the Microsoft.Office.Interop.Excel.Workshop instance

           @param piRow The row index

           @param The System.Object[] with the values that should be assigned to the cells in that row

       ------------------------------------------------------------------------------*/

       public static void SetExcelCellValues(Worksheet poWorksheet, int iRow, Object[] oValues)

       {

           int i;

           for (i = 0; i < oValues.Length; i++)

           {

               ((Range)poWorksheet.Cells[iRow, i + 1]).Value = oValues[i];

           }

       }

   }

}

This thread is closed