POI操作excel

public class TestExcel01 {
    //POI初级入门操作:简单读写
    //读取一个excel文件,读取第一行第一列内容并输出
    @Test
    public void testRead(){
        try {
            Workbook wb=WorkbookFactory.create(new File("F:/web/test/poi/11.xls"));
            Sheet sheet=wb.getSheetAt(0);
            Row row=sheet.getRow(0);
            Cell c=row.getCell(0);
            System.out.println("内容类型="+c.getCellType());
            System.out.println("内容="+c.getStringCellValue());
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //读取一个excel文件,遍历所有内容并输出
    @Test
    public void testList01(){
        try {
            Workbook wb=WorkbookFactory.create(new File("F:/web/test/poi/11.xls"));
            Sheet sheet=wb.getSheetAt(0);
            //System.out.println("最后一行="+sheet.getLastRowNum());
            Row row=null;
            for(int i=0;i<sheet.getLastRowNum();i++){
                row=sheet.getRow(i);
                //System.out.println("最后一列="+row.getLastCellNum());
                for(int j=0;j<row.getLastCellNum();j++){
                    //System.out.print(row.getCell(j).getStringCellValue());
                    //以上因为每个cell的值类型不一定都是String而报错
                    System.out.print(getCellValue(row.getCell(j))+"---");
                }
                System.out.println();
            }
            
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        
    //根据cell内值类型取不同的值
    private String getCellValue(Cell c){
        String str=null;
        switch(c.getCellType()){
        case Cell.CELL_TYPE_BLANK:
            str="";break;
        case Cell.CELL_TYPE_BOOLEAN:
            str=String.valueOf(c.getBooleanCellValue());break;
        case Cell.CELL_TYPE_FORMULA:
            str=String.valueOf(c.getCellFormula());break;
        case Cell.CELL_TYPE_NUMERIC:
            str=String.valueOf(c.getNumericCellValue());break;
        case Cell.CELL_TYPE_STRING:
            str=c.getStringCellValue();break;
        default:str=null;break;
        }
        return str;
    }
    
    //读取一个excel文件,通过迭代器遍历所有内容并输出
    @Test
    public void testList02(){
        try {
            Workbook wb=WorkbookFactory.create(new File("F:/web/test/poi/11.xls"));
            Sheet sheet=wb.getSheetAt(0);
            //POI 3.8以后版本支持,使用不多:因为表格第一行往往不是数据,最后一行也不一定是数据
            for(Row row:sheet){
                for(Cell cell:row){
                    System.out.print(getCellValue(cell)+"---");
                }
                    System.out.println();
            }
                
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //创建并生成一个新的excel
    @Test
    public void testWrite01(){
        Workbook wb=new HSSFWorkbook();//新建一个2003版本的workbook
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream("F:/web/test/poi/cerate01.xls");
            Sheet sheet=wb.createSheet("测试写入01");
            Row row=sheet.createRow(0);
            row.setHeightInPoints((short)30);//行高
            CellStyle cs=wb.createCellStyle();//样式
            cs.setAlignment(CellStyle.ALIGN_CENTER);//内容水平居中
            cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//内容垂直居中
            cs.setBorderBottom(CellStyle.BORDER_DOTTED);cs.setBorderLeft(CellStyle.BORDER_THIN);
            cs.setBorderRight(CellStyle.BORDER_THIN);cs.setBorderTop(CellStyle.BORDER_THIN);
            //此处设置style代码量太大,建议用模板
            Cell cell=row.createCell(0);
            cell.setCellStyle(cs);
            cell.setCellValue("标识");
            cell=row.createCell(1);
            cell.setCellValue("用户名");
            wb.write(fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(fos!=null) fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


你可能感兴趣的:(POI操作excel)