|
| 1 | +package net.finmath.smartcontract.simulation; |
| 2 | + |
| 3 | +import java.io.Closeable; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.OutputStream; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.util.LinkedHashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Objects; |
| 12 | + |
| 13 | +import org.apache.poi.ss.usermodel.Cell; |
| 14 | +import org.apache.poi.ss.usermodel.CellStyle; |
| 15 | +import org.apache.poi.ss.usermodel.CellType; |
| 16 | +import org.apache.poi.ss.usermodel.DataFormat; |
| 17 | +import org.apache.poi.ss.usermodel.Row; |
| 18 | +import org.apache.poi.ss.usermodel.Sheet; |
| 19 | +import org.apache.poi.ss.usermodel.Workbook; |
| 20 | +import org.apache.poi.ss.usermodel.WorkbookFactory; |
| 21 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 22 | + |
| 23 | +public class ExcelTableWriter implements Closeable { |
| 24 | + |
| 25 | + private final Path path; |
| 26 | + private final Workbook wb; |
| 27 | + |
| 28 | + private final CellStyle numericStyle; |
| 29 | + |
| 30 | + public ExcelTableWriter(Path path) throws IOException { |
| 31 | + this.path = path; |
| 32 | + |
| 33 | + if (Files.exists(path)) { |
| 34 | + try (InputStream in = Files.newInputStream(path)) { |
| 35 | + this.wb = WorkbookFactory.create(in); |
| 36 | + } |
| 37 | + } else { |
| 38 | + this.wb = new XSSFWorkbook(); |
| 39 | + } |
| 40 | + |
| 41 | + DataFormat df = wb.createDataFormat(); |
| 42 | + |
| 43 | + numericStyle = wb.createCellStyle(); |
| 44 | + numericStyle.setDataFormat(df.getFormat("0.###############")); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * Writes a "column table" to a sheet. |
| 50 | + * |
| 51 | + * @param sheetName sheet to write into |
| 52 | + * @param columns LinkedHashMap preserves column order |
| 53 | + * @param replaceSheet if true, deletes existing sheet with same name |
| 54 | + * @param autosize if true, autosize columns (can be slow on big sheets) |
| 55 | + */ |
| 56 | + public void writeTable( |
| 57 | + String sheetName, |
| 58 | + LinkedHashMap<String, ? extends List<?>> columns, |
| 59 | + boolean replaceSheet, |
| 60 | + boolean autosize |
| 61 | + ) { |
| 62 | + Objects.requireNonNull(sheetName, "sheetName"); |
| 63 | + Objects.requireNonNull(columns, "columns"); |
| 64 | + |
| 65 | + if (columns.isEmpty()) { |
| 66 | + throw new IllegalArgumentException("No columns provided."); |
| 67 | + } |
| 68 | + |
| 69 | + // Determine row count (max length) |
| 70 | + int rowCount = columns.values().stream().mapToInt(List::size).max().orElse(0); |
| 71 | + |
| 72 | + Sheet sheet = getOrCreateSheet(sheetName, replaceSheet); |
| 73 | + |
| 74 | + // Header |
| 75 | + Row header = sheet.createRow(0); |
| 76 | + int colIdx = 0; |
| 77 | + for (String name : columns.keySet()) { |
| 78 | + Cell cell = header.createCell(colIdx++, CellType.STRING); |
| 79 | + cell.setCellValue(name); |
| 80 | + } |
| 81 | + |
| 82 | + // Data |
| 83 | + for (int r = 0; r < rowCount; r++) { |
| 84 | + Row row = sheet.createRow(r + 1); |
| 85 | + |
| 86 | + colIdx = 0; |
| 87 | + for (List<?> col : columns.values()) { |
| 88 | + Object value = (r < col.size()) ? col.get(r) : null; |
| 89 | + writeValue(row.createCell(colIdx++), value); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (autosize) { |
| 94 | + for (int c = 0; c < columns.size(); c++) { |
| 95 | + sheet.autoSizeColumn(c); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private Sheet getOrCreateSheet(String sheetName, boolean replaceSheet) { |
| 101 | + Sheet existing = wb.getSheet(sheetName); |
| 102 | + |
| 103 | + if (existing != null && replaceSheet) { |
| 104 | + int idx = wb.getSheetIndex(existing); |
| 105 | + wb.removeSheetAt(idx); |
| 106 | + return wb.createSheet(sheetName); |
| 107 | + } |
| 108 | + |
| 109 | + return existing != null ? existing : wb.createSheet(sheetName); |
| 110 | + } |
| 111 | + |
| 112 | + private void writeValue(Cell cell, Object value) { |
| 113 | + if (value == null) { |
| 114 | + cell.setBlank(); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (value instanceof Number n) { |
| 119 | + cell.setCellValue(n.doubleValue()); |
| 120 | + cell.setCellStyle(numericStyle); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + // Fallback: string |
| 125 | + cell.setCellValue(String.valueOf(value)); |
| 126 | + } |
| 127 | + |
| 128 | + public void save() throws IOException { |
| 129 | + Files.createDirectories(path.toAbsolutePath().getParent()); |
| 130 | + try (OutputStream out = Files.newOutputStream(path)) { |
| 131 | + wb.write(out); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void close() throws IOException { |
| 137 | + wb.close(); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments