How can I rotate the text within an HSSFCell class of Apache POI?
Asked
Active
Viewed 1.3k times
3 Answers
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
-
2The int parameter in setRotation should be cast to short. – Giohji Aug 13 '13 at 20:17
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