26

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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
sahab singh
  • 351
  • 2
  • 4
  • 8

5 Answers5

29

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);
bluish
  • 26,356
  • 27
  • 122
  • 180
Vlad
  • 1,077
  • 8
  • 8
12

You can use this code :

style.setVerticalAlignment(VerticalAlignment.TOP);
Jesús Sánchez
  • 705
  • 11
  • 16
8

this implementation

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP)

is deprecated, use :

org.apache.poi.ss.usermodel.CellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
Tiago Medici
  • 1,944
  • 22
  • 22
6
XSSFWorkbook wbOut = new XSSFWorkbook();    
CellStyle style = wbOut.createCellStyle();
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
cell.setCellStyle(style);
Doesystem
  • 61
  • 1
  • 4
-4
style = wb.createCellStyle();
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue("X");
cell.setCellStyle(style);
ASR
  • 1,801
  • 5
  • 25
  • 33