/** exports the results to a excel file */ public void exportExcel() { try { FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=\"umsaetze.xls\";"); OutputStream out = response.getOutputStream(); WorkbookSettings ws = new WorkbookSettings(); ws.setLocale(new Locale("de", "DE")); WritableWorkbook workbook = Workbook.createWorkbook(out, ws); WritableSheet sheet = workbook.createSheet("Sheet1", 0); sheet.addCell(new Label(0, 0, "Datum")); sheet.addCell(new Label(1, 0, "Umssatzinformation")); sheet.addCell(new Label(2, 0, "EURO")); DateFormat format = new SimpleDateFormat("dd.MM.yyyy"); int row = 1; if (transactions != null) for (Transaction transaction : transactions) { sheet.addCell(new Label(0, row, format.format(transaction.getDate()))); if (transaction.getFromAccount().toString().equals(acc)) { sheet.addCell( new Label( 1, row, "AN :" + transaction.getToAccount().toString() + ", " + transaction.getPaymentReason())); } else { sheet.addCell( new Label( 1, row, "VON:" + (transaction.getFromAccount().getAccountID() == BankSettings.bankAccountID ? "E-BANK-SYSTEM" : transaction.getFromAccount()) + ", " + transaction.getPaymentReason())); } sheet.addCell( new Label( 2, row, transaction.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString())); row++; } workbook.write(); workbook.close(); response.flushBuffer(); context.responseComplete(); } catch (Exception e) { e.printStackTrace(); } }
/** Exports the results to a pdf */ public void exportPdf() { try { Customer c = accountController.getCustomer(); DateFormat format = new SimpleDateFormat("dd.MM.yyyy"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfPCell cell; Document document = new Document(); PdfWriter.getInstance(document, baos); Font defaultFont = new Font(Font.FontFamily.HELVETICA, 10); Font headerFont = new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD); document.open(); document.add( new Paragraph( "Umsatzanzeige für " + c.getFirstName() + " " + c.getFamilyName() + ", erstellt am " + format.format(new Date()) + " für Konto " + acc, defaultFont)); // document.add(new Paragraph("Zweiter Paragraph")); PdfPTable table = new PdfPTable(new float[] {1f, 4f, 1f}); table.setWidthPercentage(100f); table.setSpacingBefore(20f); cell = new PdfPCell(new Phrase("Datum", headerFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPadding(4f); cell.setGrayFill(0.9f); table.addCell(cell); cell = new PdfPCell(new Phrase("Umsatzinformation", headerFont)); cell.setPadding(4f); cell.setGrayFill(0.9f); table.addCell(cell); cell = new PdfPCell(new Phrase("Euro", headerFont)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setPadding(4f); cell.setGrayFill(0.9f); table.addCell(cell); if (transactions != null) for (Transaction transaction : transactions) { cell = new PdfPCell(new Phrase(format.format(transaction.getDate()), defaultFont)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setBorder(Rectangle.BOTTOM); cell.setPadding(4f); table.addCell(cell); if (transaction.getFromAccount().toString().equals(acc)) { cell = new PdfPCell( new Phrase( "AN : " + transaction.getToAccount() + ", " + transaction.getPaymentReason(), defaultFont)); } else { cell = new PdfPCell( new Phrase( "VON: " + (transaction.getFromAccount().getAccountID() == BankSettings.bankAccountID ? "E-BANK-SYSTEM" : transaction.getFromAccount()) + ", " + transaction.getPaymentReason(), defaultFont)); } cell.setBorder(Rectangle.BOTTOM); cell.setPadding(4f); table.addCell(cell); cell = new PdfPCell( new Phrase( transaction.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString(), defaultFont)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setBorder(Rectangle.BOTTOM); cell.setPadding(4f); table.addCell(cell); } document.add(table); document.close(); FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.setContentType("application/pdf"); response.setContentLength(baos.size()); response.setHeader("Content-disposition", "inline;filename=\"umsaetze.pdf\""); OutputStream out = response.getOutputStream(); baos.writeTo(out); out.flush(); response.flushBuffer(); context.responseComplete(); } catch (Exception e) { e.printStackTrace(); } }