2011-03-18 61 views
0

我用了JExcel API导入数据的从Excel,使用JExcel,我写了下面的程序:在Android中从Excel导入数据需要帮助吗?

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(); 
    } 

} 

它工作正常,但是当我已经在Android中使用它,我已经得到了以下异常

03-18 16:33:31.225: INFO/System.out的(8693):家长 DIRC:/温度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):在 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):在 jxl.Workbook.getWorkbook(Workbook.java:198) 03-18 16:33:31.255: WARN/System.err的(8693):在 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)

回答

3
test.setInputFile("c:/temp/Book2.xls"); 

Android是一个基于Linux的系统,所以没有驱动器在那里。在android中使用“c:/”没有出现。请阅读关于data storage的文章。您可以使用external storage来放置文件并从那里读取。

+0

无法获取您的信息,请告诉清楚,那又怎么能做到这一点 – 2011-03-18 11:12:35

+0

你不能使用“C:”在android系统。没有一个。使用/ data/...等等。阅读这篇文章,了解更深入的:http://developer.android.com/guide/topics/data/data-storage.html – 2011-03-18 11:13:49

+0

谢谢,我会读它 – 2011-03-18 11:15:02

0

错误日志说FileNotFoundException。所以可能你的路径在这里不见了。你可以把你的Excel文件在/mnt/sdcard/目录中,然后你可以使用这样的:

test.setInputFile("/mnt/sdcard/Book2.xls"); 
+2

不要使用/ mnt/sdcard /而是你可以使用getExternalDirectory()函数获取路径。 – Reno 2011-03-18 12:27:50

+2

@Reno:是的,我知道这一点,但这已经由弗拉基米尔伊万诺夫**解释过,这就是为什么我只能这样做。 – 2011-03-18 12:33:36

0

我已经固定我的问题,从该文件夹放置Book2.xls中文件的原始文件夹下,然后读取该文件。

感谢