Skip to content

Instantly share code, notes, and snippets.

@cofearabi
Created October 28, 2014 22:12
Show Gist options
  • Save cofearabi/a62f699b8155c2516954 to your computer and use it in GitHub Desktop.
Save cofearabi/a62f699b8155c2516954 to your computer and use it in GitHub Desktop.
Android sample appli which read Excel file and display the data of the sheet.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="24dp"
android:text="TextView" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.andexcelread0"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
public class MainActivity extends Activity {
String dbStr = Environment.getExternalStorageDirectory() + "/dropbox/xls/stock1.xls";
String strHyouji="";
String[][] arrays = read();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(arrays == null){strHyouji="no such file";}else{
for (String[] array : arrays) {
for (String v : array) {
strHyouji = strHyouji + v + ",";
}
strHyouji = strHyouji + "\n";
}
}
TextView textSetting = (TextView) findViewById(R.id.textView1);
textSetting.setText(strHyouji);
}
public String[][] read() {
Workbook workbook = null;
try {
WorkbookSettings ws = new WorkbookSettings();
ws.setGCDisabled(true);
workbook = Workbook.getWorkbook(new File(dbStr), ws);
Sheet sheet = workbook.getSheet(0);
int rowCount = sheet.getRows();
String[][] result = new String[rowCount][];
for (int i = 0; i < rowCount; i++) {
Cell[] row = sheet.getRow(i);
result[i] = new String[row.length];
for (int j = 0; j < row.length; j++) {
result[i][j] = row[j].getContents();
}
}
return result;
} catch (BiffException e) {
strHyouji=strHyouji+ e.toString();
} catch (IOException e) {
strHyouji=strHyouji+ e.toString();
} catch (Exception e) {
strHyouji=strHyouji+ e.toString();
} finally {
if (workbook != null) {
workbook.close();
}
}
return null;
}
}
@NatiDeme
Copy link

i want write

@nitish8879
Copy link

can you please share the dependancy fro android studio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment