Liferay có một portlet "Reports" , trong đó là một ứng dụng báo cáo đầy đủ và chính thức.
Đôi khi một khách hàng có thể yêu cầu một tập tin PDF cho mục đích đơn giản là làm báo cáo. Đây có thể là một portlet nhỏ, sử dụng một URL nguồn tài nguyên. URL nguồn tài nguyên như quy định trong JSR 286.
Thêm code bên dưới vào trang JSP của ban
<input type="button" value="Submit" onClick="location.href = '<portlet:resourceURL><portlet:param name="reportType" value="pdf" /></portlet:resourceURL>'" />
Và đây là code sử lí
public void serveResource(ResourceRequest req, ResourceResponse res)
throws PortletException, IOException {
String rType = ParamUtil.getString(req, "reportType");
HttpServletRequest request = PortalUtil.getHttpServletRequest(req);
HttpSession session = request.getSession();
ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
WebKeys.THEME_DISPLAY);
Company company = themeDisplay.getCompany();
if(rType != null && rType.equals("pdf")){
try {
String msg = "Latest Weather Report";
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph(msg));
document.add(Chunk.NEWLINE);
document.add(new Paragraph("It is a sunny day today."));
document.close();
res.setContentType("application/pdf");
res.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
res.setContentLength(baos.size());
OutputStream out = res.getPortletOutputStream();
baos.writeTo(out);
out.flush();
out.close();
} catch (Exception e2) {
System.out.println("Error in " + getClass().getName() + "\n" + e2);
}
}
else{
try {
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow((short)0);
Cell cell = row.createCell(0);
cell.setCellValue(1);
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(
createHelper.createRichTextString("This is a string"));
row.createCell(3).setCellValue(true);
res.setContentType("application/vnd.ms-excel");
res.addProperty(
HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
OutputStream out = res.getPortletOutputStream();
wb.write(out);
out.flush();
out.close();
} catch (Exception e) {
System.out.println("Exception occurred ...");
}
}
}
No comments:
Post a Comment