Need Help in Importing data's from Excel in Android?
I had used JExcel
API for importing Data's from Excel,for using JExcel , I have written the following program:
public class ReadExcel {
private String inputFile;
public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}
public void read() throws IOException {
File inputWorkbook = new File(inputFile);
File parent_dir = inputWorkbook.getParentFile();
Workbook w;
try {
System.out.println(开发者_开发技巧"Parent dir"+parent_dir);
if(parent_dir.exists() == true){
System.out.println("Pardent_dir failed"+"1");
}
else
{
System.out.println("Pardent _ dir not failed"+"2");
}
if(inputWorkbook.exists()== true)
{
System.out.println("File Exists");
}
else
{
System.out.println("File NOt Exists");
System.out.println("Path "+inputWorkbook.getAbsoluteFile());
}
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
for (int j = 0; j < sheet.getColumns(); j++) {
for (int i = 0; i < sheet.getRows(); i++) {
Cell cell = sheet.getCell(j, i);
CellType type = cell.getType();
if (cell.getType() == CellType.LABEL) {
System.out.println("I got a label "
+ cell.getContents());
}
if (cell.getType() == CellType.NUMBER) {
System.out.println("I got a number "
+ cell.getContents());
}
}
}
} catch (BiffException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
ReadExcel test = new ReadExcel();
test.setInputFile("c:/temp/Book2.xls");
test.read();
}
}
It works fine, but when i had used it in Android, I had got the following Exception
03-18 16:33:31.225: INFO/System.out(8693): Parent dirc:/temp 03-18 16:33:31.225: INFO/System.out(8693): Pardent _ dir not failed2 03-18 16:33:31.235: INFO/System.out(8693): File NOt Exists 03-18 16:33:31.235: INFO/System.out(8693): Path /c:/temp/Book2.xls 03-18 16:33:31.245: WARN/System.err(8693): java.io.FileNotFoundException: /c:/temp/Book2.xls 03-18 16:33:31.255: WARN/System.err(8693): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:244) 03-18 16:33:31.255: WARN/System.err(8693): at java.io.FileInputStream.(FileInputStream.java:77) 03-18 16:33:31.255: WARN/System.err(8693): at jxl.Workbook.getWorkbook(Workbook.java:213) 03-18 16:33:31.255: WARN/System.err(8693): at jxl.Workbook.getWorkbook(Workbook.java:198) 03-18 16:33:31.255: WARN/System.err(8693): at com.san.test.MyActivity.read(MyActivity.java:93) 03-18 16:33:31.255: WARN/System.err(8693): at com.san.test.MyActivity.displayAlert(MyActivity.java:62)
test.setInputFile("c:/temp/Book2.xls");
Android is a linux-based system, so there are no drives in there. Using "c:/" has no sence in android. Please, read the article about data storage. You can use external storage to place your files and read from there.
Error log says that FileNotFoundException
. So probably your path is missing here. You can push your excel file in the /mnt/sdcard/
directory and then you can use like that:
test.setInputFile("/mnt/sdcard/Book2.xls");
I had fixed my issue, by placing the Book2.xls file in under raw folder and then read that file from that folder.
Thanks
精彩评论