0

I am getting following error when I try to read an excel file in java although the file is present. I am in Windows environment. I have confirmed that that the file is present at that location.

java.io.FileNotFoundException: D:\myfile.xls (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at jxl.Workbook.getWorkbook(Workbook.java:213)

My code is as follows:

       wb = Workbook.getWorkbook(new File("D:/myfile.xls"));
ollo
  • 24,797
  • 14
  • 106
  • 155
user2314864
  • 19
  • 1
  • 1

3 Answers3

2

have tried with

   wb = Workbook.getWorkbook(new File("D:\\myfile.xls"));

Al so see :File.separator or File.pathSeparator

Make sure that your file is in D drive.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Thanks for your reply. I have ensured that the file is present in D drive and the case and extension is right. I have also tried with "D:\\myfile.xls", but I get same error. – user2314864 Apr 24 '13 at 09:44
  • see this http://stackoverflow.com/questions/3059383/file-path-windows-format-to-java-format – Suresh Atta Apr 24 '13 at 09:45
0

Let me give you my example, code has been tested and let me know if this works.

File fileExcel = new File("D:/DATAVALUE.xls");

Workbook w;

w = Workbook.getWorkbook(fileExcel); Sheet sheet = w.getSheet(0);

I tested this code and let me captured this one for you.

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package stackoverflow;

import java.io.File; import jxl.Cell; import jxl.Sheet; import jxl.Workbook;

/** * * @author jwijaya */ public class Stackoverflow {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    try
                    {

                    File fileExcel = new File("D:/tes.xls");
                    Workbook w;
                    w = Workbook.getWorkbook(fileExcel);
                    Sheet sheet = w.getSheet(0);

                    for (int j = 0; j < sheet.getRows(); j++) {
                        for (int i = 0; i < sheet.getColumns(); i++) {
                            Cell cell = sheet.getCell(i, j);


                                                if((j==0 && i==0 ) || (j==0 && i==1) ||  (j==0 && i==2) || (j==0 && i==3))
                                                {
                                                System.out.println("Isi tabel "
                                                + cell.getContents());
                                                }

                        }
                                                Cell tanggal = sheet.getCell(0,j);
                                                String tanggalfinal = tanggal.getContents(); 
                                                System.out.println(tanggalfinal);

                                                Cell datausd = sheet.getCell(1,j);
                                                String datausdfinal = datausd.getContents(); 
                                                System.out.println(datausdfinal);

                                                Cell datajapan = sheet.getCell(2,j);
                                                String datajapanfinal = datajapan.getContents(); 
                                                System.out.println(datajapanfinal);


                                                Cell dataaus = sheet.getCell(3,j);
                                                String dataausfinal = dataaus.getContents(); 
                                                System.out.println(dataausfinal);



                    }

                    }
                    catch (Exception e)
                    {
                    System.err.println("Got an exception!");
                    System.err.println(e.getMessage());
                    }
} }

Download this file

https://hotfile.com/dl/204326337/3f04919/tes.xls.html
  • Don't forget to add your libraries. JXL –  Apr 24 '13 at 09:49
  • Thanks for the reply. I have created file DATAVALUE.xls in D drive but I still get the same error: java.io.FileNotFoundException: D:\DATAVALUE.xls (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at jxl.Workbook.getWorkbook(Workbook.java:213) at jxl.Workbook.getWorkbook(Workbook.java:198) – user2314864 Apr 24 '13 at 09:59
  • Please accept this question if this is true, please save your document within the 97-2003 workbook then it will be xls I think you save within the xlsx format. After you try let me know –  Apr 24 '13 at 10:26
  • Thanks, but i am getting this error for any doc not only xls. Even if I create a notepad file, I get the same error. – user2314864 Apr 24 '13 at 10:29
  • Can anyone please help, I am unable to move ahead. I do not know why I am getting this error although the file is present at the location. – user2314864 Apr 24 '13 at 10:57
0

In case you created the excel file beforehand, try to save it as a 97-2000 worksheet (So the old Excel format).

Hendrik
  • 153
  • 1
  • 2
  • 18