10

I am able to create the following excel using POI:

enter image description here

As clear from the image, each table is having two values viz. Val One and Val Two.

However, I want the Table Name two cells to be merged into one cell in the first column as below:

enter image description here

How to achieve this in POI ?

Vicky
  • 16,679
  • 54
  • 139
  • 232

1 Answers1

19
 Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("new sheet");

    Row row = sheet.createRow((short) 1);
    Cell cell = row.createCell((short) 1);
    cell.setCellValue("This is a test of merging");

    sheet.addMergedRegion(new CellRangeAddress(
            1, //first row (0-based)
            1, //last row  (0-based)
            1, //first column (0-based)
            2  //last column  (0-based)
    ));

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
shreyansh jogi
  • 2,082
  • 12
  • 20
  • 7
    This is actually an oficial example from here: http://poi.apache.org/spreadsheet/quick-guide.html#MergedCells – Ivan Sopov Jun 26 '13 at 06:15