[ACCEPTED]-How to rotate text in a spreadsheet cell using Apache POI?-spreadsheet

Accepted answer
Score: 27

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

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

HSSFCell c = row.createCell(columnNumber);
c.setCellStyle(myStyle);
Score: 1
CellStyle cssVertical = wb.createCellStyle();
cssVertical.setFont(f);
cssVertical.setRotation((short)90);

0

Score: 0
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 1 in cellXfs section.

More Related questions