2012-09-01 96 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是你的主要活动内容。