-1

I'm trying to create a document (excel/word) and then send it to browser for downloading.

The file is created within eclipse directory but I'm not getting a dialog to save/downlaod the file. Instead I have characters like " ������Root Entry���������Workbook������������������ ��A����\php B�a=���=h:�#8X@�"��1���Arial1���Arial1���Arial1���Arial"$"#,##0_);("$"#,##0)"$"#,##0_);[Red]0);(* "-");(@)5)0_("$"* #,##0_);("� ������������������`�bSheet0��� Some text� D � ��X d����MbP?*+��%������"d,,�?�?U�� �">�@ ��"

Here is my code for (excel):

public void exportResults_EXCEL(ActionRequest actionRequest, ActionResponse actionResponse)throws IOException,PortletException{


        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("Some text");

        // write it as an excel attachment
        ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
        wb.write(outByteStream);

        ByteArrayInputStream excelStream = new ByteArrayInputStream(outByteStream.toByteArray());
        byte [] outArray = outByteStream.toByteArray();

        HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
        HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=something.xls");
        response.setContentLength(outArray.length);

ServletResponseUtil.sendFile(request, response, "something.xls" ,outArray, ContentTypes.APPLICATION_VND_MS_EXCEL);

        FileOutputStream output = new FileOutputStream(new File("something.xls"));
        ServletOutputStream out = response.getOutputStream();

        wb.write(output);
        output.flush();
        output.close();
}

P.S ! I'm using eclipse, liferay, poi

Ghita
  • 169
  • 1
  • 1
  • 14
  • See http://stackoverflow.com/questions/3592058/how-to-send-byte-as-pdf-to-browser-in-java-web-application AND http://stackoverflow.com/questions/7412337/how-to-write-a-file-object-on-server-response-and-without-saving-file-on-server. I hope you may get some idea now – The Guest Mar 10 '16 at 21:16
  • Thank you for your answer, it was an error from liferay because I didn't specify the windowstate. – Ghita Mar 11 '16 at 08:07

1 Answers1

2

For anyone struggling with this, It was related to liferay because I didn't specify windows state in the actionURL as I was using a popup.

   <portlet:actionURL var="exportURL" name="exportEXCEL" windowState="<%= LiferayWindowState.POP_UP.toString() %>">

And in java for sending file to the browser to download :

    ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
    wb.write(outByteStream);

    byte [] outArray = outByteStream.toByteArray();
    HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
   HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);

    ServletResponseUtil.sendFile(request, response, "result.xls" ,outArray, ContentTypes.APPLICATION_VND_MS_EXCEL);
Ghita
  • 169
  • 1
  • 1
  • 14