Search code examples
javaexcelapache-poitestnghssf

Write data to excel using Apache POI with TestNG framework


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

Solution

  • 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());