Foren

CSV File Download in Liferay 4.3.5

Venkat Utla, geändert vor 11 Jahren.

CSV File Download in Liferay 4.3.5

New Member Beiträge: 3 Beitrittsdatum: 28.08.12 Neueste Beiträge
Hi,

We have a requirement that we need to export data into CSV file and allow the client to download the CSV file.
We have used the following code.This was writing the content of CSV file in portlet. We want it to display the a dialog box which asks the user to save or download.

Could you please let us the solution for this?

Thanks in advance.

--Venkat.

public ActionForward render(ActionMapping mapping, ActionForm form,
PortletConfig config, RenderRequest req, RenderResponse res)
throws Exception {
log.info("SubmitSearchForReportsAction, render, starts");
req.setAttribute("csvFileData", "a,b,c");
try {
HttpServletResponse response = PortalUtil.getHttpServletResponse(res);

response.setContentType("text/x-csv");
response.setHeader("Content-Disposition","attachment;filename=\"promotionreport.csv\"");
PrintWriter p =res.getWriter ();
StringTokenizer rt = new StringTokenizer("a,b,c;d,e,f",";");
String rowToken = null;
while (rt.hasMoreTokens()) {
rowToken = rt.nextToken();
p.println(rowToken);
}
p.flush();
} catch (Exception e) {
SessionErrors.add(req, e.getClass().getName());
setForward(req, ActionConstants.COMMON_NULL);
}
log.info("SubmitSearchForReportsAction, render, ends");
return mapping.findForward(Constants.ACTION_SUCCESS);
}