使用Apache POI和TestNG框架将数据写入excel

我正在尝试使用Apache POI将数据写入excel表单。我正在使用TestNG framerwork和eclipse IDE。 程序正在成功执行,没有任何错误。但是当我点击我的项目源的刷新,excel工作表不会来。 请告诉我,我将如何看到我生成的Excel表单。 我的代码如下:

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

请检查您的项目的根目录。 它应该在那里默认。 如果您在运行configuration – >参数中指定了工作目录,则应该检查该文件夹。 另外,你总是可以在Java中获得完整的文件path。

 System.out.println(new File("User.xls").getAbsolutePath()); 

试试这种方式来创build文件

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

这个文件将被存储在你的类目录文件夹中。

例如:( Test.java存储在( c://Workspace//src//Test.java) ,该文件也将存储在相同的pathc://Workspace//src//User.xls"