我想在安卓设备上创建一个Excel文件。但是当我点击按钮创建文件时,我的应用程序崩溃了。
日志
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
中,然后清理并构建您的项目。 - M D右键项目->属性->库->你会看到“添加外部JARS”
按钮,点击它并添加。 - M D