Java Apache POI 打开文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20184041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:49:06  来源:igfitidea点击:

Java Apache POI Opening file?

javaexcelfile-ioapache-poi

提问by Izzo

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?

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

采纳答案by Digital Alchemist

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

由于您尚未提供代码,我正在使用ViralPatel 教程示例

Lets Create Excel File First

让我们先创建 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();
    }

Output

输出

enter image description here

在此处输入图片说明

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

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

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

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

Else u can invoke this as well

否则你也可以调用它

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

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

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

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

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

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

回答by Sankumarsingh

I don't know wheter POI is providing such methods or not. However you can do this using simple java method that uses the command prompt and open the desired file using startcommand.

我不知道 POI 是否提供这样的方法。但是,您可以使用简单的 java 方法执行此操作,该方法使用命令提示符并使用start命令打开所需的文件。

public static void openExcelFile(){
    try{    
        Runtime.getRuntime().exec("cmd /c start "+FilePath);
    }catch(IOException  e){  
        e.printStackTrace();  
    }
}

NOTE: The space after startis mandatory. Don't forget it

注意:后面的空格start是强制性的。不要忘记它