阅读并显示包含的apache poi xls

时间:2016-08-24 13:09:49

标签: android apache apache-poi xls

我想在textview中显示我的Excel文件的内容。我用这段代码读了它:

private void readExcelFile(Context context, String filename) {

    if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
        Log.w("FileUtils", "Storage not available or read only");
        return;
    }

    try {
        // Creating Input Stream
        File file = new File(context.getExternalFilesDir(null), filename);
        FileInputStream myInput = new FileInputStream(file);

        // Create a POIFSFileSystem object
        POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

        // Create a workbook using the File System
        HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

        // Get the first sheet from workbook
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);

        /** We now need something to iterate through the cells.**/
        Iterator<Row> rowIter = mySheet.rowIterator();

        while (rowIter.hasNext()) {
            HSSFRow myRow = (HSSFRow) rowIter.next();
            Iterator<Cell> cellIter = myRow.cellIterator();
            while (cellIter.hasNext()) {
                HSSFCell myCell = (HSSFCell) cellIter.next();
                Log.w("FileUtils", "Cell Value: " + myCell.toString());
                Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
                //final TextView textViewToChange = (TextView) findViewById(R.id.affichage);
               // textViewToChange.setText((CharSequence) myCell);
                Log.d("Debug", String.valueOf(myCell));

            }

        }


    } catch (Exception e) {
        e.printStackTrace();
    }

    return;
}

如果我必须以其他方式阅读文件,这没有问题。 你能帮帮我吗?

0 个答案:

没有答案