为什么Desktop.getDesktop()。open(excelFile)无法自动打开Excel文件?

时间:2019-04-30 08:00:56

标签: java windows servlets apache-poi windows-server-2012

我设计了一个Java应用程序,该程序从数据库读取数据并写入excel文件。我使用Apache-POI创建excel文件,并使用Desktop类在创建后立即自动打开该excel文件。当我在本地计算机(Windows 7,Eclipse,Apache-tomcat)上运行程序时,该应用程序成功创建了excel文件并自动将其打开。

然后,我创建了该应用程序的WAR文件,并将其部署到Apache-tomcat中的远程服务器(Window服务器2012)。但是这一次,当我运行应用程序时,会在所需位置创建excel文件,但该文件不会自动打开??

我用于创建excel文件并自动打开它的Java方法如下:

public void excelReport() {

            // my Apache-POI code for creating excel file is here which is not included

            String excelFileName = costReport.xls
            String excelFilePath = D:/projects/workspace/tomcat/webapps/reports/excel/
            String fullExcelPath = excelFilePath + excelFileName;

            FileOutputStream out = new FileOutputStream(new File(fullExcelPath));

            // write operation workbook using file out object
            workbook.write(out);
            out.flush();
            out.close();

            // Close workbook
            workbook.close();

            // Open excel file as soon as excel file is created
            try {
                if (Desktop.isDesktopSupported()) {
                    Desktop.getDesktop().open(new File(fullExcelPath)); // Used this class to auto open the excel file as soon as it is created but it doesn't work

                } 
            } catch (Exception e) {

                e.printStackTrace();
            }
}

有人可以帮我找出解决这个问题的方法吗?

0 个答案:

没有答案