I have a function that creates an excel file, however the function that the excel generates saves on disk. What I want to know is how to get the excel that I generate can be downloaded by pressing a button from a form of primefaces.
xhtml:
<h:form>
<p:commandButton action="#{controller.descargar}" value="descargar"/>
</h:form>
Bean:
@Named(value = "controller")
@SessionScoped
public class ClientsCtrl {
public void descargar(){
GeneraExcel gExcel = new GeneraExcel();
gExcel.crearExcel();
}
}
GeneratesExcel:
public class GeneraExcel {
public void crearExcel() {
try {
String filename = "C:/NewExcelFile.xls";
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell(0).setCellValue("No.");
rowhead.createCell(1).setCellValue("Name");
rowhead.createCell(2).setCellValue("Address");
rowhead.createCell(3).setCellValue("Email");
HSSFRow row = sheet.createRow((short) 1);
row.createCell(0).setCellValue("1");
row.createCell(1).setCellValue("Sankumarsingh");
row.createCell(2).setCellValue("India");
row.createCell(3).setCellValue("lalalla.com");
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");
} catch (Exception ex) {
System.out.println(ex);
}
}
}