How to rotate text in a spreadsheet cell using Apache POI?
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);
CellStyle cssVertical = wb.createCellStyle();cssVertical.setFont(f);cssVertical.setRotation((short)90);
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 rotationcs.getStyleXf().setApplyAlignment(true); // <<< Importantcell.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.