Android學習隨筆(16)------Excle表格的解析

最近完成了一個課程表的小Demo,代碼已經上傳至GitHub
完成主要是依靠jxl.jar這個jar包工具
Exler

Excel文件的樣例 :
Exler

Exler

Excel文件的讀取及解析

    public static void readXLS(final File file, Context context) {
        final StringBuilder sb = new StringBuilder();
        try {
            Workbook book = Workbook.getWorkbook(file);
            for (int n = 0; n < book.getNumberOfSheets(); n++) {    // 獲取sheet頁的數目
                List<Integer> arrayList = new ArrayList<>();    // 存放節數
                Map<String, WeekDay> map = new HashMap<String, WeekDay>();    // 存放班級的單天課程
                Sheet sheet = book.getSheet(n);
                for (int i = 2; i< sheet.getColumns(); i++) {    // 初始化map class表
                    map.put(sheet.getCell(i,0).getContents(), null);
                    DBHelperImpl dbHelper = new DBHelperImpl(context, "my.db", null, 1);
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    dbHelper.insertClass(db, sheet.getName(), sheet.getCell(i,0).getContents());
                }
                int j = 0;
                int i = 0;
                arrayList.add(0);
                for (i = 1; !sheet.getCell(0, i).getContents().equals("備註"); i++, j++) {    // 解析一個年級的課有幾天每天有幾節課
                    if (sheet.getCell(0, i).getContents().contains("星期")) {
                        if (j > 0) {
                            arrayList.add(j);
//                                    Log.d("file", j + "" +sheet.getCell(0, i).getContents());    // 16級表 6 12 18 24 30 31
                        }
                    }
                }
                arrayList.add(j);
                arrayList.add(arrayList.get(arrayList.size() - 1) + 1);    // 備註
//                        for (int k = 0; k < arrayList.size(); k++) {    // 測試天數
//                            System.out.println(arrayList.get(k));
//                        }

                int row = i;    // 真實行數 - 1 從0開始數
                int col = sheet.getColumns();    // 真實列數 從1開始數
                int l;

                WeekDay wd = null;
                List<Section> sections = null;
                for (int k = 1; k < arrayList.size(); k++) {
                    for (i = 2; i < col; i++) {    // 班級循環
                        Section s = null;
                        wd = new WeekDay();
                        sections = new ArrayList<>();
                        for (j = arrayList.get(k - 1) + 1, l = 1; j <= arrayList.get(k); j++, l++) {    // 課程循環
                            s = new Section();
                            s.setRow(j);
                            s.setCol(i);
                            s.setSectionTime(l * 2 - 1 + "-" + l * 2);
                            Log.d("file_rc", i + "  " + " " + j);
                            s.setSectionContent(sheet.getCell(i, j).getContents());
                            sb.append(s.toString());
                            sections.add(s);
                        }    // 一天課程
                        wd.setSections(sections);
                        wd.setWeekDayName(k+"");
                        map.put(sheet.getCell(i,0).getContents(), wd);
                        Log.d("file_fg", "--------------------------");
                    }
                    for (Map.Entry<String, WeekDay> m : map.entrySet()) {
//                                 調用數據庫
                        DBHelperImpl dbHelper = new DBHelperImpl(context, "my.db", null, 1);
                        SQLiteDatabase db = dbHelper.getWritableDatabase();
                        dbHelper.insertCURRICULUM(db, m.getKey(), m.getValue());
                    }
                }
            }
            book.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

寫回Excel表格,因爲權限的緣故,所以寫回操作的流程是 新建一個副本然後修改數據

    public static int writeExcel(Section section, Context context) {
        int times;
        try {
            SharedPreferences pref = context.getSharedPreferences("SharedPreferences_data",MODE_PRIVATE);
            String path = pref.getString("path", "");
            times = pref.getInt("times", 0);

            Workbook rwb = Workbook.getWorkbook(new File(path));
            WritableWorkbook wwb = Workbook.createWorkbook(new File(Environment.getExternalStorageDirectory() + "/" +times +".xls"), rwb);// copy
            WritableSheet ws = wwb.getSheet(getSheetIdByName(section.getClassName()));
            WritableCell wc = ws.getWritableCell(section.getCol(), section.getRow());
            // 判斷單元格的類型,做出相應的轉換
            Label label = (Label) wc;
            label.setString(section.getSectionContent());
            Log.d("admin_ec", section.getSectionContent());
            Log.d("admin_ec", label.getString());
            wwb.write();
            wwb.close();
            rwb.close();

            SharedPreferences.Editor editor = context.getSharedPreferences("SharedPreferences_data",MODE_PRIVATE).edit();
            times++;
            editor.putInt("times", times);    // 增加次數
            editor.putString("path", Environment.getExternalStorageDirectory() + "/" + (times-1) +".xls");
            editor.apply();
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
        return 0;
    }

    public static int getSheetIdByName(String className) {
        int a = Integer.parseInt(className.substring(0,2));
        if (a == 13) {return 0;}
        if (a == 14) {return 1;}
        if (a == 15) {return 2;}
        if (a == 16) {return 3;}
        return 0;
    }

項目全部代碼已上傳GitHub,程序界面用的是網上一個仿課程格子比較好的Demo,解析這一塊是寫這個程序時候比較花時間的部分了,拿出來給大家提供思路。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章