コード例 #1
0
 @GET
 @Path("/getreport")
 @Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
 public Response exportExcel(@QueryParam("from") String from, @QueryParam("to") String to)
     throws Exception {
   String params = from + to;
   System.out.println(params);
   File file = new File("src/main/resources/template.xlsx");
   FileInputStream inputStream = new FileInputStream(file);
   List<Customer> customerList = null;
   XSSFWorkbook wb = new XSSFWorkbook(inputStream);
   XSSFSheet sheet = wb.getSheetAt(0);
   // CellStyle dateStyle = wb.createCellStyle();
   // CreationHelper createHelper = wb.getCreationHelper();
   // dateStyle.setDataFormat(createHelper.createDataFormat().getFormat(
   // "yyyy/mm/dd hh:mm"));
   if (from != null && to != null) {
     Date startDate = Utils.formatDate(from);
     Date endDate = Utils.formatDate(to);
     customerList = dao.findByDate(startDate, endDate);
     // wb = Utils.fillReport(wb, customerList);
   } else {
     customerList = dao.findAll();
     // wb = Utils.fillReport(wb, customerList);
   }
   for (int i = 0; i < customerList.size(); i++) {
     XSSFRow row = sheet.createRow(i + 3);
     Customer customer = customerList.get(i);
     for (int j = 0; j < 10; j++) {
       XSSFCell cell = row.createCell(j);
       switch (j) {
         case 0:
           cell.setCellValue(customer.getCreated());
           cell.setCellStyle(sheet.getColumnStyle(j));
           // cell.setCellStyle(dateStyle);
           break;
         case 1:
           cell.setCellValue(customer.getName());
           break;
         case 2:
           cell.setCellValue(customer.getEmail());
           break;
         case 3:
           cell.setCellValue(customer.getPhone());
           break;
         case 4:
           cell.setCellValue(customer.getOrganisation());
           break;
         case 5:
           cell.setCellValue(customer.getStartDate());
           cell.setCellStyle(sheet.getColumnStyle(j));
           break;
         case 6:
           cell.setCellValue(customer.getEndDate());
           cell.setCellStyle(sheet.getColumnStyle(j));
           break;
         case 7:
           cell.setCellValue(customer.getNumberOfPeople());
           break;
         case 8:
           cell.setCellValue(customer.isCatering());
           break;
         case 9:
           cell.setCellValue(customer.getAdditionalComments());
           break;
         default:
           break;
       }
     }
   }
   StreamingOutput stream =
       new StreamingOutput() {
         public void write(OutputStream output) throws IOException, WebApplicationException {
           try {
             wb.write(output);
             wb.close();
           } catch (Exception e) {
             throw new WebApplicationException(e);
           }
         }
       };
   Date date = new Date();
   SimpleDateFormat sdf = new SimpleDateFormat("YYYY_MM_dd");
   String dateString = sdf.format(date);
   return Response.ok(stream)
       .header(
           "content-disposition",
           "attachment; filename = space-bookings-export_" + dateString + ".xlsx")
       .build();
 }
コード例 #2
0
 @GET
 @Path("/getcount")
 public int getAll() {
   return dao.findAll().size();
 }