首页 文章

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

提问于
浏览
9

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

enter image description here

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

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

enter image description here

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

1 回答

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

相关问题