Java Exce 读写(一)

这次用的是Jxl的方式

jar包可以在
http://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl/2.6.12 下载


 @Test
    public void Test1(){
        File file = new File("D:\\a.xls");
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            WritableWorkbook writableWorkbook = Workbook.createWorkbook(file);
            WritableSheet sheet1 = writableWorkbook.createSheet("sheet1", 0);
            for ( int row = 0 ; row<10;row++) { // row
                for (int i = 0; i < 10; i++) {  //col
                    sheet1.addCell(new Label(i,row,"content:"+i));
                }
            }
            writableWorkbook.write();
            writableWorkbook.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }

    }

添加单元格就是根据坐标哪行哪列就行了


    /**
     *  read excel
     */
    @Test
    public void test2(){
        File file = new File("D:\\a.xls");
        try {
            Workbook workbook = Workbook.getWorkbook(file);
            int numberOfSheets = workbook.getNumberOfSheets();
            for (int i=0 ; i< numberOfSheets ; i++) {
                Sheet sheet = workbook.getSheet(i);

                for (int j = 0; j < sheet.getRows(); j++) {
                    for (int k = 0; k < sheet.getColumns(); k++) {
                        System.out.print(" ");
                        System.out.print(sheet.getCell(k,j).getContents());
                        System.out.print(" ");
                    }
                    System.out.println("");
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }

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