Java – Reads the excel sheet of an Android program

Reads the excel sheet of an Android program… here is a solution to the problem.

Reads the excel sheet of an Android program

I’m trying to read an Excel worksheet for an Android application using an Eclipse workspace. I’m putting the file path but can’t even seem to find it. Do I need to declare the path to the file somewhere?

Below is my code. Thanks for your help!

public void onCreate(Bundle savedInstanceState) {
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);    calls VIBRATOR_SERVICE when button is pressed
        screensize = getScreenSize();
        setTheme(android. R.style.Theme_Translucent_NoTitleBar);
        super.onCreate(savedInstanceState);

Eula.show(this);     displays the EULA and asks user to Accept or Don't Accept

try{
            fs = new FileInputStream(new File("/Users/paul/Desktop/CollegeTrack/Samp.xls"));
            InputStream fileinput = fs;
            try{
                Workbook workbook = Workbook.getWorkbook(fileinput);
                sheet = workbook.getSheet(0);
                database_array = Database.classDatabase(sheet); stores each row in an array
                workbook.close();

}
            catch (BiffException e) {
                e.printStackTrace();
            }
                catch (IOException e) {
                e.printStackTrace();
            }
        fs.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
            savedAssignments_array = Database.assignmentDatabase();
            savedCourses_array = Database.savedCourses();
            goToWelcomeScreen();
        }

Solution

It may fail because of the path you are using:

fs = new FileInputStream(new File("/Users/paul/Desktop/CollegeTrack/Samp.xls"));

I’m assuming this points to a file somewhere on your computer because it has “Desktop” in its path. Your emulator is running a completely separate operating system (Android) that does not know the location of the file. In order to access it, you need to either 1) transfer the file to the emulator’s storage space or 2) establish a shared network connection between the two.

Think from the perspective of an actual phone: You can’t point your phone at a file on your desktop. It is a self-contained standalone device. The emulator is just that, it runs right on top of your computer’s operating system.

Related Problems and Solutions