Soru & Cevap

Android de excel den veri okuma işlemini nasıl yapabilirim ?

12.05.2014 - 02:08

Satır ve sütünü kodun içersinde belirteceğim ve sunucu daki bi excel dosyasında veriyi okuyup ekrana nasıl yazdırabiliirm ?

575 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-49950-1547400129.jpg
zafercinar
04.12.2019 - 11:24

Merhaba Fatih,

Sorunun hala devam ediyor mu bilmiyorum fakat aşağıdaki link senin problemini çözecektir.

Link: https://gist.github.com/cofearabi/a62f699b8155c2516954

Kodlar;

activity_main.xml

<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>

AndroidManifest.xml

<?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>

MainActivity.java

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;
    }
	
}

Bol kodlu günler :)