Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Android application which get the price of the stock written in Excel file
<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.andexcelread"
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.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>
import java.io.File;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import android.app.Activity;
import android.os.AsyncTask;
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();
int line=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(arrays == null ){
strHyouji = "failure in reading Excel file";
}else{
for (String[] array : arrays) {
strHyouji = strHyouji + array[0];
System.out.println();
strHyouji = strHyouji + "\n";
}
}
TextView textSetting = (TextView) findViewById(R.id.textView1);
textSetting.setText(strHyouji);
if(arrays != null){
new MyTask().execute();
}
}
private class MyTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String url = "http://stocks.finance.yahoo.co.jp/stocks/detail/?code=6954.T";
String arg1 ="-----";
String arg2 ="-----";
String strKaeshi = "";
for (String[] array : arrays) {
try{
url = "http://stocks.finance.yahoo.co.jp/stocks/detail/?code=" + array[0] + ".T";
Document doc = Jsoup.connect(url).get();
Elements stoksPrices=doc.select("td[class=stoksPrice]");
Elements reals=doc.select("dd[class=yjSb real]");
for (Element stoksPrice : stoksPrices){
System.out.println(stoksPrice.text());
arg2 =stoksPrice.text();
System.out.println(arg2.replace(",",""));
arg2=(arg2.replace(",",""));
}
for (Element real : reals){
System.out.println(real.child(0).text());
arg1 = real.child(0).text();
}
} catch (Exception e) {
arg2=e.toString();
}
strKaeshi = strKaeshi + arg1 + " " + arg2 + "\n";
arg1 ="-----";
arg2 ="-----";
}
return strKaeshi;
}
@Override
protected void onPostExecute(String result) {
((TextView)findViewById (R.id.textView1)).setText(result);
}
}
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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (workbook != null) {
workbook.close();
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment