7

i write this code to read an Excel file which i paste it in assets folder (my file name:book.xls) to read it. but when i press button to show file it doesn't work and doesn't show anything. please help me to solve my issue. Thanks a lot. here is my code:

package com.example.android.readingexcellfile;

import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.io.InputStream;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void order (View v){

        try

        {
            AssetManager am=getAssets();
            InputStream is=am.open("book.xls");
            Workbook wb=Workbook.getWorkbook(is);
            Sheet s=wb.getSheet(0);
            int row=s.getRows();
            int col=s.getColumns();

            String xx="";
            for (int i=0;i<row;i++)
            {

                for(int c=0;i<col;c++)
                {
                    Cell z=s.getCell(c,i);
                    xx=xx+z.getContents();

                }

                xx=xx+"\n";
            }
            display(xx);
        }

        catch (Exception e){}




    }
    public void display (String value){

        TextView x=(TextView)findViewById(R.id.textView);
        x.setText(value);

    }


}

and here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.readingexcellfile.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="28dp"
        android:layout_marginStart="28dp"
        android:layout_marginTop="69dp"
        android:onClick="order"
        android:id="@+id/button" />
</RelativeLayout>

1
  • 1
    Once you've solved this problem, the next thing that you should consider is "why an Excel file" ? Given that the file is embedded in your assets folder, and the user can't change it, why not just convert it to a more friendly format, like a .csv file, or even create a data class in Java that represents this data? Then, by omitting both the Excel file and the Jexcel library, you'd be making your APK a lot smaller. Commented Jan 8, 2017 at 17:55

1 Answer 1

2

The display() method never gets called as your inner for loop never ends because of the line:

for(int c=0;i<col;c++)

Change this to:

for(int c=0;c<col;c++)

(i.e. check that 'c' is < than col as opposed to 'i') and the TextView value is changed as intended.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.