use of io.jmix.ui.component.data.meta.EntityDataGridItems in project jmix by jmix-framework.
the class ExcelExporter method exportDataGrid.
@Override
public void exportDataGrid(Downloader downloader, DataGrid<Object> dataGrid, ExportMode exportMode) {
if (downloader == null) {
throw new IllegalArgumentException("Downloader is null");
}
createWorkbookWithSheet();
createFonts();
createFormats();
List<DataGrid.Column<Object>> columns = dataGrid.getColumns();
int r = 0;
Row row = sheet.createRow(r);
createAutoColumnSizers(columns.size());
float maxHeight = sheet.getDefaultRowHeightInPoints();
CellStyle headerCellStyle = wb.createCellStyle();
headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
for (DataGrid.Column column : columns) {
String caption = column.getCaption();
int countOfReturnSymbols = StringUtils.countMatches(caption, "\n");
if (countOfReturnSymbols > 0) {
maxHeight = Math.max(maxHeight, (countOfReturnSymbols + 1) * sheet.getDefaultRowHeightInPoints());
headerCellStyle.setWrapText(true);
}
}
row.setHeightInPoints(maxHeight);
for (int c = 0; c < columns.size(); c++) {
DataGrid.Column column = columns.get(c);
String caption = column.getCaption();
Cell cell = row.createCell(c);
RichTextString richTextString = createStringCellValue(caption);
richTextString.applyFont(boldFont);
cell.setCellValue(richTextString);
ExcelAutoColumnSizer sizer = new ExcelAutoColumnSizer();
sizer.notifyCellValue(caption, boldFont);
sizers[c] = sizer;
cell.setCellStyle(headerCellStyle);
}
EntityDataGridItems<Object> dataGridSource = (EntityDataGridItems) dataGrid.getItems();
if (dataGridSource == null) {
throw new IllegalStateException("DataGrid is not bound to data");
}
if (exportMode == ExportMode.SELECTED && dataGrid.getSelected().size() > 0) {
Set<Object> selected = dataGrid.getSelected();
List<Object> ordered = dataGridSource.getItems().filter(selected::contains).collect(Collectors.toList());
for (Object item : ordered) {
if (checkIsRowNumberExceed(r)) {
break;
}
createDataGridRow(dataGrid, columns, 0, ++r, Id.of(item).getValue());
}
} else {
if (dataGrid instanceof TreeDataGrid) {
TreeDataGrid treeDataGrid = (TreeDataGrid) dataGrid;
TreeDataGridItems<Object> treeDataGridItems = (TreeDataGridItems) dataGridSource;
List<Object> items = treeDataGridItems.getChildren(null).collect(Collectors.toList());
for (Object item : items) {
if (checkIsRowNumberExceed(r)) {
break;
}
r = createDataGridHierarchicalRow(treeDataGrid, treeDataGridItems, columns, 0, r, item);
}
} else {
for (Object itemId : dataGridSource.getItems().map(entity -> Id.of(entity).getValue()).collect(Collectors.toList())) {
if (checkIsRowNumberExceed(r)) {
break;
}
createDataGridRow(dataGrid, columns, 0, ++r, itemId);
}
}
}
for (int c = 0; c < columns.size(); c++) {
sheet.setColumnWidth(c, sizers[c].getWidth() * COL_WIDTH_MAGIC);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
wb.write(out);
} catch (IOException e) {
throw new RuntimeException("Unable to write document", e);
}
ByteArrayDataProvider dataProvider = new ByteArrayDataProvider(out.toByteArray(), uiProperties.getSaveExportedByteArrayDataThresholdBytes(), coreProperties.getTempDir());
switch(exportFormat) {
case XLSX:
downloader.download(dataProvider, getFileName(dataGrid) + "." + XLSX.getFileExt(), XLSX);
break;
case XLS:
downloader.download(dataProvider, getFileName(dataGrid) + "." + XLS.getFileExt(), XLS);
break;
}
}
Aggregations