0

I have designed an Java application that reads the data from the database and writes in excel file. I have used Apache-POI to create the excel file and Desktop class to auto open that excel file as soon as it is created. When I run the program in my local machine (Windows 7, Eclipse, Apache-tomcat ) the application successfully creates the excel file and opens it automatically.

Then I created a WAR file of the application and deployed it to the remote server (Window server 2012) in Apache-tomcat. But this time when I run application the excel file is created in the desired location but it doesn't open automatically???

My Java method for creating excel file and to auto open it looks like this:

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

Could someone kindly help me to figure out the solution of this problem?

sä-röze
  • 13
  • 1
  • 7
  • 3
    Is excel installed on the server? xD – Wulf Apr 30 '19 at 08:05
  • Hello @Wulf ! No windows office is not installed in windows-server-2012. Is it necessary because I am accessing the application now from my laptop (i.e. http://window-server-2012 adress:portNumber/MyApp) and my laptop has excel. – sä-röze Apr 30 '19 at 08:11
  • 2
    Sure. The Server needs excel, because the jvm is running on the server and it will open it on the serve. You wont see it in your browser accessing the webapp.... – Wulf Apr 30 '19 at 08:15
  • @Wulf Thank you very much for the info. So in my case, what is could be the solution if I want to have my application in remote server and using that I want to open excel file in my laptop? – sä-röze Apr 30 '19 at 08:23
  • 2
    If your data is on the server and you want him to generate xls (etc)files you have to build a download function and open the files on your own on your laptop. – Wulf Apr 30 '19 at 08:27
  • @Wulf yes the excel file is created in server (in this location: D:/projects/workspace/tomcat/webapps/reports/excel/costReport.xls). I wanted this file to open in my machine. Do you have any code samples how can I build a download function and open the files on my own laptop? – sä-röze Apr 30 '19 at 08:32
  • You could try to build your webapp with vaadin and use their spreedsheet addon the display your xls in the browser of your machine. https://vaadin.com/directory/component/vaadin-spreadsheet https://vaadin.com/flow – Wulf Apr 30 '19 at 08:49
  • @Wulf. Thank you for the link and information but my problem is - I cannot create whole new application. Actually I added that excel creating and opening feature in existing application and I don't have authority to use other frameworks at this moment. – sä-röze Apr 30 '19 at 08:58
  • 1
    You will have to get the user to download the file, and the browser will then prompt them if they want to open in in Excel (or whatever they have installed that handles these files). You cannot get Excel to open on the client machine without user interaction. – Thilo Apr 30 '19 at 09:00
  • @sä-röze Thilo is right. Which tech you are using? jsp/jsf? – Wulf Apr 30 '19 at 09:11
  • @Thilo Thank you for your precious suggestion. Do you have any suggestion where I can find more information on how to implement it technically? Like links or sample codes etc...? – sä-röze Apr 30 '19 at 09:18
  • @Wulf I am using Java servlet and JSP. I created a java servlet to handle this feature. – sä-röze Apr 30 '19 at 09:19
  • Easiest would be to write the Excel to a temporary file on disk (on the server) and then send this file to the browser, as in https://stackoverflow.com/a/31271103/14955 – Thilo Apr 30 '19 at 10:34

0 Answers0