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 .
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:
\r\n
(CR+LF) line ending. Maybe the server is Linux and delivers\n
(LF).Then
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: