能否导出table所有的列?
我看了看源码,目前是不支持导出不可见列(visible=false)或折叠的列(collapsed=“true”)。
但是修改一下,可以支持导出折叠列。但是不可见列还是不支持。
在你代码中新建一个包:io.jmix.gridexportui.exporter.excel
,因为要支持一个 protected 方法。
包内新建 CustomTableExporter 继承 ExcelExporter
,复制 exportTable
的代码。如果你使用的是 DataGrid,需要复制相应的方法:,然后把 table.getNotCollapsedColumns()
改为 table.getColumns()
:
@Primary
@Component("custom_ExcelExporter")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class CustomTableExporter extends ExcelExporter {
public CustomTableExporter(GridExportProperties gridExportProperties, AllRecordsExporter allRecordsExporter) {
super(gridExportProperties, allRecordsExporter);
}
private RichTextString createStringCellValue(String str) {
return new XSSFRichTextString(str);
}
@Override
public void exportTable(Downloader downloader, Table<Object> table, ExportMode exportMode) {
if (downloader == null) {
throw new IllegalArgumentException("Downloader is null");
}
if (table.getItems() == null) {
throw new IllegalStateException("Table items should not be null");
}
@SuppressWarnings("unchecked")
List<Table.Column<Object>> columns = Collections.unmodifiableList(table.getColumns()).stream()
.map(c -> (Table.Column<Object>) c)
.collect(Collectors.toList());
createWorkbookWithSheet();
try {
createFonts();
createFormats();
int r = 0;
Row row = sheet.createRow(r);
createAutoColumnSizers(columns.size());
float maxHeight = sheet.getDefaultRowHeightInPoints();
CellStyle headerCellStyle = wb.createCellStyle();
headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
for (Table.Column<Object> 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++) {
Table.Column<Object> 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);
}
TableItems<Object> tableItems = table.getItems();
if (exportMode == ExportMode.SELECTED_ROWS && table.getSelected().size() > 0) {
Set<Object> selected = table.getSelected();
List<Object> ordered = tableItems.getItemIds().stream()
.map(tableItems::getItem)
.filter(selected::contains)
.collect(Collectors.toList());
for (Object item : ordered) {
if (checkIsRowNumberExceed(r)) {
break;
}
createRow(table, columns, 0, ++r, Id.of(item).getValue());
}
} else if (exportMode == ExportMode.CURRENT_PAGE) {
if (table.isAggregatable() && exportAggregation
&& hasAggregatableColumn(table)) {
if (table.getAggregationStyle() == Table.AggregationStyle.TOP) {
r = createAggregatableRow(table, columns, ++r, 1);
}
}
if (table instanceof TreeTable) {
TreeTable<Object> treeTable = (TreeTable<Object>) table;
TreeTableItems<Object> treeTableSource = (TreeTableItems<Object>) treeTable.getItems();
if (treeTableSource != null) {
for (Object itemId : treeTableSource.getRootItemIds()) {
if (checkIsRowNumberExceed(r)) {
break;
}
r = createHierarchicalRow(treeTable, columns, exportExpanded, r, itemId);
}
}
} else if (table instanceof GroupTable && tableItems instanceof GroupTableItems
&& ((GroupTableItems<Object>) tableItems).hasGroups()) {
GroupTableItems<Object> groupTableSource = (GroupTableItems<Object>) tableItems;
for (Object item : groupTableSource.rootGroups()) {
if (checkIsRowNumberExceed(r)) {
break;
}
r = createGroupRow((GroupTable<Object>) table, columns, ++r, (GroupInfo<?>) item, 0);
}
} else {
if (tableItems != null) {
for (Object itemId : tableItems.getItemIds()) {
if (checkIsRowNumberExceed(r)) {
break;
}
createRow(table, columns, 0, ++r, itemId);
}
}
}
if (table.isAggregatable() && exportAggregation
&& hasAggregatableColumn(table)) {
if (table.getAggregationStyle() == Table.AggregationStyle.BOTTOM) {
r = createAggregatableRow(table, columns, ++r, 1);
}
}
} else if (exportMode == ExportMode.ALL_ROWS) {
boolean addLevelPadding = !(table instanceof TreeTable);
allRecordsExporter.exportAll(tableItems, (context) -> {
createRowForEntityInstance(table, columns, 0, context.getRowNumber(), context.getEntity(),
addLevelPadding);
}, this::checkIsRowNumberExceed);
}
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);
}
if (isXlsxMaxRowNumberExceeded()) {
showWarnNotification(table);
}
ByteArrayDataProvider dataProvider = new ByteArrayDataProvider(out.toByteArray(),
uiProperties.getSaveExportedByteArrayDataThresholdBytes(), coreProperties.getTempDir());
downloader.download(dataProvider, getFileName(table) + ".xlsx", XLSX);
} finally {
disposeWorkBook();
}
}
}
如果项目报一些 poi 的类无法引入,则加上下面的依赖:
implementation 'org.apache.poi:poi:4.1.2'
implementation 'org.apache.poi:poi-ooxml:4.1.2'
column 设置collapsed值的必须id是entity的字段名,但是设置visible是可以随意设置id的
这也就意味着,如果我想自定义导出一些东西,我要在entity加一些jmixproperty
我看了源码后也感觉目前这个导出组件是还有些不方便的地方。不是那么灵活。
是的 导出一般伴随导入,批量操作这种。不仅仅是一些界面显示的字段,还可能包含子表等。
而且折叠的体验还是不好,隐藏好接受些,导出用来导入的数据可能涉及一些字段是用户无须查看的。