Reading excel file in android.java

I am writing this code to read an Excel file, which I paste into the resource folder (my file name: book.xls) to read it. but when I click the button to show the file, it does not work and shows nothing. please help me solve my problem. Many thanks. 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);

    }


}
Run codeHide result

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>
Run codeHide result
+4
source share
1 answer

The method is display()never called, since your inner loop fornever ends because of a line:

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

Change this to:

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

(i.e. check that there is 'c' <than col, in contrast to 'i'), and the value TextViewchanges as intended.

+1
source

Source: https://habr.com/ru/post/1666093/


All Articles