简体   繁体   中英

Reading Excel from src folder using JXL

I am a beginner and having a hard time to get my program run correctly after exporting it as runnable JAR.

I need to read an Excel called "bstn.xls", which is in the dDefault package such as all other classes. Here is how I did it.

  1. get the file path with: URL excelURL = this.getClass().getClassLoader().getResource("bstn.xls");
  2. Create a File with the URL File file = new File(new ResourceLoader().loader());
  3. Create a workbook with the file wbook = Workbook.getWorkbook(file);

I got the error in german here, but it says: the System cannot find the given path. Is JXL not able to read with the full qualified path? I can't find anything on the JXL site.

Since the file is in the classpath, you may use the getWorkbook method taking an InputStream parameter :

InputStream stream = this.getClass().getClassLoader().getResourceAsStream("bstn.xls");
wbook = Workbook.getWorkbook(stream);

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