简体   繁体   中英

How to align and merge texts into a single cell

I am trying to do like this

from:

| Hello World Hey |

to a single cell and row like this:

|Hello |
|World |
|Hey   |

I have tried:

style2.setAlignment(CellStyle.ALIGN_LEFT);
cell.setCellStyle(style2);

and also:

style2.setVerticalAlignment(VerticalAlignment.CENTER);
cell.setCellStyle(style2);

But nothing work the way I want, can someone help ?

Can you try below approach:

CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
    cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
    lastCell.setCellStyle(cellStyle);

Create a new cell style from workbook.

CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();

If you want to set the whole text in single cell, then you need to wrap it as we do in excel. For tahat yoou need to set wrap text as true.

 style2.setWrapText(true);

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