一、设置单元格对齐方式:
1 /** 2 * 设置单元格对齐方式 3 */ 4 public static void main(String[] args) throws Exception { 5 Workbook wb = new HSSFWorkbook(); //定义一个新的工作簿 6 Sheet sheet = wb.createSheet("第一个sheet页"); 7 Row row = sheet.createRow(2); //创建第3行 8 row.setHeightInPoints(30); //设置行高 9 10 createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM); 11 createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER); 12 createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP); 13 createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP); 14 15 FileOutputStream fileOut = new FileOutputStream("E:\\工作簿.xls"); 16 wb.write(fileOut); 17 fileOut.close(); 18 } 19 /** 20 * 创建一个单元格并为其设置指定的对齐方式 21 * @param wb 工作簿 22 * @param row 行 23 * @param column 列 24 * @param halign 水平方向对齐方式 25 * @param valign 垂直方向对齐方式 26 */ 27 @SuppressWarnings("unused") 28 private static void createCell(Workbook wb, Row row, short column, short halign, short valign){ 29 Cell cell = row.createCell(column); //创建单元格 30 cell.setCellValue(new HSSFRichTextString("Align It")); //设置值 31 CellStyle cellStyle = wb.createCellStyle(); //创建单元格样式 32 cellStyle.setAlignment(halign); //设置单元格水平方向对齐方式 33 cellStyle.setVerticalAlignment(valign); //设置单元格垂直方向对齐方式 34 cell.setCellStyle(cellStyle); 35 }