0

I am displaying list of my custom objects on liferay portlet. Now my problem is I want to provide Export feature to user. There will be export link below list displayed. When user click on that button it will download the displayed list as excel file.

I am generating excel file while generating the list for display.So, now my problem is how exactly export link should behave.

My export button code.

<portlet:actionURL name="exportURL" var="exportURL"></portlet:actionURL>
 <p><a href="<%=exportURL %>">&larr; Export</a></p>

One approach I am thinking is after generating excel file I can upload it in Document library of Liferay and then provide download link as export link on portlet is it a good approch?

mitpatoliya
  • 2,037
  • 12
  • 23
  • You should dynamically generate the excel file whenever the user clicks on that link. Manually generating it and uploading - doesn't work in real life as you'll soon find yourself always uploading with changes. – adarshr Aug 02 '13 at 09:20
  • I am not doing it manually I have utility class which can generate it now the only thing is on click of this button I should be able to invoke that to which will generate the Excel file and it should also gets downloaded. – mitpatoliya Aug 02 '13 at 10:35
  • http://stackoverflow.com/questions/8654568/serve-pdf-in-spring-portlet-mvc-architecture-liferay-6-0-6 – Ashok Goli Feb 09 '15 at 22:29

3 Answers3

2

Instead of using <portlet:actionURL> you want to use <portlet:resourceURL>. This triggers the resource-phase of the portlet where you can serve other content types than just HTML snippets, e.g. Excel types.

I have the impression that you nailed the excel export itself already and just need an idea how to get to the export from the portlet UI, right? In serveResource you get a ResourceRequest and ResourceResponse object and can set the Mimetype for the response (and its OutputStream)

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
1

You should generate the file only after user clicks the link for export as adarshr has written in comment to your question. It will be useless waste of server resources if you generate the file and upload it to download center every time the list will show in the portlet.

Karel
  • 46
  • 1
0

You can use poi-2.5.1.jar for excel file generation in java.

Make use of serveResource method.

Below code snippet ,you can make use of.\

Workbook workBook = new HSSFWorkbook();
Sheet sheet = workBook.createSheet("new sheet");
Row row = sheet.createRow((short)0);
Cell cell = row.createCell(0);
//set row,cell value as per your custom entity
resourceResponse.setContentType("application/vnd.ms-excel");
OutputStream out = resourceResponse.getPortletOutputStream();
workBook.write(out);

HTH, Regards

Pankaj Kathiriya
  • 4,210
  • 2
  • 19
  • 26