1

Here is my sample code. I am using eclipse , tomcat server .Browser as IE9.

protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

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


        ServletContext context = request.getServletContext();
        @SuppressWarnings("unchecked")
        List<Student> students = (List<Student>) context.getAttribute("students");
        PrintWriter out = response.getWriter();
        for(Student student:students){
            out.println(student.getId()+"\t"+student.getName());
        }
        out.close();

    }

I am getting the List of Student. But when i am opening the downloaded file file getting error saying that file format or extention is not valid. My downloaded file is .xlsx .

Jens
  • 60,806
  • 15
  • 81
  • 95
navin kumar
  • 171
  • 2
  • 5
  • 15

2 Answers2

0

I strongly recommend you to use HSSFWorkbook class to create your excel file. After its created (for creation process see: this example) you can write its contents to response like this:

Workbook workbook = new XSSFWorkbook();

// Add sheet(s), colums, cells and its contents to your workbook here ...

// First set response headers
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=YourFilename.xlsx");

// Get response outputStream
ServletOutputStream outputStream = response.getOutputStream();

// Write workbook data to outputstream
workbook.write(outputStream);
Glogo
  • 2,398
  • 2
  • 18
  • 22
  • in this way i will be creating a .xls file not .xlsx file. Here i want to create a .xlsx file – navin kumar Sep 11 '14 at 10:20
  • Sorry I misread that requirement. Then you simply use `XSSFWorkbook` class instead and change `YourFilename extension to xlsx. Both `HSSFWorkbook` and `XSSFWorkbook` are implementing Wokbook interface, so its api will be same. See http://poi.apache.org/spreadsheet/quick-guide.html Updating answer – Glogo Sep 11 '14 at 12:20
0

It is not so much an .xlsx file, more a CSV or tab separated value text file. It fakes to be an Excel file; and yes, then Excel opens it correctly,

Try to read it with NotePad. You also can make a .xlsx file with NotePad to check whether the trick works.

The following tries:

  • .xls
  • A Windows \r\n (CR+LF) line ending. Maybe the server is Linux and delivers \n (LF).
  • A defined encoding.

Then

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

    ServletContext context = request.getServletContext();
    @SuppressWarnings("unchecked")
    List<Student> students = (List<Student>) context.getAttribute("students");
    PrintWriter out = response.getWriter();
    out.print("\uFEFF"); // UTF-8 BOM, redundant and ugly
    for(Student student:students){
        out.printf("%s\t%s\r\n", student.getId(), student.getName());
    }
    //out.close();
Joop Eggen
  • 96,344
  • 7
  • 73
  • 121
  • i have kept the response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); unchanged. – navin kumar Sep 11 '14 at 11:18
  • when i am opening the same downloaded(.xlsx) file in notepad++ , able to view the content. – navin kumar Sep 11 '14 at 11:19
  • `response.setHeader("Content-Disposition", "filename=\"test.xsl\"";` or so? Try the (downloaded, edited) text file as .xsl and .xslx by double-clicking it. – Joop Eggen Sep 11 '14 at 13:13