title: How to merge cells and set value at the same time by using Apache POI?
body: <p>I want to merge some cells in my excel template file, and then set values in merged cells. Here is my java code. Before I add <code>set value part</code>, it works well on merging cells. But when I just add <code>set value part</code>, it seems like nothing changed even no cells merged. I also try to move <code>set value part</code> after <code>merge cells part</code>, then office remind me repair the output excel file. </p>
  
  <p>What should I do to merge cells and set value at the same time?</p>
  
  <pre><code>for (int sht = 0; sht &lt; 3; sht++) {
      sheet = workbook.getSheetAt(sht);
      row = 1;
      for (Data d : list) {
          // set value part
          cell = sheet.getRow(row).getCell(0);
          cell.setCellValue(d.a);
          cell = sheet.getRow(row).getCell(1);
          cell.setCellValue(d.b);
          cell = sheet.getRow(row).getCell(2);
          cell.setCellValue(d.c);
          // merge cells part
          sheet.addMergedRegion(new CellRangeAddress(row, row + 1, 0, 0));
          sheet.addMergedRegion(new CellRangeAddress(row, row + 1, 1, 1));
          sheet.addMergedRegion(new CellRangeAddress(row, row + 1, 2, 2));
          sheet.addMergedRegion(new CellRangeAddress(row, row + 1, 3, 3));
          //
          row += 2;
      }
  }
  </code></pre>