import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * Writes an Excel file named 'rotated_columns.xls' to the current * directory. */ public class AutosizeRotatedTextColumns { public static void main(String [] argv) { HSSFWorkbook book = new HSSFWorkbook(); HSSFSheet sheet = book.createSheet("Rotated text"); HSSFRow row = sheet.createRow(0); short col; // Make text rotated from -90 to 90 in 9 degree increments for( col = 0; col <= 20; col++ ) { short rot = (short) (-90 + col*9); HSSFCellStyle style = book.createCellStyle(); style.setRotation(rot); HSSFCell cell = row.createCell(col); cell.setCellStyle(style); cell.setCellValue(new HSSFRichTextString("Test Column")); } // Make numbers rotated from -90 to 90 in 9 degree increments for( ; col <= 40; col++ ) { short rot = (short) (-90 + (col-20)*9); HSSFCellStyle style = book.createCellStyle(); style.setRotation(rot); HSSFCell cell = row.createCell(col); cell.setCellStyle(style); cell.setCellValue(54.234342D); } for( col = 0; col <= 40; col++ ) sheet.autoSizeColumn(col); try { FileOutputStream outStream = new FileOutputStream( "rotated_columns.xls"); book.write(outStream); outStream.close(); } catch (Exception e) { e.printStackTrace(); } } }