|
22 | 22 | import java.util.Collection; |
23 | 23 | import java.util.Objects; |
24 | 24 | import java.util.function.Supplier; |
| 25 | +import lombok.extern.slf4j.Slf4j; |
25 | 26 | import org.apache.fesod.excel.ExcelWriter; |
26 | 27 | import org.apache.fesod.excel.exception.ExcelGenerateException; |
27 | 28 | import org.apache.fesod.excel.write.metadata.WriteSheet; |
28 | 29 | import org.apache.fesod.excel.write.metadata.fill.FillConfig; |
| 30 | +import org.apache.poi.ss.usermodel.Workbook; |
29 | 31 |
|
30 | 32 | /** |
31 | 33 | * Build sheet |
32 | 34 | * |
33 | 35 | * |
34 | 36 | */ |
| 37 | +@Slf4j |
35 | 38 | public class ExcelWriterSheetBuilder extends AbstractExcelWriterParameterBuilder<ExcelWriterSheetBuilder, WriteSheet> { |
36 | 39 | private ExcelWriter excelWriter; |
37 | 40 | /** |
@@ -73,13 +76,13 @@ public ExcelWriterSheetBuilder sheetNoIfNotNull(Integer sheetNo) { |
73 | 76 | * @return |
74 | 77 | */ |
75 | 78 | public ExcelWriterSheetBuilder sheetName(String sheetName) { |
76 | | - writeSheet.setSheetName(sheetName); |
| 79 | + writeSheet.setSheetName(sensitiveSheetName(sheetName)); |
77 | 80 | return this; |
78 | 81 | } |
79 | 82 |
|
80 | 83 | public ExcelWriterSheetBuilder sheetNameIfNotNull(String sheetName) { |
81 | 84 | if (Objects.nonNull(sheetName)) { |
82 | | - writeSheet.setSheetName(sheetName); |
| 85 | + writeSheet.setSheetName(sensitiveSheetName(sheetName)); |
83 | 86 | } |
84 | 87 | return this; |
85 | 88 | } |
@@ -136,4 +139,31 @@ public ExcelWriterTableBuilder table(Integer tableNo) { |
136 | 139 | protected WriteSheet parameter() { |
137 | 140 | return writeSheet; |
138 | 141 | } |
| 142 | + |
| 143 | + /** |
| 144 | + * Processes worksheet names to ensure they comply with MS Excel's length limitations. |
| 145 | + * <p> |
| 146 | + * If the provided sheet name exceeds the maximum allowed length defined by |
| 147 | + * {@link Workbook#MAX_SENSITIVE_SHEET_NAME_LEN}, it will be truncated to fit within |
| 148 | + * the limit. A warning message is logged when truncation occurs, as the original |
| 149 | + * sheet name will not be available in scenarios such as formula references. |
| 150 | + * </p> |
| 151 | + * |
| 152 | + * @param sheetName the original worksheet name to process |
| 153 | + * @return the processed sheet name that complies with Excel's length restrictions |
| 154 | + */ |
| 155 | + private String sensitiveSheetName(String sheetName) { |
| 156 | + if (sheetName.length() > Workbook.MAX_SENSITIVE_SHEET_NAME_LEN) { |
| 157 | + String trimmedSheetName = sheetName.substring(0, Workbook.MAX_SENSITIVE_SHEET_NAME_LEN); |
| 158 | + |
| 159 | + // we still need to warn about the trimming as the original sheet name won't be available |
| 160 | + // e.g. when referenced by formulas |
| 161 | + log.warn( |
| 162 | + "Sheet '{}' will be added with a trimmed name '{}' for MS Excel compliance.", |
| 163 | + sheetName, |
| 164 | + trimmedSheetName); |
| 165 | + return trimmedSheetName; |
| 166 | + } |
| 167 | + return sheetName; |
| 168 | + } |
139 | 169 | } |
0 commit comments