Java Apache POI 打开文件? [英] Java Apache POI Opening file?

查看:32
本文介绍了Java Apache POI 打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java 程序,可以编辑现有的 excel 文件并将其另存为新文件.但是,我也希望程序在结束时自动打开新创建的文件.是否有 apache poi 命令可以让我执行此操作?

I have a Java program that edits an existing excel file and saves it as a new file. However, I would also like the program to automatically open the newly created file upon ending. Is there an apache poi command that lets me do this?

推荐答案

由于您还没有提供代码,我正在使用 ViralPatel 教程

As you have not Provided Code , I m using the Example of ViralPatel Tutorial

让我们先创建 Excel 文件

HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Sample sheet");

    Map<String, Object[]> data = new HashMap<String, Object[]>();
    data.put("1", new Object[] { "Emp No.", "Name", "Salary" });
    data.put("2", new Object[] { 1d, "John", 1500000d });
    data.put("3", new Object[] { 2d, "Sam", 800000d });
    data.put("4", new Object[] { 3d, "Dean", 700000d });

    Set<String> keyset = data.keySet();
    int rownum = 0;
    for (String key : keyset) {
        Row row = sheet.createRow(rownum++);
        Object[] objArr = data.get(key);
        int cellnum = 0;
        for (Object obj : objArr) {
            Cell cell = row.createCell(cellnum++);
            if (obj instanceof Date)
                cell.setCellValue((Date) obj);
            else if (obj instanceof Boolean)
                cell.setCellValue((Boolean) obj);
            else if (obj instanceof String)
                cell.setCellValue((String) obj);
            else if (obj instanceof Double)
                cell.setCellValue((Double) obj);
        }
    }

    try {
        FileOutputStream out = new FileOutputStream(new File("D:\\new.xls"));
        workbook.write(out);
        out.close();
        System.out.println("Excel written successfully..");

        /** Opening Excel File From Java **/

        try {
            Desktop.getDesktop().open(new File("D:\\new.xls"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

输出

解决方案:在您使用正确的位置创建Excel后,将此代码与Try/Catch Block一起放置,它将自动打开

Solution : Put this code with Try/Catch Block after You have created Excel with correct Location, It will be opened Automatically

Desktop.getDesktop().open(new File("D:\new.xls"));

否则你也可以调用它

尝试使用 Desktop.open() 而不是 Desktop.edit() :

Try to use Desktop.open() instead of Desktop.edit() :

Desktop dt = Desktop.getDesktop();
dt.open(new File("D:\\new.xls"));

如果 Desktop.open() 不可用,则可以使用 Windows 文件关联:

If Desktop.open() is not available then the Windows file association can be used :

 Process p =    Runtime.getRuntime()    .exec("rundll32
 url.dll,FileProtocolHandler " + "D:\\new.xls");

这篇关于Java Apache POI 打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆