Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.axesscom.qmh.business;
- import java.io.*;
- import javax.faces.bean.ManagedBean;
- import javax.faces.context.ExternalContext;
- import javax.faces.context.FacesContext;
- import javax.faces.view.ViewScoped;
- import javax.servlet.http.Part;
- import org.apache.poi.util.IOUtils;
- import org.apache.poi.xssf.usermodel.XSSFCell;
- import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
- import org.apache.poi.xssf.usermodel.XSSFDrawing;
- import org.apache.poi.xssf.usermodel.XSSFPicture;
- import org.apache.poi.xssf.usermodel.XSSFRow;
- import org.apache.poi.xssf.usermodel.XSSFSheet;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- @ViewScoped
- @ManagedBean (name = "DaveExportTest")
- public class DaveExportTest {
- private String fname;
- private String lname;
- private int day;
- private int month;
- private int year;
- private String gender;
- private boolean hasDependents;
- private Part image;
- // =========== method for dropdown value ===========
- public int[] getDays()
- {
- int[] days = new int[31];
- for(int i=1; i<=31; i++)
- {
- days[i-1] = i;
- }
- return days;
- }
- public int[] getMonths()
- {
- int[] months = new int[12];
- for(int i=1; i<=12; i++)
- {
- months[i-1] = i;
- }
- return months;
- }
- public int[] getYears()
- {
- int[] years = new int[120];
- for(int i=1; i<=120 ; i++)
- {
- years[i-1] = i + 1900;
- }
- return years;
- }
- public void acceptInfo()
- {
- // get uploaded file/image
- String fileName = null;
- InputStream input = null;
- try
- {
- input = image.getInputStream();
- fileName = image.getSubmittedFileName();
- }
- catch(IOException e)
- {
- System.err.println(e);
- }
- // debug log if all data were passed successfully
- System.out.println(this.fname + " " + this.lname + " " + this.day + "/" + this.month + "/" + this.year
- + " " + this.gender + " " + this.hasDependents + " " + fileName);
- // create excel file and download
- this.createExcelFile(fname, lname, day, month, year, gender, hasDependents, input);
- }
- // method for creating and downloading the excel file
- XSSFRow row; XSSFCell cell;
- private void createExcelFile(String fname, String lname, int day, int month, int year, String gender, boolean hasDependents, InputStream input)
- {
- XSSFWorkbook wb = new XSSFWorkbook();
- XSSFSheet sh = wb.createSheet("Page 1");
- // first name, last name
- row = sh.createRow(1);
- cell = row.createCell(0);
- cell.setCellValue(fname);
- cell = row.createCell(1);
- cell.setCellValue(lname);
- // birthdate
- row = sh.createRow(2);
- String birthdate = day + "/" + month + "/" + year;
- cell = row.createCell(0);
- cell.setCellValue("Birthdate:");
- cell = row.createCell(1);
- cell.setCellValue(birthdate);
- // gender
- row = sh.createRow(3);
- cell = row.createCell(0);
- cell.setCellValue("Gender:");
- cell = row.createCell(1);
- cell.setCellValue(gender);
- // dependents
- row = sh.createRow(4);
- cell = row.createCell(0);
- cell.setCellValue("Has Dependents?");
- cell = row.createCell(1);
- String dep = hasDependents ? "Yes" : "None";
- cell.setCellValue(dep);
- // place image to sheet
- try
- {
- byte[] bytes = IOUtils.toByteArray(input); // convert image to bytes, needed for the getting the image index method
- int imageIndex = wb.addPicture(bytes, wb.PICTURE_TYPE_JPEG); // get image index, needed for create picture method
- input.close(); // close inputstream
- XSSFDrawing drawing = sh.createDrawingPatriarch(); // create drawing container
- XSSFClientAnchor anchor = new XSSFClientAnchor(); // define top left corner
- anchor.setCol1(5);
- anchor.setRow1(6);
- anchor.setCol2(17);
- anchor.setRow2(18);
- XSSFPicture pic = drawing.createPicture(anchor, imageIndex); // invoke createPicture and pass the anchor point and image id
- // pic.resize(); // resize the image to its original size
- }
- catch(IOException e)
- {
- System.err.print(e);
- }
- // code to download file
- FacesContext facesContext = FacesContext.getCurrentInstance();
- ExternalContext externalContext = facesContext.getExternalContext();
- externalContext.setResponseContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
- externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"davefileexport.xlsx\"");
- try {
- wb.write(externalContext.getResponseOutputStream());
- facesContext.responseComplete();
- System.out.println("Workbook created");
- }
- catch(IOException e) {
- System.err.println(e);
- }
- }
- // property GETTERS
- public String getFname()
- {
- return fname;
- }
- public String getLname()
- {
- return lname;
- }
- public int getDay()
- {
- return day;
- }
- public int getMonth()
- {
- return month;
- }
- public int getYear()
- {
- return year;
- }
- public String getGender()
- {
- return this.gender;
- }
- public boolean getHasDependents()
- {
- return this.hasDependents;
- }
- public Part getImage()
- {
- return this.image;
- }
- // property SETTERS
- public void setFname(String fname)
- {
- this.fname = fname;
- }
- public void setLname(String lname)
- {
- this.lname = lname;
- }
- public void setDay(int day)
- {
- this.day = day;
- }
- public void setMonth(int month)
- {
- this.month = month;
- }
- public void setYear(int year)
- {
- this.year = year;
- }
- public void setGender(String gender)
- {
- this.gender = gender;
- }
- public void setHasDependents(boolean hasDependents)
- {
- this.hasDependents = hasDependents;
- }
- public void setImage(Part image)
- {
- this.image = image;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement