Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cell vertical top alignment using poi

Tags:

I am using POI api. Now my problem is that I am not able to align cell text vertically top.I am using getCellStyle().setAlignment(HSSFCellStyle.VERTICAL_TOP) to set alignment .

Yet when I open sheet it is not impacted.

like image 360
sahab singh Avatar asked Nov 03 '11 09:11

sahab singh


People also ask

How do you align text to the top of a cell?

Answer: Select the cells that you wish to align. Right-click and then select "Format Cells" from the popup menu. When the Format Cells window appears, select the Alignment tab. Then select "Top" in the drop-down box called Vertical.

How do I center align with Apache POI in Excel?

Use CellUtil. setAlignment(cell, HorizontalAlignment. CENTER); , the method setAlignment(Cell cell, Workbook workbook, short align) is deprecated now from 3.15-beta2.


2 Answers

Had this problem as well, you will be supprised but to set vertical aligment to style in POI you should use setVerticalAlignment() function not setAlignment(). Example:

XSSFCellStyle styleSubHeader = (XSSFCellStyle) wb.createCellStyle(); styleSubHeader.setVerticalAlignment(CellStyle.VERTICAL_CENTER); 
like image 77
Vlad Avatar answered Nov 29 '22 18:11

Vlad


You can use this code :

style.setVerticalAlignment(VerticalAlignment.TOP); 
like image 31
Jesús Sánchez Avatar answered Nov 29 '22 17:11

Jesús Sánchez