如何限制通过Apache POI从Excel文件读取行

时间:2019-06-09 19:06:54

标签: java android excel apache apache-poi

我正在尝试通过Android Studio使用Apache POI从Excel文件中读取和显示信息。我对列的“限制”做得很好,但是由于某些原因,不确定如何处理行。

通过SharedPreferences,我选择了一个值,并使用简单的逻辑表达式来“限制”应用程序应显示的内容,它只是显示所选列中的所有行。

public void readExcelFileFromAssets() {

    //getting shared preferences
    SharedPreferences pref = getSharedPreferences("MyPref", 0);
    String x = pref.getString("key_str", "notext");
    String y = pref.getString("key_day", "notext");

    try {

        InputStream myInput;
        AssetManager assetManager = getAssets();
        myInput = assetManager.open("III_Kursas.xls");
        POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
        HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);

        Iterator<Row> rowIter = mySheet.rowIterator();
        int rowno;
        int limit;

        if (y.equals("mon")){
            rowno = 0;
            limit = 8;
        }
        else if (y.equals("tue")){
            textView.append("FUCKO");
            rowno = 8;
            limit = 16;
        }
        else if (y.equals("wed")){
            textView.append("BUCKO");
            rowno = 16;
            limit = 24;
        }
        else if (y.equals("thr")){
            rowno = 24;
            limit = 32;
        }
        else {
            rowno = 32;
            limit = 40;
        }

        while (rowIter.hasNext()
                //rowno < limit
        ){
            //Log.e(TAG, " row no " + rowno);
            HSSFRow myRow = (HSSFRow) rowIter.next();

            if (rowno != 0){
                Iterator<Cell> cellIter = myRow.cellIterator();
                int colno = 0;

                String[] column = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"};

                while (cellIter.hasNext()){
                    HSSFCell myCell = (HSSFCell) cellIter.next();

                    column[colno] = myCell.toString();

                    //Log.e(TAG, " Index :" + myCell.getColumnIndex() + " -- " + myCell.toString());

                    colno++;
                }

                if (x.equals("i16")){
                    textView.append(column[2] + "\n");
                }
                else if (x.equals("v16")){
                    textView.append(column[3] + "\n");
                }
                else if (x.equals("p16")){
                    textView.append(column[4] + "\n");
                }
                else if (x.equals("s16")){
                    textView.append(column[5] + "\n");
                }
                else {
                    textView.append("Failed");
                }
            }
            rowno++;
        }

    } catch (Exception e){
        Log.e(TAG, "error" + e.toString());
    }
}

没有错误消息,只希望应用程序显示基于变量的选定行。

0 个答案:

没有答案