首页 > 代码库 > 解析Excel_Jxl

解析Excel_Jxl

/*

*

*使用jxl创建Excel并写入数据

*/

public class JxlExpeExcel{

  public static void main(String[] args){

    String[] title = {"id","name","sex"};//用数组存表头

    //创建Excel文件

    File  File= newFile("e:/jxl_test.xls");

    try{

      file.createNewFile();

    //创建工作簿

      WritableWorkbook workbook = Workbook.createWorkbook(file);

    //创建sheet

      WritableSheet sheet = workbook.createSheet("sheet1",0);

    //添加数据

      Label label = null;  

    for(int i = 0; i < title.length; i++){

      label = new Label(i,0,title[i])  //( 列,行,名称)

      sheet.addCell(lable);

    }

    //追加数据

    for((int i = 0; i < 10; i++){

      lable = new Lable(0,1,"a" + 1);

      sheet.addCell(label);

      lable = new Lable(1,1,"user" + i);

      sheet.addCell(label);

      lable = new Lable(2,1,"男" + 1);

      sheet.addCell(label);

    }

    /写书数据

    workbook.write();

    workbook.close();

    }catch(Exception){

      e.printStackTrace();

    }

  }

}

 

 

/*

*

*使用jxl解析Excel

*/

public class JxlReadExcel{

  public static void main(String[] args){

    //创建workbook

    Workbook workbook = Workbook.getWorkbook(new File("e:/jxl_test.xls"));//要捕获Exception

    //获取工作表sheet

    Sheet sheet = workbook.getSheet(0);

    //获取Excel的数据

    for(int i = 0; i < sheet.getRows; i++){

    //循环列

      for(int j = 0; j <sheet.getColumns(); j++)

        Cell cell = sheet.getCell(j,i);

        System.out.print(cell.getCootents() + " " );

      }

    System.out.println();

    }

    workbook.close();

 

  }

}

解析Excel_Jxl