2012-09-01 127 views
1

我是新來的java,但一個快速學習者。 我試圖讓我的Android應用程序從EditText獲取用戶輸入,在Excel列中搜索匹配的字符串,並返回另一列(同一行)的值。Java-錯誤與Eclipse閱讀excel文件

我在構建路徑上添加了一個jxl.jar,並且我的代碼看起來應該可以工作。教我自己,我正在慢慢地搞清楚調試。這個問題似乎是源android.jar沒有找到(如果我沒記錯的話)。我找到它,現在我得到「源附件不包含文件instrumentation.class源」

這是我第一個使用excel的應用程序。我需要以某種方式解決Android Manifest?

這裏是我的代碼,以防萬一,任何幫助是非常讚賞!:

public class Frame_Bore extends Activity { 
private String inputFile; 

public void setInputFile(String inputFile) { 
    this.inputFile = "c:/Desktop/AAS Work/PDA Programs/JBDB.xls"; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.framebore); 

    Button Btn1 = (Button) findViewById(R.id.button1); 
    final EditText T1 = (EditText) findViewById(R.id.et1); 
    final EditText T2 = (EditText) findViewById(R.id.et2); 

    Btn1.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      String t1Value = T1.getText().toString(); 
      File inputWorkbook = new File(inputFile); 
      Workbook w; 
      String bore = null; 

      try { 
       w = Workbook.getWorkbook(inputWorkbook); 
       Sheet sheet = w.getSheet("Frame-Bore"); 

       int y = 6; 
       while (y < 200) { 
        int x = 2; 
        Cell columnB = sheet.getCell(x, y); 
        String motorframe = columnB.getContents(); 

        if (motorframe == t1Value) { 
         Cell columnC = sheet.getCell(x + 1, y); 
         bore = columnC.getContents(); 
        } else { 
         y = y + 1; 
        } 

       } 
      } catch (BiffException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       T2.setText("" + bore); 
      } 
     } 
    }); 
} 

回答

0

您試圖訪問C:驅動器(「C:/桌面/ AAS工作/ PDA程序/ JBDB。 xls「)從你的模擬器,這是不可能的。如果您正在設備中運行,則需要將您的excel文件放在資源文件夾或SD卡中。

模擬器有一個虛擬SD卡,其侷限性在於它無法直接從計算機訪問任何其他驅動器。但是,您可以通過製作一個web服務來代替您的android應用程序執行操作。你需要從你的android應用程序調用這個web服務。

0

放入資產文件夾中的文件和訪問它是這樣的:

InputStream is = null; 
     try { 
      is = context.getAssets().open("JBDB.xls"); 
      workbook = Workbook.getWorkbook(is); 

     } catch (IOException e) { 

      e.printStackTrace(); 
     } catch (BiffException e) { 

      e.printStackTrace(); 

     } 
     if(workbook!=null) 
     sheet = workbook.getSheet("Frame-bore"); 

context是你的主要活動內容。