我正在编写一个将数据写入android中的excel文件的应用程序...但我的应用程序崩溃请帮助我在哪里做错了
这是我的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 and not read only
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 for 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 a path where we will 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 to save file", e);
} finally {
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 a POIFSFileSystem object
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
// Create a workbook using the File System
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
// Get the first sheet from workbook
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
/** We now need something to iterate through the 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;
}
}
这是我的日志猫:
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): at com.example.excel_file.MainActivity.saveExcelFile(MainActivity.java:71)
01-02 02:29:39.550: E/AndroidRuntime(6111): at com.example.excel_file.MainActivity.onClick(MainActivity.java:52)
01-02 02:29:39.550: E/AndroidRuntime(6111): at android.view.View.performClick(View.java:4240)
01-02 02:29:39.550: E/AndroidRuntime(6111): at android.view.View$PerformClick.run(View.java:17721)
01-02 02:29:39.550: E/AndroidRuntime(6111): at android.os.Handler.handleCallback(Handler.java:730)
01-02 02:29:39.550: E/AndroidRuntime(6111): at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 02:29:39.550: E/AndroidRuntime(6111): at android.os.Looper.loop(Looper.java:137)
01-02 02:29:39.550: E/AndroidRuntime(6111): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-02 02:29:39.550: E/AndroidRuntime(6111): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 02:29:39.550: E/AndroidRuntime(6111): at java.lang.reflect.Method.invoke(Method.java:525)
01-02 02:29:39.550: E/AndroidRuntime(6111): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-02 02:29:39.550: E/AndroidRuntime(6111): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 02:29:39.550: E/AndroidRuntime(6111): at dalvik.system.NativeStart.main(Native Method)
这是我的清单:
<?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>
答案 0 :(得分:0)
你有NoClassDefFoundError。我认为,在编译期间, org.apache.poi.hssf.usermodel.HSSFWorkbook 类存在,但在运行时期间未在您的设备中找到它。在这里,您可以阅读解释https://stackoverflow.com/a/34419/1555366