Here is the example of downloading xls and pdf in liferay6
String cmd = resourceRequest.getParameter(Constants.CMD);
PortletSession session= resourceRequest.getPortletSession();
List<Subcription> subscription = (List<Subcription>)session.getAttribute("resultscustomer");
if(subscription!=null)
{
try
{
resourceResponse.setContentType("application/vnd.ms-excel");
resourceResponse.addProperty(
HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=Marketing.xls"
);
Workbook wb = new HSSFWorkbook();
OutputStream fileOut = resourceResponse.getPortletOutputStream();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("MTS Requirement");
// Sheet Subscriber = wb.createSheet("Subscriber");
//Sheet Content = wb.createSheet("Content");
//Sheet KPI = wb.createSheet("KPI");
CellStyle style;
Font headerFont = wb.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(headerFont);
Row row = sheet.createRow((short)0);
// Row subscriberrow = Subscriber.createRow((short)0);
// Create a cell and put a value in it.
// Or do it on one line.
row.createCell(0).setCellValue("MDN");
row.createCell(1).setCellValue("Subscription Type");
row.createCell(2).setCellValue("Product & Pack");
row.createCell(3).setCellValue("Subscription Status");
row.createCell(4).setCellValue("Customer Name");
row.createCell(5).setCellValue("Subscription Date");
row.createCell(6).setCellValue("Renewal Due Date");
int i=1;
for(Subcription sub:subscription)
{
Row datarow = sheet.createRow(i);
datarow.createCell(0).setCellValue(sub.getMdn());
datarow.createCell(1).setCellValue(sub.getType());
String productpack=
datarow.createCell(2).setCellValue(productpack);
String status="";
if(sub.getActive())
{
status="Active";
}
else
{
status="Inactive";
}
datarow.createCell(3).setCellValue(status);
datarow.createCell(4).setCellValue(com.liferay.portal.service.UserLocalServiceUtil.getUser(sub.getUserId()).getFullName() );
datarow.createCell(5).setCellValue(sdf.format(sub.getCrdDate()));
datarow.createCell(6).setCellValue(sdf.format(sub.getNextCrdDate()));
i++;
}
/* subscriberrow.createCell(0).setCellValue("Date");
subscriberrow.createCell(1).setCellValue("MDN");
subscriberrow.createCell(2).setCellValue("Subscriber Profile");
subscriberrow.createCell(3).setCellValue("Download History");
subscriberrow.createCell(4).setCellValue("Plan");*/
wb.write(fileOut);
// wb.write(fileOut);
fileOut.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
For Pdf using Itext
PortletSession session= resourceRequest.getPortletSession();
List<Subcription> subscription = (List<Subcription>)session.getAttribute("resultscustomer");
try
{
PdfPTable table = new PdfPTable(7);
// Code 1
table.setWidths(new int[]{2,4,4,4,4,4,4});
// Code 2
table.addCell("MDN");
table.addCell("Subscription Type");
// Code 3
table.addCell("Product & Pack");
table.addCell("Subscription Status");
// Code 4
table.addCell("Customer Name");
table.addCell("Subscription Date");
table.addCell("Renewal Due Date");
for(Subcription sub:subscription)
{
String productpack=
String status="";
if(sub.getActive())
{
status="Active";
}
else
{
status="Inactive";
}
table.addCell(sub.getMdn());
table.addCell(sub.getType());
// Code 3
table.addCell(productpack);
table.addCell(status);
// Code 4
table.addCell(com.liferay.portal.service.UserLocalServiceUtil.getUser(sub.getUserId()).getFullName());
table.addCell(sdf.format(sub.getCrdDate()));
table.addCell(sdf.format(sub.getNextCrdDate()));
}
// Code 5
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
document.add(table);
document.close();
resourceResponse.setContentType("application/pdf");
resourceResponse.addProperty(
HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=Marketingreport.pdf"
);
//resourceResponse.setContentLength(baos.size());
OutputStream out = resourceResponse.getPortletOutputStream();
baos.writeTo(out);
out.flush();
out.close();
}catch(Exception e)
{
e.printStackTrace();
}
In your Jsp
<a class="view-in-excel" title="View in Excel" href="<portlet:resourceURL><portlet:param name="<%= Constants.CMD %>" value="export_csd" /></portlet:resourceURL>"><span
class="link-to-download">Excel<%-- <img src="<%=themeDisplay.getPathThemeImages().toString()%>"/> --%></span></a>
<a class="view-in-excel" title="View in Excel" href="<portlet:resourceURL><portlet:param name="<%= Constants.CMD %>" value="export_csd_pdf" /></portlet:resourceURL>"><span
class="link-to-download">PDF<%-- <img src="<%=themeDisplay.getPathThemeImages().toString()%>/> --%></span></a>
</div>
String cmd = resourceRequest.getParameter(Constants.CMD);
PortletSession session= resourceRequest.getPortletSession();
List<Subcription> subscription = (List<Subcription>)session.getAttribute("resultscustomer");
if(subscription!=null)
{
try
{
resourceResponse.setContentType("application/vnd.ms-excel");
resourceResponse.addProperty(
HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=Marketing.xls"
);
Workbook wb = new HSSFWorkbook();
OutputStream fileOut = resourceResponse.getPortletOutputStream();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("MTS Requirement");
// Sheet Subscriber = wb.createSheet("Subscriber");
//Sheet Content = wb.createSheet("Content");
//Sheet KPI = wb.createSheet("KPI");
CellStyle style;
Font headerFont = wb.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(headerFont);
Row row = sheet.createRow((short)0);
// Row subscriberrow = Subscriber.createRow((short)0);
// Create a cell and put a value in it.
// Or do it on one line.
row.createCell(0).setCellValue("MDN");
row.createCell(1).setCellValue("Subscription Type");
row.createCell(2).setCellValue("Product & Pack");
row.createCell(3).setCellValue("Subscription Status");
row.createCell(4).setCellValue("Customer Name");
row.createCell(5).setCellValue("Subscription Date");
row.createCell(6).setCellValue("Renewal Due Date");
int i=1;
for(Subcription sub:subscription)
{
Row datarow = sheet.createRow(i);
datarow.createCell(0).setCellValue(sub.getMdn());
datarow.createCell(1).setCellValue(sub.getType());
String productpack=
datarow.createCell(2).setCellValue(productpack);
String status="";
if(sub.getActive())
{
status="Active";
}
else
{
status="Inactive";
}
datarow.createCell(3).setCellValue(status);
datarow.createCell(4).setCellValue(com.liferay.portal.service.UserLocalServiceUtil.getUser(sub.getUserId()).getFullName() );
datarow.createCell(5).setCellValue(sdf.format(sub.getCrdDate()));
datarow.createCell(6).setCellValue(sdf.format(sub.getNextCrdDate()));
i++;
}
/* subscriberrow.createCell(0).setCellValue("Date");
subscriberrow.createCell(1).setCellValue("MDN");
subscriberrow.createCell(2).setCellValue("Subscriber Profile");
subscriberrow.createCell(3).setCellValue("Download History");
subscriberrow.createCell(4).setCellValue("Plan");*/
wb.write(fileOut);
// wb.write(fileOut);
fileOut.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
For Pdf using Itext
PortletSession session= resourceRequest.getPortletSession();
List<Subcription> subscription = (List<Subcription>)session.getAttribute("resultscustomer");
try
{
PdfPTable table = new PdfPTable(7);
// Code 1
table.setWidths(new int[]{2,4,4,4,4,4,4});
// Code 2
table.addCell("MDN");
table.addCell("Subscription Type");
// Code 3
table.addCell("Product & Pack");
table.addCell("Subscription Status");
// Code 4
table.addCell("Customer Name");
table.addCell("Subscription Date");
table.addCell("Renewal Due Date");
for(Subcription sub:subscription)
{
String productpack=
String status="";
if(sub.getActive())
{
status="Active";
}
else
{
status="Inactive";
}
table.addCell(sub.getMdn());
table.addCell(sub.getType());
// Code 3
table.addCell(productpack);
table.addCell(status);
// Code 4
table.addCell(com.liferay.portal.service.UserLocalServiceUtil.getUser(sub.getUserId()).getFullName());
table.addCell(sdf.format(sub.getCrdDate()));
table.addCell(sdf.format(sub.getNextCrdDate()));
}
// Code 5
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
document.add(table);
document.close();
resourceResponse.setContentType("application/pdf");
resourceResponse.addProperty(
HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=Marketingreport.pdf"
);
//resourceResponse.setContentLength(baos.size());
OutputStream out = resourceResponse.getPortletOutputStream();
baos.writeTo(out);
out.flush();
out.close();
}catch(Exception e)
{
e.printStackTrace();
}
In your Jsp
<a class="view-in-excel" title="View in Excel" href="<portlet:resourceURL><portlet:param name="<%= Constants.CMD %>" value="export_csd" /></portlet:resourceURL>"><span
class="link-to-download">Excel<%-- <img src="<%=themeDisplay.getPathThemeImages().toString()%>"/> --%></span></a>
<a class="view-in-excel" title="View in Excel" href="<portlet:resourceURL><portlet:param name="<%= Constants.CMD %>" value="export_csd_pdf" /></portlet:resourceURL>"><span
class="link-to-download">PDF<%-- <img src="<%=themeDisplay.getPathThemeImages().toString()%>/> --%></span></a>
</div>
No comments:
Post a Comment