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>
To read data from Excel cells, use the Excel runtime object. In some advanced cases, for instance, when you work with ranges of cells in Excel files, you can use the Excel. Application COM object.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With