在POI中合并excel输出中的单元格

时间:2013-06-26 06:12:30

标签: java apache-poi

我可以使用POI创建以下Excel:

enter image description here

从图像中可以清楚地看出,每个表都有两个值即。 Val One和Val Two。

但是,我希望将表名称两个单元格合并到第一列中的一个单元格中,如下所示:

enter image description here

如何在POI中实现这一目标?

1 个答案:

答案 0 :(得分:18)

 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();