package ru.nbch.indicsearchservice.util; import jakarta.annotation.Nullable; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import org.apache.commons.lang3.mutable.MutableInt; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFFont; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import ru.nbch.indicsearchservice.model.TableData; @Component public class ExcelWriter { private static final Logger log = LoggerFactory.getLogger(ExcelWriter.class); public void writeExcelFile(List tableDataList) { Map> tableDataGroupedByTables = tableDataList.stream() .collect(Collectors.groupingBy(TableData::getTableName)); Path excelFile = Path.of("2.xlsx"); try (SXSSFWorkbook workbook = new SXSSFWorkbook(); OutputStream os = Files.newOutputStream(excelFile)) { CellStyle headerCellStyle = createHeaderCellStyle(workbook); CellStyle cellStyle = createCellStyle(workbook); for (Map.Entry> entry : tableDataGroupedByTables.entrySet()) { String tableName = entry.getKey(); List tableRows = entry.getValue(); Sheet tableSheet = createSheet(workbook, tableName.toUpperCase()); MutableInt currentRow = new MutableInt(0); for (TableData tableRow : tableRows) { writeDataSheetIntoSheetRow( tableRow, tableSheet, currentRow, headerCellStyle, cellStyle ); } } workbook.write(os); } catch (IOException e) { log.error(e.getMessage(), e); } } private CellStyle createHeaderCellStyle(Workbook workbook) { CellStyle headerCellStyle = workbook.createCellStyle(); headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("SansSerif"); font.setFontHeightInPoints((short) 11); font.setBold(true); headerCellStyle.setFont(font); return headerCellStyle; } private CellStyle createCellStyle(Workbook workbook) { CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("Calibri"); font.setFontHeightInPoints((short) 11); cellStyle.setFont(font); return cellStyle; } private Sheet createSheet(Workbook workbook, String sheetName) { Sheet sheet = workbook.createSheet(sheetName); sheet.setColumnWidth(0, 25 * 256); sheet.setColumnWidth(1, 115 * 256); sheet.setColumnWidth(2, 38 * 256); return sheet; } private void createHeaderRow(String localizedTableName, Sheet sheet, MutableInt currentRow, CellStyle headerCellStyle) { Row headerRow = sheet.createRow(currentRow.intValue()); headerRow.setHeightInPoints(15); Cell headerCell = headerRow.createCell(0); headerCell.setCellValue(localizedTableName); headerCell.setCellStyle(headerCellStyle); sheet.addMergedRegion(new CellRangeAddress(currentRow.intValue(), currentRow.intValue(), 0, 2)); currentRow.increment(); } public void writeDataSheetIntoSheetRow(TableData tableData, Sheet sheet, MutableInt currentRow, CellStyle headerCellStyle, CellStyle cellStyle) { createHeaderRow( tableData.getTableName(), sheet, currentRow, headerCellStyle ); for (TableData.FieldData fieldData : tableData.getFieldData()) { writeFieldDataIntoSheetRow( fieldData.columnName(), fieldData.columnName(), fieldData.columnValue(), sheet, currentRow, cellStyle ); } currentRow.increment(); if (!tableData.getRelatedTableData().isEmpty()) { for (TableData relatedTableData : tableData.getRelatedTableData()) { writeDataSheetIntoSheetRow( relatedTableData, sheet, currentRow, headerCellStyle, cellStyle ); } } } private void writeFieldDataIntoSheetRow(String fieldKey, String localizedFieldKey, @Nullable Object fieldValue, Sheet sheet, MutableInt currentRow, CellStyle cellStyle) { Row dataRow = sheet.createRow(currentRow.intValue()); dataRow.setHeightInPoints(15); Cell dataFieldCell = dataRow.createCell(0); dataFieldCell.setCellValue(fieldKey.toUpperCase()); dataFieldCell.setCellStyle(cellStyle); Cell descriptionCell = dataRow.createCell(1); descriptionCell.setCellValue(localizedFieldKey); descriptionCell.setCellStyle(cellStyle); Cell dataValueCell = dataRow.createCell(2); dataValueCell.setCellValue(Optional.ofNullable(fieldValue).orElse("").toString()); dataValueCell.setCellStyle(cellStyle); currentRow.increment(); } }