简体   繁体   中英

Java- Error with Eclipse reading excel file

I'm new to java, but a quick learner. I'm trying make my Android App to take user-input from an EditText, search an Excel column for a matching string, and return the value of (the same row of) another column.

I added a jxl.jar on the build path, and my code seems like it should work. Teaching myself, I am slowly figuring out the debugging. The problem seems to be source android.jar was not found (if i remember correctly). I located it, and now I get "the source attachment does not contain the source for the file instrumentation.class"

This is my first app using excel. Do I need to address the Android Manifest somehow?

Here's my code, just in case, and any help is very appreciated!:

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

You are trying to access c: drive ( "c:/Desktop/AAS Work/PDA Programs/JBDB.xls" ) from your emulator, which is not possible. You need to put your excel file either in the resources folder or in SD card, if you are running it in device.

An Emulator has a virtual SD Card, and its limitations is that it can not direct access any other drive from the computer. However you can do it by making a webservice which will perform operation on behalf of your android application. You need to call this web-service from your android application.

Put the file in assets folder and access it like this:

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 is your main activity context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM