我正在编写一个程序,它接受输入并将它们放入excel文件并保存它的外部存储设备。我出于某种原因无法保存文件,尽管经过多次尝试和数小时的研究! 这是我的代码:
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath());
if(!directory.isDirectory()){
directory.mkdirs();
}
File file = new File(directory, file_name);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook;
Context context = this;
try{
workbook = Workbook.createWorkbook(file,wbSettings);
WritableSheet wsheet = workbook.createSheet("Cable Support Containment Check List", 0);
try{
Label label = new Label(0, 1, dir1);
Label label1 = new Label(1, 1, dir2);
Label label2 = new Label(0, 2, jr1);
Label label3 = new Label(1, 2, jr2);
Label label4 = new Label(0, 3, sd1);
Label label5 = new Label(1, 3, sd2);
Label label6 = new Label(0, 4, a1);
Label label7 = new Label(1, 4, a2);
Label label8 = new Label(0, 5, m1);
Label label9 = new Label(1, 5, m2);
Label label10 = new Label(0, 6, dn1);
Label label11 = new Label(1, 6, dn2);
Label label12 = new Label(0, 7, r1);
Label label13 = new Label(1, 7, r2);
Label label14 = new Label(0, 8, q1);
Label label15 = new Label(1, 8, q1a);
Label label16 = new Label(0, 9, q1c);
Label label17 = new Label(1, 9, q1ca);
Label label18 = new Label(0, 10, q2);
Label label19 = new Label(1, 10, q2a);
Label label202 = new Label(0, 11, q2c);
Label label203 = new Label(1, 11, q2ca);
Label label20 = new Label(0, 12, q3);
Label label21 = new Label(1, 12, q3a);
Label label22 = new Label(0, 13, q3c);
Label label23 = new Label(1, 13, q3ca);
Label label24 = new Label(0, 14, q4);
Label label25 = new Label(1, 14, q4a);
Label label26 = new Label(0, 15, q4c);
Label label27 = new Label(1, 15, q4ca);
Label label28 = new Label(0, 16, q5);
Label label29 = new Label(1, 16, q5a);
Label label30 = new Label(0, 17, q5c);
Label label31 = new Label(1, 17, q5ca);
Label label32 = new Label(0, 18, q6);
Label label33 = new Label(1, 18, q6a);
Label label34 = new Label(0, 19, q6c);
Label label35 = new Label(1, 19, q6ca);
Label label36 = new Label(0, 20, q7);
Label label37 = new Label(1, 20, q7a);
Label label38 = new Label(0, 21, q7c);
Label label39 = new Label(1, 21, q7ca);
Label label40 = new Label(0, 22, q8);
Label label41 = new Label(1, 22, q8a);
Label label42 = new Label(0, 23, q8c);
Label label43 = new Label(1, 23, q8ca);
Label label44 = new Label(0, 24, q9);
Label label45 = new Label(1, 24, q9a);
Label label46 = new Label(0, 25, q9c);
Label label47 = new Label(1, 25, q9ca);
wsheet.addCell(label);
wsheet.addCell(label1);
wsheet.addCell(label2);
wsheet.addCell(label3);
wsheet.addCell(label4);
wsheet.addCell(label5);
wsheet.addCell(label6);
wsheet.addCell(label7);
wsheet.addCell(label8);
wsheet.addCell(label9);
wsheet.addCell(label10);
wsheet.addCell(label11);
wsheet.addCell(label12);
wsheet.addCell(label13);
wsheet.addCell(label14);
wsheet.addCell(label15);
wsheet.addCell(label16);
wsheet.addCell(label17);
wsheet.addCell(label18);
wsheet.addCell(label19);
wsheet.addCell(label20);
wsheet.addCell(label21);
wsheet.addCell(label22);
wsheet.addCell(label23);
wsheet.addCell(label24);
wsheet.addCell(label25);
wsheet.addCell(label26);
wsheet.addCell(label27);
wsheet.addCell(label28);
wsheet.addCell(label29);
wsheet.addCell(label30);
wsheet.addCell(label31);
wsheet.addCell(label32);
wsheet.addCell(label33);
wsheet.addCell(label34);
wsheet.addCell(label35);
wsheet.addCell(label36);
wsheet.addCell(label37);
wsheet.addCell(label38);
wsheet.addCell(label39);
wsheet.addCell(label40);
wsheet.addCell(label41);
wsheet.addCell(label42);
wsheet.addCell(label43);
wsheet.addCell(label44);
wsheet.addCell(label45);
wsheet.addCell(label46);
wsheet.addCell(label47);
wsheet.addCell(label202);
wsheet.addCell(label203);
} catch (RowsExceededException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "wrong",
Toast.LENGTH_SHORT).show();
} catch (WriteException e) {
Toast.makeText(getBaseContext(), "wrong",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
workbook.write();
try {
workbook.close();
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();
startActivity(new Intent(FirstForm.this, Data.class));
} catch (WriteException e) {
Toast.makeText(getBaseContext(), "wrong",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
} catch (IOException e) {
Toast.makeText(getBaseContext(), "wrong",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
我永远无法通过捕获,我不知道为什么,有没有人有任何想法?提前谢谢。