Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#315: Pass data map to SimpleExporter #316

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions jxls-poi/src/main/java/org/jxls/template/SimpleExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,48 @@ public void registerGridTemplate(InputStream inputStream) throws IOException {
templateBytes = ImageCommand.toByteArray(inputStream);
}

/**
* This is the main method of the SimpleExporter.
* @param headers a collection of headers
* @param dataObjects a collection of data objects
* @param objectProps a comma-separated list of object property names
* @param outputStream an OutputStream to write the final Excel file
*/
public void gridExport(Iterable<?> headers, Iterable<?> dataObjects, String objectProps, OutputStream outputStream) {
Map<String, Object> data = new HashMap<>();
data.put("headers", headers);
data.put("data", dataObjects);
gridExport(data, objectProps, outputStream);
}

/**
* #315
* @param data a data map that should contain a collection of headers named "headers" and a collection
* of data objects called "data"
* @param objectProps a comma-separated list of object property names
* @param outputStream an OutputStream to write the final Excel file
*/
public void gridExport(Map<String, Object> data, String objectProps, OutputStream outputStream) {
getBuilder(objectProps).buildAndFill(data, () -> outputStream);
}

protected JxlsPoiTemplateFillerBuilder getBuilder(String objectProps) {
if (templateBytes == null) {
InputStream is = SimpleExporter.class.getResourceAsStream(GRID_TEMPLATE_XLS);
try {
registerGridTemplate(is);
} catch (IOException e) {
throw new JxlsException("Failed to read default template file " + GRID_TEMPLATE_XLS, e);
}
}
Map<String, Object> data = new HashMap<>();
data.put("headers", headers);
data.put("data", dataObjects);
JxlsPoiTemplateFillerBuilder.newInstance().withTemplate(new ByteArrayInputStream(templateBytes))
.withAreaBuilder((transformer, ctc) -> {
List<Area> areas = new XlsCommentAreaBuilder().build(transformer, ctc);
GridCommand gridCommand = (GridCommand) areas.get(0).getCommandDataList().get(0).getCommand();
gridCommand.setProps(objectProps);
return areas;
})
.buildAndFill(data, () -> outputStream);
}
return JxlsPoiTemplateFillerBuilder.newInstance()
.withTemplate(new ByteArrayInputStream(templateBytes))
.withAreaBuilder((transformer, ctc) -> {
// Setting objectProps to jx:grid
List<Area> areas = new XlsCommentAreaBuilder().build(transformer, ctc);
GridCommand gridCommand = (GridCommand) areas.get(0).getCommandDataList().get(0).getCommand();
gridCommand.setProps(objectProps);
return areas;
});
}
}
1 change: 1 addition & 0 deletions jxls-site/docs/changes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Release Notes

## v3.1.0
- [#315 Pass data map to SimpleExporter](https://github.com/jxlsteam/jxls/issues/315)
- [#240 Table syntax support for AbstractFormulaProcessor.getFormulaCellRefs()](https://github.com/jxlsteam/jxls/issues/240)

## v3.0.0
Expand Down
7 changes: 7 additions & 0 deletions jxls-site/docs/special-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,10 @@ Where
`outputStream` an OutputStream to write the final Excel

Call registerGridTemplate() for registering your own custom template. The template must contain the jx:grid command.

There's an alternative method if you need more control over the data map (e.g. for adding more data vars in case of
using a custom template).

```
new SimpleExporter().gridExport(data, propertyNames, outputStream);
```