-4
public static void main(String []args)throws IOException
{
    //FileInputStream file = new FileInputStream(new File("test.xlsx"));
    FileInputStream fis = new FileInputStream(new File("test.xlsx"));
    XSSFWorkbook workbook = new XSSFWorkbook (fis);
    XSSFSheet sheet = workbook.getSheetAt(0);
    //Create First Row
    XSSFRow row1 = sheet.createRow(0);
    XSSFCell r1c1 = row1.createCell(0);
    r1c1.setCellValue("Emd Id");
    fis.close();
    try
    {
    FileOutputStream fos =new FileOutputStream(new File("test.xlsx"));
    workbook.write(fos);
    fos.close();
    System.out.println("Done");
    }
    catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }
}

The error is:

Exception in thread "main" java.io.FileNotFoundException: test.xlsx (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at excel.CxssfWe1.main(CxssfWe1.java:25)
tshepang
  • 12,111
  • 21
  • 91
  • 136
krishnadhar
  • 73
  • 1
  • 6
  • 1
    You need to post a specific question, not just a bunch of code. – Mwigs Jul 31 '14 at 11:19
  • Your input file is not found. So your program will crash at this line: `FileInputStream fis = new FileInputStream(new File("test.xlsx"));` – Jens Jul 31 '14 at 11:26
  • Did you actually read the error message? They are not there just for fun you know. – The Blue Dog Jul 31 '14 at 11:26
  • i am new to java dev. just in a learning phase and while while writing code got this issue. like am able to create a HSSFWorkbook i.e. xls workbook but not xlsx workbook. Now how should i ask a specific question can u explain! – krishnadhar Jul 31 '14 at 11:26
  • yup i have read it. and i am unable to get it right!. and i know they are not just for fun. – krishnadhar Jul 31 '14 at 11:27
  • you need to put it in a folder where the app sees it which I think is the project folder – EpicPandaForce Jul 31 '14 at 11:36
  • The way you wrote your code doesn't imply the creation of a new file by the way, it tries to open an existing file but it can't find it. Are you sure this is what you want to do, opening a file and then editing it and saving it? –  Jul 31 '14 at 12:00
  • i want to create an xlsx file and then write data into it – krishnadhar Aug 05 '14 at 05:42

1 Answers1

0

You are trying to read a file that does not exist at the row

FileInputStream fis = new FileInputStream(new File("test.xlsx"));

that is exactly what the error message says: "(The system cannot find the file specified)"

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • Yes Albin. I got that.but my issue here is. what should be done to get that right! – krishnadhar Jul 31 '14 at 11:30
  • Well, make sure the file exists first, http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-java-on-windows and/or embed the code in a try-catch block. – Albin Sunnanbo Jul 31 '14 at 11:35