Advertisement
Guest User

Gen

a guest
Jan 17th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. public class ExcelCreator {
  2.  
  3. private static String[] columns = { "Nom du produit", "Prix de produit", "Quantite",
  4. "Prix de la commande" };
  5.  
  6. public static FileOutputStream ExcelGen(List<Produit_Panier> pp) throws IOException {
  7.  
  8. Workbook workbook = new XSSFWorkbook();
  9. Sheet sheet = workbook.createSheet("Report");
  10.  
  11. Font headerFont = workbook.createFont();
  12. headerFont.setBold(true);
  13. headerFont.setFontHeightInPoints((short) 14);
  14. headerFont.setColor(IndexedColors.RED.getIndex());
  15.  
  16. CellStyle headerCellStyle = workbook.createCellStyle();
  17. headerCellStyle.setFont(headerFont);
  18.  
  19. // Create a Row
  20. Row headerRow = sheet.createRow(0);
  21.  
  22. for (int i = 0; i < columns.length; i++) {
  23. Cell cell = headerRow.createCell(i);
  24. cell.setCellValue(columns[i]);
  25. cell.setCellStyle(headerCellStyle);
  26. }
  27.  
  28. // Create Other rows and cells with contacts data
  29. int rowNum = 1;
  30.  
  31. for (Produit_Panier p : pp) {
  32. Row row = sheet.createRow(rowNum++);
  33. row.createCell(0).setCellValue(p.getProduit().getNomProduit());
  34. row.createCell(1).setCellValue(p.getProduit().getPrix());
  35. row.createCell(2).setCellValue(p.getQte());
  36. row.createCell(3).setCellValue(p.getQte()*p.getProduit().getPrix());
  37. }
  38.  
  39. // Resize all columns to fit the content size
  40. for (int i = 0; i < columns.length; i++) {
  41. sheet.autoSizeColumn(i);
  42. }
  43.  
  44. // Write the output to a file
  45. FileOutputStream fileOut = new FileOutputStream("ReportPanier.xlsx");
  46. workbook.write(fileOut);
  47. fileOut.close();
  48.  
  49. return fileOut;
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement