我想在android中創建一個excel文件。但是當我點擊按鈕來創建文件時,我的應用程序崩潰。如何在android中創建excel文件?
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();
}
}
}
順便說一句,我使用的Java Excel的API。
看起來好像沒有找到'jxl.WorkbookSettings'。將'Java Excel API.jar'添加到項目'libs'中並清理並構建您的項目 –
「找不到類'jxl.WorkbookSettings'」 – DevZer0
@MD - 如何將它添加到libs文件夾?當我嘗試將它添加到我的項目中時,我的項目會創建一個名爲「Referenced Libraries」的新文件夾並將其添加到該文件夾中。 – speedsir