POI日常应用--换行,对齐,遍历单元格

POI常用的三种操作
    public static void main(String[] args)throws Exception {
	HSSFWorkbook wb = new HSSFWorkbook();     
        HSSFSheet sheet = wb.createSheet("new sheet");  
        
        //遍历列和单元格
        for(Iterator rit=sheet.rowIterator();rit.hasNext();){
            HSSFRow row = (HSSFRow)rit.next();
            for(Iterator cit = row.cellIterator();cit.hasNext();){
        	HSSFCell cell = (HSSFCell)cit.next();
        	cell.setCellValue("hello");
            }
        }
        
        HSSFCellStyle style = wb.createCellStyle();  
        //单元格对齐
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
//        style.setAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
//        style.setAlignment(HSSFCellStyle.VERTICAL_BOTTOM);//垂直底部
//        style.setAlignment(HSSFCellStyle.VERTICAL_TOP);//垂直顶部
        
        //在单元格中使用换行
        HSSFRow row3 = sheet.createRow(3);
        HSSFCell cell3 = row3.createCell(3);
        cell3.setCellValue("Use \n with word wrap on to create a new line");
        //为了能够使用换行,您需要设置单元格的样式 wrap=true
        HSSFCellStyle s = wb.createCellStyle();
        s.setWrapText(true);
        cell3.setCellStyle(s);
        
        //增加单元格的高度 以能够容纳2行字
        row3.setHeightInPoints(2*sheet.getDefaultRowHeightInPoints());
        //调整列宽以使用内容长度
        sheet.autoSizeColumn((short)2);
        
        // Write the output to a file     
        FileOutputStream fileOut = new FileOutputStream("workbook2.xls");     
        wb.write(fileOut);     
        fileOut.close();     
        System.out.println("SUCCESS!");
    }

你可能感兴趣的:(java,poi)