this is the code for download excel file on browser but when i open it excel throw a error "Excel can not open the file 'Location.xlsx' because the file format or file extension is not valid. verify that the file has not been corrupted and that the file extension matches the format of the file" but when i simply create this file its opening only this error is coming when i perform download operation and i am using poi jar for excel file creation...???
<html>
<body>
<%
LocationDownload.downloadLocation();
%>
<%
String filename = "Location.xlsx";
String filepath = "C:\\Users\\dsingh\\GlassFish_Server\\glassfish\\domains\\domain1\\config\\";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename=" + filename);
java.io.FileInputStream fileInputStream=new java.io.FileInputStream(filepath + filename);
int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
%>
</body>
</html>
First move your File Download coding to Servlet (don't do inside JSP file)..
Check this code it will help.. It is working with me for the same purpose as u require.