Pregunta

I've a web project, where grid view is displayed from database. Arraylist name is leadSchoolList. So, I kept a button, when clicked it runs a method(named:-actionExportToExcel) in Struts action class. Right now I am able to export list elements to excel.

But the problem I'm facing is opening up that exported excel Sheet on window. So another method(named:-open) is called inside actionExportToExcel. But I don't know where I'm wrong, so can anyone help me?

public String actionExportToExcel(){
    try {
        FileOutputStream fileOut = new FileOutputStream("D:/poi-test.xls");

     HSSFWorkbook wb = new HSSFWorkbook();
     HSSFSheet sheet = wb.createSheet("new sheet");


     leadSchoolList = leadSchoolService.getAllLeadSchool();
     for(int rowNum = 0; rowNum < leadSchoolList.size(); rowNum++){
         HSSFRow row = sheet.createRow(rowNum);

         //for(int colNum = 0; colNum < 5; colNum++ ){
             HSSFCell cell1 = row.createCell(0);
             LeadSchool leadSchool = leadSchoolList.get(rowNum);
             cell1.setCellValue(leadSchool.getLeadSchool_String_LSchool_ID());

             HSSFCell cell2 = row.createCell(1);
             cell2.setCellValue(leadSchool.getLeadSchool_String_Name());

             HSSFCell cell3 = row.createCell(2);
             cell3.setCellValue(leadSchool.getLeadSchool_String_Address());

             HSSFCell cell4 = row.createCell(3);
             cell4.setCellValue(leadSchool.getLeadSchool_String_Phone_No());

             HSSFCell cell5 = row.createCell(4);
             cell5.setCellValue(leadSchool.getLeadSchool_String_Remarks());
             System.out.println("Successfully exported to Excel");






     }
     try {
        wb.write(fileOut);

    // fileOut.flush();
     open(fileOut);
     //fileOut.close();
     } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        addActionError("The process cannot access the file because it is being used by another process");
        e.printStackTrace();
    }

    return SUCCESS;
}

  private void open(FileOutputStream f) {

      String[] cmd = new String[4];
        try{
            cmd[0] = "cmd.exe";
            cmd[1] = "/C";
            cmd[2] = "start";
            cmd[3] = "D:/poi-test.xls";

            Runtime.getRuntime().exec(cmd);
            //Runtime.getRuntime().exec("cmd.exe /c start D:/poi-test.xls");
            System.out.print("file opened");
        }
        catch(Exception e){
            e.printStackTrace();
        }
 }    
¿Fue útil?

Solución

Fortunately found the solution myself. .First need to run flush() and close() method and then the second(named:-open) method. Inside open method, instead of R

Runtime.getRuntime().exec(cmd);

i expanded it into two lines:-

Runtime run = Runtime.getRuntime();
Run.exec(cmd); 

Thats it..:D

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top