title: Java - Apache POI - It's impossible to fill rows and cells with loops in this pattern? (Excel)
body: <p>Recently I opened this topic:</p>
  
  <p><a href="https://stackoverflow.com/questions/17226194/java-apache-poi-trouble-filling-rows-and-cells-with-loops-excel">Java - Apache POI - Trouble filling rows and cells with loops (Excel)</a></p>
  
  <p>I developed another structure, hoping that this would solve the problem, and the problem became even more clear. </p>
  
  <p>Considering that pattern:</p>
  
  <pre><code>hashkey1 | hashkey2 | hashkey3 | hashkey4
  value1-1 | value2-1 | value3-1 | value4-1  
  value1-2 | value2-2 | value3-2 | value4-2  
  value1-3 | value2-3 | value3-3 | value4-3  
  value1-4 | value2-4 | value3-4 | value4-4  
  value1-5 | value2-5 | value3-5 | value4-5 
  </code></pre>
  
  <p>Here's the new code:</p>
  
  <pre><code>Row keyRow = worksheet.createRow(4);
  Row valueRow = null;
  int keyCell = -2;
  int firstIndex = 0;
  int secondIndex = -2;
  int row = 5;
  ArrayList&lt;String&gt; keyArray = new ArrayList();
  ArrayList&lt;String&gt; innerValueArray;
  ArrayList&lt;ArrayList&lt;String&gt;&gt; outerValueArray = new ArrayList&lt;ArrayList&lt;String&gt;&gt;();
  for (Map.Entry&lt;String, ArrayList&lt;String&gt;&gt; e : matrix.entrySet()) {
      keyArray.add(e.getKey());
      innerValueArray = new ArrayList();
      for (String s : e.getValue()) {
          innerValueArray.add(s);
      }
      outerValueArray.add(innerValueArray);
  }
  for (String s : keyArray) {
      keyCell += 2;
      keyRow.createCell(keyCell).setCellValue(keyArray.get(firstIndex++));
  }
  for (ArrayList&lt;String&gt; aS : outerValueArray) {
      secondIndex += 2;
      for (String s : aS) {
          if ((row - 5) &lt; outerValueArray.size()) {
              row++;  
          }
          valueRow = worksheet.createRow(row);
          valueRow.createCell(secondIndex).setCellValue(s);
      }
  }
  </code></pre>
  
  <p>In the other topic, the problematic variable is the "keyCell", and in this one, it is the secondIndex. The only way to put in the right place the values is through the same variable that is used for the keys, but this makes possible only one column to get filled, erasing the content of others. The problem is EXACTLY the same from the other topic. All alternatives lead to this, and I want the values in the SAME lines.</p>
  
  <p>The question is: It is IMPOSSIBLE to fill and excel sheet in this pattern? Because it's certainly impossible to update the variable in the inner loop and keep the desired pattern. How can I build this pattern with loops?</p>
  
  <p>OBS: It gets incremented by 2 in my software, it makes no difference. Doing it by one renders the same problem (the cells get erased).</p>
  
  <p>I thank you very much!</p>