Chinaunix首页 | 论坛 | 博客
  • 博客访问: 13865
  • 博文数量: 9
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-19 15:06
个人简介

我就是我,走自己的路!

文章分类

全部博文(9)

分类: Java

2013-09-19 18:40:46

  1. 设置单元格内容的位置
    1. public static void main(String[] args) throws Exception {
    2.         Workbook wb = new HSSFWorkbook();
    3.         
    4.         Sheet sheet = wb.createSheet("options");
    5.         Row row = sheet.createRow(2);
    6.         row.setHeightInPoints(30);
    7.         
    8.         CreateCell(wb, row, 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM);
    9.         CreateCell(wb, row, 1, CellStyle.ALIGN_CENTER_SELECTION, CellStyle.VERTICAL_BOTTOM);
    10.         CreateCell(wb, row, 2, CellStyle.ALIGN_FILL, CellStyle.VERTICAL_CENTER);
    11.         CreateCell(wb, row, 3, CellStyle.ALIGN_GENERAL, CellStyle.VERTICAL_CENTER);
    12.         CreateCell(wb, row, 4, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY);
    13.         CreateCell(wb, row, 5, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_TOP);
    14.         CreateCell(wb, row, 6, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_TOP);
    15.         
    16.         FileOutputStream fileOut = new FileOutputStream("e:/poi/demo7.xls");
    17.         wb.write(fileOut);
    18.         fileOut.close();
    19.     }
    20.     
    21.     /**
    22.      * 以某种方式创建Cell
    23.      * @param wb         工作薄
    24.      * @param row         行号
    25.      * @param column    单元格列号
    26.      * @param halign    水平位置
    27.      * @param valign    垂直位置
    28.      */
    29.     private static void CreateCell(Workbook wb, Row row, int column, short halign, short valign){
    30.         Cell cell = row.createCell(column);
    31.         cell.setCellValue("Align It");
    32.         
    33.         CellStyle cellStyle = wb.createCellStyle();
    34.         cellStyle.setAlignment(halign);    
    35.         cellStyle.setVerticalAlignment(valign);
    36.         cell.setCellStyle(cellStyle);
    37.     }
  2. 设置单元格的边框
    1. Workbook wb = new HSSFWorkbook();
    2.         Sheet sheet = wb.createSheet("borders");
    3.         
    4.         Row row = sheet.createRow(1);
    5.         
    6.         Cell cell = row.createCell(1);
    7.         cell.setCellValue(4);
    8.         
    9.         CellStyle style = wb.createCellStyle();
    10.         //设置下边框
    11.         style.setBorderBottom(CellStyle.BORDER_THIN);
    12.         style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
    13.         //设置左边框
    14.         style.setBorderLeft(CellStyle.BORDER_THIN);
    15.         style.setBottomBorderColor(IndexedColors.GREEN.getIndex());
    16.         //设置右边框
    17.         style.setBorderRight(CellStyle.BORDER_THIN);
    18.         style.setBorderBottom(IndexedColors.BLUE.getIndex());
    19.         //设置上边框
    20.         style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);
    21.         style.setTopBorderColor(IndexedColors.BLACK.getIndex());
    22.         
    23.         cell.setCellStyle(style);
    24.         
    25.         FileOutputStream fileOut = new FileOutputStream("e:/poi/demo8.xls");
    26.         wb.write(fileOut);
    27.         fileOut.close();

阅读(537) | 评论(0) | 转发(0) |
0

上一篇:POI--初探

下一篇:JSP---初探

给主人留下些什么吧!~~