Application crashed while Writing data to excel file in android -
i writing application writes data excel file in android...but application crashes please me did mistake
here's mainactivity.java
package com.example.excel_file; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.util.iterator; import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.hssf.usermodel.hssfcellstyle; import org.apache.poi.hssf.usermodel.hssfrow; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.hssf.util.hssfcolor; import org.apache.poi.poifs.filesystem.poifsfilesystem; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.cellstyle; import org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.usermodel.sheet; import org.apache.poi.ss.usermodel.workbook; import android.app.activity; import android.content.context; import android.os.bundle; import android.os.environment; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.toast; public class mainactivity extends activity implements onclicklistener { button writeexcelbutton,readexcelbutton; static string tag = "exellog"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); writeexcelbutton = (button) findviewbyid(r.id.button1); writeexcelbutton.setonclicklistener(this); readexcelbutton = (button) findviewbyid(r.id.button2); readexcelbutton.setonclicklistener(this); } public void onclick(view v) { switch (v.getid()) { case r.id.button1: saveexcelfile(this,"myexcel.xls"); break; case r.id.button2: readexcelfile(this,"myexcel.xls"); break; } } private static boolean saveexcelfile(context context, string filename) { // check if available , not read if (!isexternalstorageavailable() || isexternalstoragereadonly()) { log.e(tag, "storage not available or read only"); return false; } boolean success = false; //new workbook workbook wb = new hssfworkbook(); cell c = null; //cell style header row cellstyle cs = wb.createcellstyle(); cs.setfillforegroundcolor(hssfcolor.lime.index); cs.setfillpattern(hssfcellstyle.solid_foreground); //new sheet sheet sheet1 = null; sheet1 = wb.createsheet("myorder"); // generate column headings row row = sheet1.createrow(0); c = row.createcell(0); c.setcellvalue("item number"); c.setcellstyle(cs); c = row.createcell(1); c.setcellvalue("quantity"); c.setcellstyle(cs); c = row.createcell(2); c.setcellvalue("price"); c.setcellstyle(cs); sheet1.setcolumnwidth(0, (15 * 500)); sheet1.setcolumnwidth(1, (15 * 500)); sheet1.setcolumnwidth(2, (15 * 500)); // create path place our list of objects on external storage file file = new file(context.getexternalfilesdir(null), filename); fileoutputstream os = null; try { os = new fileoutputstream(file); wb.write(os); log.w("fileutils", "writing file" + file); success = true; } catch (ioexception e) { log.w("fileutils", "error writing " + file, e); } catch (exception e) { log.w("fileutils", "failed save file", e); } { try { if (null != os) os.close(); } catch (exception ex) { } } return success; } private static void readexcelfile(context context, string filename) { if (!isexternalstorageavailable() || isexternalstoragereadonly()) { log.e(tag, "storage not available or read only"); return; } try{ // creating input stream file file = new file(context.getexternalfilesdir(null), filename); fileinputstream myinput = new fileinputstream(file); // create poifsfilesystem object poifsfilesystem myfilesystem = new poifsfilesystem(myinput); // create workbook using file system hssfworkbook myworkbook = new hssfworkbook(myfilesystem); // first sheet workbook hssfsheet mysheet = myworkbook.getsheetat(0); /** need iterate through cells.**/ iterator rowiter = mysheet.rowiterator(); while(rowiter.hasnext()){ hssfrow myrow = (hssfrow) rowiter.next(); iterator celliter = myrow.celliterator(); while(celliter.hasnext()){ hssfcell mycell = (hssfcell) celliter.next(); log.d(tag, "cell value: " + mycell.tostring()); toast.maketext(context, "cell value: " + mycell.tostring(), toast.length_short).show(); } } }catch (exception e){e.printstacktrace(); } return; } public static boolean isexternalstoragereadonly() { string extstoragestate = environment.getexternalstoragestate(); if (environment.media_mounted_read_only.equals(extstoragestate)) { return true; } return false; } public static boolean isexternalstorageavailable() { string extstoragestate = environment.getexternalstoragestate(); if (environment.media_mounted.equals(extstoragestate)) { return true; } return false; } }
this log cat:
01-02 02:29:39.550: e/androidruntime(6111): fatal exception: main 01-02 02:29:39.550: e/androidruntime(6111): java.lang.noclassdeffounderror: org.apache.poi.hssf.usermodel.hssfworkbook 01-02 02:29:39.550: e/androidruntime(6111): @ com.example.excel_file.mainactivity.saveexcelfile(mainactivity.java:71) 01-02 02:29:39.550: e/androidruntime(6111): @ com.example.excel_file.mainactivity.onclick(mainactivity.java:52) 01-02 02:29:39.550: e/androidruntime(6111): @ android.view.view.performclick(view.java:4240) 01-02 02:29:39.550: e/androidruntime(6111): @ android.view.view$performclick.run(view.java:17721) 01-02 02:29:39.550: e/androidruntime(6111): @ android.os.handler.handlecallback(handler.java:730) 01-02 02:29:39.550: e/androidruntime(6111): @ android.os.handler.dispatchmessage(handler.java:92) 01-02 02:29:39.550: e/androidruntime(6111): @ android.os.looper.loop(looper.java:137) 01-02 02:29:39.550: e/androidruntime(6111): @ android.app.activitythread.main(activitythread.java:5103) 01-02 02:29:39.550: e/androidruntime(6111): @ java.lang.reflect.method.invokenative(native method) 01-02 02:29:39.550: e/androidruntime(6111): @ java.lang.reflect.method.invoke(method.java:525) 01-02 02:29:39.550: e/androidruntime(6111): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 01-02 02:29:39.550: e/androidruntime(6111): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 01-02 02:29:39.550: e/androidruntime(6111): @ dalvik.system.nativestart.main(native method)
this manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.excel_file" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="18" /> <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.write_external_storage"/> </manifest>
you have noclassdeffounderror. think, during compilation org.apache.poi.hssf.usermodel.hssfworkbook class present wasn't found in device during runtime. here can read explanation https://stackoverflow.com/a/34419/1555366
Comments
Post a Comment