Advertisement
flixbeat

DaveExportTest.java

Dec 3rd, 2019
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1. package com.axesscom.qmh.business;
  2.  
  3. import java.io.*;
  4.  
  5. import javax.faces.bean.ManagedBean;
  6. import javax.faces.context.ExternalContext;
  7. import javax.faces.context.FacesContext;
  8. import javax.faces.view.ViewScoped;
  9. import javax.servlet.http.Part;
  10.  
  11. import org.apache.poi.util.IOUtils;
  12. import org.apache.poi.xssf.usermodel.XSSFCell;
  13. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  14. import org.apache.poi.xssf.usermodel.XSSFDrawing;
  15. import org.apache.poi.xssf.usermodel.XSSFPicture;
  16. import org.apache.poi.xssf.usermodel.XSSFRow;
  17. import org.apache.poi.xssf.usermodel.XSSFSheet;
  18. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  19.  
  20. @ViewScoped
  21. @ManagedBean (name = "DaveExportTest")
  22. public class DaveExportTest {
  23.    
  24.     private String fname;
  25.     private String lname;
  26.     private int day;
  27.     private int month;
  28.     private int year;
  29.     private String gender;
  30.     private boolean hasDependents;
  31.     private Part image;
  32.    
  33.     // =========== method for dropdown value ===========
  34.     public int[] getDays()
  35.     {
  36.         int[] days = new int[31];
  37.         for(int i=1; i<=31; i++)
  38.         {
  39.             days[i-1] = i;
  40.         }
  41.         return days;
  42.     }
  43.    
  44.     public int[] getMonths()
  45.     {
  46.         int[] months = new int[12];
  47.         for(int i=1; i<=12; i++)
  48.         {
  49.             months[i-1] = i;
  50.         }
  51.         return months;
  52.     }
  53.    
  54.     public int[] getYears()
  55.     {
  56.         int[] years = new int[120];
  57.         for(int i=1; i<=120 ; i++)
  58.         {
  59.             years[i-1] = i + 1900;
  60.         }
  61.         return years;
  62.     }
  63.    
  64.     public void acceptInfo()   
  65.     {
  66.         // get uploaded file/image
  67.         String fileName = null;
  68.         InputStream input = null;
  69.        
  70.         try
  71.         {
  72.             input = image.getInputStream();
  73.             fileName = image.getSubmittedFileName();
  74.         }
  75.         catch(IOException e)
  76.         {
  77.             System.err.println(e);
  78.         }
  79.        
  80.         // debug log if all data were passed successfully
  81.         System.out.println(this.fname + " " + this.lname + " " + this.day + "/" + this.month + "/" + this.year
  82.                 + " " + this.gender + " " + this.hasDependents + " " + fileName);
  83.        
  84.         // create excel file and download
  85.         this.createExcelFile(fname, lname, day, month, year, gender, hasDependents, input);
  86.     }
  87.    
  88.     // method for creating and downloading the excel file
  89.     XSSFRow row; XSSFCell cell;
  90.     private void createExcelFile(String fname, String lname, int day, int month, int year, String gender, boolean hasDependents, InputStream input)
  91.     {
  92.         XSSFWorkbook wb = new XSSFWorkbook();
  93.         XSSFSheet sh = wb.createSheet("Page 1");
  94.        
  95.         // first name, last name
  96.         row = sh.createRow(1);
  97.         cell = row.createCell(0);
  98.         cell.setCellValue(fname);
  99.         cell = row.createCell(1);
  100.         cell.setCellValue(lname);
  101.         // birthdate
  102.         row = sh.createRow(2);
  103.         String birthdate = day + "/" + month + "/" + year;
  104.         cell = row.createCell(0);
  105.         cell.setCellValue("Birthdate:");
  106.         cell = row.createCell(1);
  107.         cell.setCellValue(birthdate);
  108.         // gender
  109.         row = sh.createRow(3);
  110.         cell = row.createCell(0);
  111.         cell.setCellValue("Gender:");
  112.         cell = row.createCell(1);
  113.         cell.setCellValue(gender);
  114.         // dependents
  115.         row = sh.createRow(4);
  116.         cell = row.createCell(0);
  117.         cell.setCellValue("Has Dependents?");
  118.         cell = row.createCell(1);
  119.         String dep = hasDependents ? "Yes" : "None";
  120.         cell.setCellValue(dep);
  121.         // place image to sheet
  122.         try
  123.         {
  124.             byte[] bytes = IOUtils.toByteArray(input); // convert image to bytes, needed for the getting the image index method
  125.             int imageIndex = wb.addPicture(bytes, wb.PICTURE_TYPE_JPEG); // get image index, needed for create picture method
  126.             input.close(); // close inputstream
  127.             XSSFDrawing drawing = sh.createDrawingPatriarch(); // create drawing container
  128.             XSSFClientAnchor anchor = new XSSFClientAnchor(); // define top left corner
  129.             anchor.setCol1(5);
  130.             anchor.setRow1(6);
  131.             anchor.setCol2(17);
  132.             anchor.setRow2(18);
  133.             XSSFPicture pic = drawing.createPicture(anchor, imageIndex); // invoke createPicture and pass the anchor point and image id
  134.             // pic.resize(); // resize the image to its original size
  135.         }
  136.         catch(IOException e)
  137.         {
  138.             System.err.print(e);
  139.         }
  140.        
  141.         // code to download file
  142.         FacesContext facesContext = FacesContext.getCurrentInstance();
  143.         ExternalContext externalContext = facesContext.getExternalContext();
  144.         externalContext.setResponseContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  145.         externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"davefileexport.xlsx\"");
  146.        
  147.         try {
  148.             wb.write(externalContext.getResponseOutputStream());
  149.             facesContext.responseComplete();
  150.             System.out.println("Workbook created");
  151.         }
  152.         catch(IOException e) {
  153.             System.err.println(e);
  154.         }
  155.        
  156.     }
  157.    
  158.     // property GETTERS
  159.     public String getFname()
  160.     {
  161.         return fname;
  162.     }
  163.    
  164.     public String getLname()
  165.     {
  166.         return lname;
  167.     }
  168.    
  169.     public int getDay()
  170.     {
  171.         return day;
  172.     }
  173.    
  174.     public int getMonth()
  175.     {
  176.         return month;
  177.     }
  178.    
  179.     public int getYear()
  180.     {
  181.         return year;
  182.     }
  183.    
  184.     public String getGender()
  185.     {
  186.         return this.gender;
  187.     }
  188.    
  189.     public boolean getHasDependents()
  190.     {
  191.         return this.hasDependents;
  192.     }
  193.    
  194.     public Part getImage()
  195.     {
  196.         return this.image;
  197.     }
  198.    
  199.     // property SETTERS
  200.     public void setFname(String fname)
  201.     {
  202.         this.fname = fname;
  203.     }
  204.    
  205.     public void setLname(String lname)
  206.     {
  207.         this.lname = lname;
  208.     }
  209.    
  210.     public void setDay(int day)
  211.     {
  212.         this.day = day;
  213.     }
  214.    
  215.     public void setMonth(int month)
  216.     {
  217.         this.month = month;
  218.     }
  219.    
  220.     public void setYear(int year)
  221.     {
  222.         this.year = year;
  223.     }
  224.    
  225.     public void setGender(String gender)
  226.     {
  227.         this.gender = gender;
  228.     }
  229.    
  230.     public void setHasDependents(boolean hasDependents)
  231.     {
  232.         this.hasDependents = hasDependents;
  233.     }
  234.    
  235.     public void setImage(Part image)
  236.     {
  237.         this.image = image;
  238.     }
  239.    
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement