当前位置: 动力学知识库 > 问答 > 编程问答 >

find all .xls file mobile and list then . ( android )

问题描述:

i am developing a app in which i want to list all .xls (excel files ) .

How can i find all .xls find in storage (both internal and external ) ?

And after getting the list i want to read the xls file ,

the file has only two columns , how to read data from that xls file row by row .

the file has one column for name of a person and other for her/his address.

I want to read data from that file and save it to mobile .

tell me how can i find all xls file s and how to read a xls file .

Thanks

网友答案:

Give the permission in your app manifesto:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />

Get SD_CARD path:

String SDCARD_PATH = Environment.getExternalStorageDirectory().getPath() + "/";

Call getXLSFiles():

getXLSFiles(SDCARD_PATH);

In your Activity:

ArrayList<String> FOLDERS = new ArrayList<String>();
ArrayList<String> XLS_FILES = new ArrayList<String>();

int CALL_COUNT = 0;

public void getXLSFiles(String path) {



    try {

            File dirFolder = new File(path);
            File[] folders = dirFolder.listFiles();

            for (File file : folders) {
                if (file.isDirectory()) {
                    FOLDERS.add(path + file.getName() + "/");
                } else {

                    if (file.getName().contains(".xls")) {

                        XLS_FILES.add(path + file.getName());


                    } else {
                        // FILE_PATH = path + file.getName();
                        // Log.e("File Deatils", "" + FILE_PATH);
                    }
                }
            }

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

        while (CALL_COUNT < FOLDERS.size()) {
            String dirURL = FOLDERS.get(CALL_COUNT);
            CALL_COUNT++;

            // Log.e("Folders", "" + dirURL);
            getSdCardFolders(dirURL);
        }

}

And you will get all .xls file in XLS_FILES list. Finally generate the listview as you want.

网友答案:

You can find file from both of directory removable storage or non removable storage Environment.getExternalStorageDirectory()

And than how about searching:

You can check is that directory or file If is it directory than scan whole directory with your file extension mean end with".xls" same process will do for another directory.

分享给朋友:
您可能感兴趣的文章:
随机阅读: