speedsir speedsir - 2 months ago 21
Android Question

How to create excel file in android ?

I'm trying to create an excel file in android. But when I click the button to create the file, my app crashes.

LogCat

02-12 17:43:48.287: E/dalvikvm(25342): Could not find class 'jxl.WorkbookSettings', referenced from method lmf.test7.MainActivity.onClick
02-12 17:43:51.257: E/AndroidRuntime(25342): FATAL EXCEPTION: main
02-12 17:43:51.257: E/AndroidRuntime(25342): java.lang.NoClassDefFoundError: jxl.WorkbookSettings
02-12 17:43:51.257: E/AndroidRuntime(25342): at lmf.test7.MainActivity.onClick(MainActivity.java:44)
02-12 17:43:51.257: E/AndroidRuntime(25342): at android.view.View.performClick(View.java:4212)
02-12 17:43:51.257: E/AndroidRuntime(25342): at android.view.View$PerformClick.run(View.java:17476)
02-12 17:43:51.257: E/AndroidRuntime(25342): at android.os.Handler.handleCallback(Handler.java:800)
02-12 17:43:51.257: E/AndroidRuntime(25342): at android.os.Handler.dispatchMessage(Handler.java:100)
02-12 17:43:51.257: E/AndroidRuntime(25342): at android.os.Looper.loop(Looper.java:194)
02-12 17:43:51.257: E/AndroidRuntime(25342): at android.app.ActivityThread.main(ActivityThread.java:5371)
02-12 17:43:51.257: E/AndroidRuntime(25342): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 17:43:51.257: E/AndroidRuntime(25342): at java.lang.reflect.Method.invoke(Method.java:525)
02-12 17:43:51.257: E/AndroidRuntime(25342): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-12 17:43:51.257: E/AndroidRuntime(25342): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-12 17:43:51.257: E/AndroidRuntime(25342): at dalvik.system.NativeStart.main(Native Method)


MainActivity

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {

String Fnamexls="testfile" + ".xls";

File sdCard = Environment.getExternalStorageDirectory();

File directory = new File (sdCard.getAbsolutePath() + "/newfolder");
directory.mkdirs();

File file = new File(directory, Fnamexls);

WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));

WritableWorkbook workbook;
try {
int a = 1;
workbook = Workbook.createWorkbook(file, wbSettings);
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
Label label = new Label(0, 2, "SECOND");
Label label1 = new Label(0,1,"first");
Label label0 = new Label(0,0,"HEADING");
Label label3 = new Label(1,0,"Heading2");
Label label4 = new Label(1,1,String.valueOf(a));

try {
sheet.addCell(label);
sheet.addCell(label1);
sheet.addCell(label0);
sheet.addCell(label4);
sheet.addCell(label3);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
workbook.write();

try {
workbook.close();
} catch (WriteException e) {

e.printStackTrace();
}

} catch (IOException e) {
e.printStackTrace();
}
}

}


Btw, I used Java Excel API.

M D M D
Answer

It seems like jxl.WorkbookSettings not found. add Java Excel API.jar into project libs and clean and built your project.

Follow this steps to add external library or JARS into your project:

right click project->goto properties ->click on "Java Build Path" located on left side-> goto libraries ->you see Add External JARS button click on it and add.