Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only

How to write a 2 dimension array into excel with SheetWrite

  • 1.  How to write a 2 dimension array into excel with SheetWrite

    Posted Fri May 10, 2019 12:10 PM

    Hi,

    this may looks trivial and I thought it was. But let me be very explicit and give an example.

    Suppose you have a file f.xls with a tab "test"

    .mod

    int n=100;
    int m=50;

    int y[i in 1..n][j in 1..m]=i+j;

    subject to
    {

    }

     

     execute
        {

        // http://cwestblog.com/2013/09/05/javascript-snippet-convert-number-to-column-name/
        function toColumnName(num) {
          for (var ret = '', a = 1, b = 26; (num -= a) >= 0; a = b, b *= 26) {
            ret = String.fromCharCode(parseInt((num % b) / a) + 65) + ret;
          }
          return ret;
        }

        // 1,1 => A1  1,4 => D1 2,27 => AA2
        function convertR1C1toA1(r,c)
        {
        return(toColumnName(c)+r);
        }

        }
        
    string sheeetWriteString;

    execute
    {
    sheeetWriteString="test!A1:"+convertR1C1toA1(n,m);  
    }  

     

    .dat

     

    SheetConnection my_sheet("file.xlsx");
    y to SheetWrite(my_sheet,sheeetWriteString);

    will write the array y in the tab "test"

     

    regards

     

    PS:

    Many how to with OPL at https://www.linkedin.com/pulse/how-opl-alex-fleischer/

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer