0

I'm new to GWT and in my company i was asked to make some changes in one of our applications. Currently the Program generates reports in .xls format. Now we want to change it to .xlsx

public class Download extends HttpServlet {

    private static final long serialVersionUID = 5580666921970339383L;

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String filename = (String)request.getSession().getAttribute(CrossReportConstants.ATTR_FILENAME);
        byte[] data = (byte[])request.getSession().getAttribute(CrossReportConstants.ATTR_REPORT);
        request.getSession().setAttribute(CrossReportConstants.ATTR_FILENAME, null);
        request.getSession().setAttribute(CrossReportConstants.ATTR_REPORT, null);

        response.setContentType("application/vnd.ms-excel");

        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        response.setContentLength(data.length);

        try {
            InputStream in = new ByteArrayInputStream(data);
            ServletOutputStream out = response.getOutputStream();

            byte[] outputByte = new byte[4096];
            // copy binary contect to output stream
            while (in.read(outputByte, 0, 4096) != -1) {
                out.write(outputByte, 0, 4096);
            }
            in.close();
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

I changed response.setContentType("application/vnd.ms-excel"); to

 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 

and I changed the filename to exampleReport.xlsx When i test the Application i can download the report but i cannot open it - Excel tells me the file is corrupted. (the file has the right size and when i open it in a text editor i can see the content) Did I miss something? Thanks in advance

DimaSan
  • 12,264
  • 11
  • 65
  • 75
KilledByCheese
  • 852
  • 10
  • 30
  • There are some answers to this topic : http://stackoverflow.com/questions/14448139/java-servlet-trying-to-output-a-xlsx-file-but-keep-getting-octet-stream and http://stackoverflow.com/questions/32826621/exporting-xlsx-files-from-an-httpservletresponse-in-jsf-corruption-errors and http://stackoverflow.com/questions/25782961/excel-xlsx-file-not-opening-after-downloading-the-file-from-the-server-in-java – c0der Oct 24 '16 at 08:12
  • "when i open it in a text editor i can see the content": Then this cannot be an `Excel` file. An `Excel` file is either a binary file (`*.xls`) or an `ZIP` archive containing files (`*.xlsx`). Both cannot be viewed in a text editor. – Axel Richter Oct 24 '16 at 10:49
  • @Axel Richter my mistake i didn't said it clear - i mean: when I drag'n'dop the report for example in the Windows Editor i get cryptic Symbols, but when i Scroll down i can see text > the content which is in the Excel cells – KilledByCheese Oct 24 '16 at 11:14
  • At first you must be sure what your `byte[] data` will contain. If it is the bytes of an binary `Excel` file in BIFF format, then the name must be *.xls. If it is the bytes of an `ZIP`ed `Excel` file in Office OpenXML format, then the name must be *.xlsx. – Axel Richter Oct 24 '16 at 11:52
  • Changing the MIME type does not change the data itself. The data you download is still an Excel xls file. Excel cannot read the file because you're giving it an Excel file while it expects an XLSX file! You need to change the module that generates the Excel code to output XLSX instead. This depends on what you currently use to generate the Excel file. – futureelite7 Oct 25 '16 at 03:14

0 Answers0