SWT&JFace

    public static boolean createExcel(File file, List<double []> data){
        WritableWorkbook workBook = null;
        try {
            workBook = Workbook.createWorkbook(file);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        WritableSheet sheet = workBook.createSheet("MatrixA", 0);
        if(data !=null && data.size() > 0){
            for(int i =0; i< data.size();i++){
                double[] d=data.get(i);
                for(int j =0; j<d.length;j++){
                    jxl.write.Number cell = new jxl.write.Number(j,i,d[j]);
                    try {
                        sheet.addCell(cell);
                    } catch (RowsExceededException e) {
                        e.printStackTrace();
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }
                   
                }
            }
        }
        sheet = workBook.createSheet("MatrixB", 1);
        try {
            workBook.write();
            workBook.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (WriteException e) {
            e.printStackTrace();
            return false;
        }       
        return true;      
    }
   
    public static List<double []> readExcel(File file){
        List<double []> list1 = new ArrayList<double []>();
        Workbook workBook = null;
        try {
            workBook = Workbook.getWorkbook(file);
        } catch (BiffException e) {
            e.printStackTrace();
            return list1;
        } catch (IOException e) {
            e.printStackTrace();
            return list1;
        }
       
        Sheet [] sheets = workBook.getSheets();
//        for(Sheet sheet : sheets){
            Sheet sheet = sheets[0];
            int rows = sheet.getRows();
            int cols = sheet.getColumns();
            System.out.println("rows="+rows+",cols="+cols);
            for(int i =0;i<rows;i++){
                double [] d = new double[cols];
                Cell[] row =sheet.getRow(i);
                for(int j =0;j<cols;j++){
                    d[j]=Double.parseDouble(row[j].getContents());
                }
                list1.add(d);
            }
           
//        }
        return list1;
    }

你可能感兴趣的:(jface)