天天看點

Android讀取excel檔案内容

/**
     * 讀取excel檔案内容
     * @throws Exception
     */
    public void readExcel() throws Exception 
    {
        try {
            Workbook book = Workbook.getWorkbook(getAssets().open("target.xls"));
            try 
            {
                Sheet sheet = book.getSheet();
                int rows = sheet.getRows();
                int columns = ;
                for (int i = ; i < rows; i++) 
                {
                    Addr addr = new AddressBean().new Addr();
                    for (int j = ; j < columns; j++) 
                    {
                        String cell = sheet.getCell(j, i).getContents(); 
                        switch (j) 
                        {
                            case :
                                addr.setId(cell);
                                break;

                            case :
                                addr.setpId(cell);
                                break;

                            case :
                                addr.setGrade(cell);
                                break;

                            case :
                                addr.setName(cell);
                                break;
                        }
                    }
                    addrs.add(addr);
                }
                AddressBean bean = new AddressBean();
                bean.setAddrs(addrs);
                String json = GsonUtil.bean2json(bean);
                PrefUtils.putString(instance, "target", json);
            } 
            finally 
            {
                if (book != null) 
                {
                    book.close();
                }
            }

        } catch (BiffException e) {
            System.err.println(e + "");
        } catch (IOException e) {
            System.err.println(e + "檔案讀取錯誤");
        }
    }
           

需要導入第三方包:jxl-2.6.jar