使用带有TestNG框架的Apache POI将数据写入excel

时间:2013-03-16 07:56:58

标签: java excel apache-poi testng hssf

我正在尝试使用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();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

请检查项目的根目录。它应该默认存在。如果您在运行配置中指定了工作目录 - >参数,你应该检查该文件夹。此外,您始终可以使用Java获取完整的文件路径。

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

答案 1 :(得分:1)

尝试这种方式来创建文件

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

此文件将存储在您的类目录文件夹中。

Ex :( Test.java存储在(c://Workspace//src//Test.java)中,该文件也会存储在同一路径中c://Workspace//src//User.xls"