Android learning essay (16) --- Analysis of Excel table

Keywords: Excel github Database

Recently completed a small Demo of curriculum, and the code has been uploaded to GitHub
The completion mainly depends on the jar package tool jxl.jar

Sample Excel file:

Reading and analysis of Excel file

    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++) {    // Get the number of sheet pages
                List<Integer> arrayList = new ArrayList<>();    // Number of nodes saved
                Map<String, WeekDay> map = new HashMap<String, WeekDay>();    // One day course for class storage
                Sheet sheet = book.getSheet(n);
                for (int i = 2; i< sheet.getColumns(); i++) {    // Initialize map class table
                    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("Remarks"); i++, j++) {    // How many classes are there every day in a grade
                    if (sheet.getCell(0, i).getContents().contains("week")) {
                        if (j > 0) {
                            arrayList.add(j);
//                                    Log. D ("file", j + "" + sheet. Getcell (0, I). Getcontents()); / / level 16 table 6 12 18 24 30 31
                        }
                    }
                }
                arrayList.add(j);
                arrayList.add(arrayList.get(arrayList.size() - 1) + 1);    // Remarks
//                        For (int k = 0; K < ArrayList. Size(); K + +) {/ / test days
//                            System.out.println(arrayList.get(k));
//                        }

                int row = i;    // Number of real rows - 1 from 0
                int col = sheet.getColumns();    // Number of real columns from 1
                int l;

                WeekDay wd = null;
                List<Section> sections = null;
                for (int k = 1; k < arrayList.size(); k++) {
                    for (i = 2; i < col; i++) {    // Class cycle
                        Section s = null;
                        wd = new WeekDay();
                        sections = new ArrayList<>();
                        for (j = arrayList.get(k - 1) + 1, l = 1; j <= arrayList.get(k); j++, l++) {    // Curriculum cycle
                            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);
                        }    // One day course
                        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()) {
//                                 Call database
                        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();
        }
    }

Write back to Excel table. Because of permission, the process of write back operation is to create a new copy and then modify the data

    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());
            // Determine cell type and make corresponding conversion
            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);    // Increase 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;
    }

The whole code of the project has been uploaded to GitHub, and the program interface is a Demo with good imitation of the course grid on the Internet. Parsing this part is the part that takes a lot of time to write this program, so take it out to provide you with ideas.

Posted by mezise on Mon, 06 Apr 2020 09:14:34 -0700