Exemplo n.º 1
0
  public static void export(Long project_id) {
    // Prepare data
    Project p = getActiveProject();
    if (p == null) {
      notFound("Project", project_id);
    }
    List<Listing> listings = p.listings;
    List<String> sheetNames = new ArrayList();
    List maps = new ArrayList();
    for (Listing l : listings) {
      Map map = new HashMap();
      map.put("project", VProject.createFromProject(p));

      sheetNames.add("(" + l.id + ") " + l.listingName);

      // l.items = Item.findByListing(l, "", l.sort, 1000);
      map.put("listing", l.listingName);
      map.put("items", Item.findByListing(l, "", l.sort, 1000));
      List<ItemField> selected_fields = l.getItemFields();
      List<ItemField> all_fields = Item.getItemFields();
      List<ItemField> other_fields = new ArrayList<ItemField>();

      for (ItemField i : all_fields) {
        if (!l.hasField(i.fieldName)) {
          other_fields.add(i);
        }
      }
      ;

      map.put("selected_fields", selected_fields);
      map.put("other_fields", other_fields);

      maps.add(map);
    }

    // Prepare template and data for excel export
    String filepath =
        Play.applicationPath
            + File.separator
            + "app"
            + File.separator
            + "views"
            + File.separator
            + "projects"
            + File.separator
            + "export.xls";
    VirtualFile templateFile = VirtualFile.open(filepath);
    InputStream inputStream = templateFile.inputstream();

    MiscUtil.ConsoleLog("use sync excel rendering");
    long start = System.currentTimeMillis();
    try {
      Workbook workbook =
          new XLSTransformer()
              .transformMultipleSheetsList(inputStream, maps, sheetNames, "map", new HashMap(), 0);
      workbook.write(response.out);
      inputStream.close();
      MiscUtil.ConsoleLog("Excel sync render takes " + (System.currentTimeMillis() - start));
    } catch (Exception e) {
      MiscUtil.ConsoleLog("ERROR: " + e.getMessage());
      error();
    }

    // Set correct response header
    try {
      response.setHeader(
          "Content-Disposition",
          "attachment; filename=" + (new URLCodec("utf-8").encode(p.name)) + ".xls");
    } catch (EncoderException e) {
      response.setHeader("Content-Disposition", "attachment; filename=exported.xls");
    }
    response.setContentTypeIfNotSet("application/vnd.ms-excel");
  }