12

How can I rotate the text within an HSSFCell class of Apache POI?

Sled
  • 18,541
  • 27
  • 119
  • 168
Garudadwajan
  • 689
  • 4
  • 11
  • 24

3 Answers3

27

Use HSSFCellStyle, that class has a method called setRotation(short rotation) which will rotate the text. All you do is apply the cell style to a cell:

HSSFCellStyle myStyle = workbook.createCellStyle();
myStyle.setRotation((short)90);

HSSFCell c = row.createCell(columnNumber);
c.setCellStyle(myStyle);
vahapt
  • 1,685
  • 1
  • 21
  • 26
amischiefr
  • 4,750
  • 2
  • 29
  • 22
2
CellStyle cssVertical = wb.createCellStyle();
cssVertical.setFont(f);
cssVertical.setRotation((short)90);
Ishan Liyanage
  • 2,237
  • 1
  • 26
  • 25
1
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFRow row = sheet.createRow(1);
XSSFCell cell = row.createCell(1);

XSSFCellStyle cs = workbook.createCellStyle();
cs.setRotation((short) 90);              // set text rotation
cs.getStyleXf().setApplyAlignment(true); // <<< Important

cell.setCellValue("Vertical Text");
cell.setCellStyle(cs);

workbook.write(new FileOutputStream("out.xlsx"));

Apache POI 3.17, need to manually add alignment="true" attribute in cellXfs section.

Grigory K
  • 1,301
  • 10
  • 10