If you are generating the excel in a browser just call the method you want to generate the excel file based on a url and set the response properties like this,
//1.Fill the data from db
//2.Set the response properties
String fileName = "Excel.xls";
response.setHeader("Content-Disposition", "inline; filename=" + fileName);
// Make sure to set the correct content type(the below content type is ok)
response.setContentType("application/vnd.ms-excel");
//3.Write to the output stream
Writer.write();//call write method of Writer class to write the data to o/p stream
Writer Class:
public class Writer {
private static Logger logger = Logger.getLogger("service");
/**
* Writes the report to the output stream
*/
public static void write(HttpServletResponse response, HSSFSheet worksheet) {
logger.debug("Writing excel data to the stream");
try {
// Retrieve the output stream
ServletOutputStream outputStream = response.getOutputStream();
// Write to the output stream
worksheet.getWorkbook().write(outputStream);
// Flush the stream
outputStream.flush();
}
catch (Exception e) {
logger.error("Unable to write excel data to the output stream");
}
}
}
In the response receiving end you'll be prompted to download the file in the browser window.. LINK