Generating Excel Reports using Apache POI in cuba

Hi,
I want to generate excel reports using apache poi in cuba. I want the file to be generated at a location i specify in the code and show the file getting downloaded in the browser. Below is my sample code. The file is not getting created. Could you please help?

serviceBean.java
 public void ExportExcel(){ 
	final String fileName = "Test.xlsx";
 	XSSFWorkbook workbook = new XSSFWorkbook();
 	XSSFSheet sheet = workbook.createSheet("Test Data");
	.....
	// Generate the header and content for excel
	....
 	try (FileOutputStream outputStream = new FileOutputStream(new File(fileName))) {
                workbook.write(outputStream);
                outputStream.flush();

            }catch(Exception e){
                e.printStackTrace();
            }

}

public class ExcelImport extends Screen {
  @Subscribe("btnExportExcel")
    protected void onButtonExportExcel(Button.ClickEvent event) {
	service.ExportExcel();
    }	
}

Hi Renuka,

this worked for me:

  • The service returns PDF file as a byte array

  • In the screen, useexportDisplay.show() to view the generated PDF.

Hope this helps!

Andrea

1 Like

Hi Alunetti,

My file name is Test.xlsx and I have specified the file storage location in app.properties as shown below.

cuba.fileStorageDir = /export/file

But it is not creating the file in the path specified in app.properties.
Could you please tell me whether we need to manually create the directories or will cuba create the directories?
Even when i tried creating the directories, the file is not getting created in the path. It is actually getting created at deploy/tomcat/bin folder. What could be the reason for this?

This is because you create it directly using FileOutputStream. CUBA mechanisms are not involved here.