Search code examples
javaexceldownloadvaadin7

java create multiple excel files


How to create multiple excel files and open for user to download. I am using next code for one, if I do it again , one after the other, I get two excels, but both are the one that is created second.

code to create first excel

GotrPersClass gotrPersClas = ...;
try {
    FileResource res;
    res = ReportTakeAway.generateCustomExcel1(gotrPersClas, office, dateF, dateT);
    if (res != null) {
        setResource("dlexcel", res);
        ResourceReference rr = ResourceReference.create(res, this, "dlexcel");
        getUI().getPage().open(rr.getURL(), "_blank", false);
    }
}
catch (Exception e) {
}

code to create second excel

List<Map<String, Object>> rs = ...;
try {
    FileResource resClass;
    resClass = ReportTakeAway.generateCustomExcel2(rs, office, dateF, dateT);

    if (resClass != null) {
        setResource("dlexcel", resClass);
        ResourceReference rr = ResourceReference.create(resClass, this, "dlexcel");
        getUI().getPage().open(rr.getURL(), "_blank", false);

    }
}
catch (Exception e) {
}

both codes are one after another

found this link, but is not for java:


Solution

  • I believe the problem is in the lines

    setResource("dlexcel", resClass);
    ResourceReference rr = ResourceReference.create(resClass, this, "dlexcel");
    

    which create the reference for the client (browser) in both cases. So regardless which download the user chooses, the file referenced as 'dlexel' will only provide one of the two.

    https://vaadin.com/api/framework/7.7.30/com/vaadin/server/ResourceReference.html#ResourceReference-com.vaadin.server.Resource-com.vaadin.server.ClientConnector-java.lang.String-

    Try to have unique values for the parameter 'key'.