简体   繁体   中英

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

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

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" )

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