0

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:

2
  • You need to show more code, especially the code that is producing the problem. Commented Mar 30, 2022 at 6:15
  • I added both codes. OK ? Commented Mar 30, 2022 at 6:33

1 Answer 1

2

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'.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, but also the line setResource("dlexcel", resClass); has to be changed . thanks
I edited my response so future readers will get it all in one step.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.