简体   繁体   中英

How to use addMergedRegion apache poi?

I am trying to merge two columns of one row to make one long line of text without any vertical separation by the cell borders. Here's what I have so far:

CellRangeAddress mergedRegion = new CellRangeAddress(0,0,0,1);
sheet.addMergedRegion(mergedRegion);
XSSFRow row = sheet.createRow(mergedRegion.getFirstRow());
XSSFCell cell = row.createCell(mergedRegion.getFirstColumn());
cell.setCellValue("some string");

Is this the correct way to set the cells contents? In my Junits do I refer to this merged region like this:

assertEquals(workbook.getSheetAt(0).getRow(mergedRegion.getFirstRow())
    .getCell(mergedRegion.getFirstColumn()).getStringCellValue(),"some string");

It is probably easier to set the cell contents before you create the merged region. So for example you could:

Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellValue("some string");
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 5));

This would add a merged region in columns 1-5 of row 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM