2012-01-09 56 views
2

我正在开发一个应用程序,并且我有一个读取和写入存储在sdcard.I中的Excel文件的要求,我正在使用jxl库来达到此目的。但是我在文件操作中遇到了一些错误。在Excel工作表中写入内容

ext = Environment.getExternalStorageDirectory().getAbsolutePath()+"/hai/dynamic.xls"; 
setOutputFile(ext); 
try { 
    write(); 
} catch (WriteException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
} 
public void setOutputFile(String inputFile) { 
    this.inputfile = inputFile; 
    } 

public void write() throws IOException, WriteException { 
     File file = new File(inputfile); 
     WorkbookSettings wbSettings = new WorkbookSettings(); 

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

     WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings); 
     workbook.createSheet("Report", 0); 
     WritableSheet excelSheet = workbook.getSheet(0); 
     createLabel(excelSheet); 
     createContent(excelSheet); 

     workbook.write(); 
     workbook.close(); 
    } 

然后我在logcat中得到这个以下错误

01-09 12:43:12.824: W/System.err(12321):  java.io.FileNotFoundException:/mnt/sdcard/hai/dynamic.xls (Permission denied) 
01-09 12:43:12.884: W/System.err(12321): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method) 
01-09 12:43:12.884: W/System.err(12321): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232) 
01-09 12:43:12.904: W/System.err(12321): at java.io.FileOutputStream.<init>(FileOutputStream.java:94) 
01-09 12:43:12.904: W/System.err(12321): at java.io.FileOutputStream.<init>(FileOutputStream.java:66) 
01-09 12:43:12.904: W/System.err(12321): at jxl.Workbook.createWorkbook(Workbook.java:301) 
01-09 12:43:12.904: W/System.err(12321): at com.fis.excel.WriteAcivity.write(WriteAcivity.java:59) 
01-09 12:43:12.904: W/System.err(12321): at com.fis.excel.WriteAcivity.onCreate(WriteAcivity.java:40) 
01-09 12:43:12.904: W/System.err(12321): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-09 12:43:12.904: W/System.err(12321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
01-09 12:43:12.916: W/System.err(12321): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
01-09 12:43:12.916: W/System.err(12321): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
01-09 12:43:12.924: W/System.err(12321): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
01-09 12:43:12.924: W/System.err(12321): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-09 12:43:12.924: W/System.err(12321): at android.os.Looper.loop(Looper.java:123) 
01-09 12:43:12.924: W/System.err(12321): at android.app.ActivityThread.main(ActivityThread.java:3683) 
01-09 12:43:12.924: W/System.err(12321): at java.lang.reflect.Method.invokeNative(Native Method) 
01-09 12:43:12.944: W/System.err(12321): at java.lang.reflect.Method.invoke(Method.java:507) 
01-09 12:43:12.944: W/System.err(12321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
01-09 12:43:12.944: W/System.err(12321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
01-09 12:43:12.944: W/System.err(12321): at dalvik.system.NativeStart.main(Native Method) 
+0

检查你的路径,并打印此...的System.out .println(“File is ::”+ ext); – 2012-01-09 08:46:38

回答

2

//添加使用许可在您的清单

WRITE_EXTERNAL_STORAGE 

Allows an application to write to external storage 


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+1

谢谢...它的工作原理... – 2012-01-09 08:38:57

相关问题