0

I am trying to write data to excel sheet using Apache POI.I am using TestNG framerwork and eclipse IDE. Program is executing successfully without any error.But when I click refresh on my project source, excel sheet is not coming. Please tell me how will I see my generated excel sheet. My code is as below:

public class Test {
    public static void main(String args[]) {
        try {
            FileOutputStream fos = new FileOutputStream("User.xls");
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet worksheet = workbook.createSheet("worksheet");
            HSSFRow row1 = worksheet.createRow((short) 0);
            HSSFCell cell1 = row1.createCell((short) 0);
            cell1.setCellValue("abc");
            HSSFCell cell2 = row1.createCell((short) 1);
            cell2.setCellValue("123");
            workbook.write(fos);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
4
  • What path you have given to generate excel sheet? Post some of your code for better understanding. Commented Mar 16, 2013 at 7:59
  • public class Test{ public static void main(String args[]){ try{FileOutputStream fos=new FileOutputStream("User.xls"); HSSFWorkbook workbook=new HSSFWorkbook(); HSSFSheet worksheet=workbook.createSheet("worksheet"); HSSFRow row1=worksheet.createRow((short)0); HSSFCell cell1=row1.createCell((short)0); cell1.setCellValue("abc"); HSSFCell cell2=row1.createCell((short)1); cell2.setCellValue("123"); workbook.write(fos); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace();}}} Commented Mar 16, 2013 at 8:08
  • Please help where i did wrong??? Commented Mar 16, 2013 at 8:17
  • Can you find it in project's root directory? Commented Mar 16, 2013 at 9:17

2 Answers 2

1

Please check your project's root directory. It should be there by default. If you have specified the working directory in your run configuration -> arguments, you should check that folder. Besides, you can always get the complete file path in Java.

System.out.println(new File("User.xls").getAbsolutePath());
Sign up to request clarification or add additional context in comments.

Comments

1

try this way to create file

FileOutputStream out =
            new FileOutputStream(new File("User.xls"));

This file will be stored in your class directory folder.

Ex: (Test.java is stored in (c://Workspace//src//Test.java) , the file also will store in same path c://Workspace//src//User.xls")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.