| | |
| | | * @author ruoyi |
| | | */ |
| | | public class ExcelUtil<T> { |
| | | |
| | | public static final String FORMULA_REGEX_STR = "=|-|\\+|@"; |
| | | public static final String[] FORMULA_STR = {"=", "-", "+", "@"}; |
| | | /** |
| | |
| | | * 标题 |
| | | */ |
| | | private String title; |
| | | /** |
| | | * 搜索时间 |
| | | */ |
| | | private String timeDesc; |
| | | /** |
| | | * 制表人 |
| | | */ |
| | | private String createUser; |
| | | /** |
| | | * 制表时间 |
| | | */ |
| | | private Date createTime; |
| | | |
| | | /** |
| | | * 最大高度 |
| | | */ |
| | |
| | | createSubHead(); |
| | | } |
| | | |
| | | public void init(List<T> list, String sheetName, String title, Type type, String timeDesc, String createUser, Date createTime) { |
| | | if (list == null) { |
| | | list = new ArrayList<T>(); |
| | | } |
| | | this.list = list; |
| | | this.sheetName = sheetName; |
| | | this.type = type; |
| | | this.title = title; |
| | | this.timeDesc = timeDesc; |
| | | this.createUser = createUser; |
| | | this.createTime = createTime; |
| | | createExcelField(); |
| | | createWorkbook(); |
| | | createTitle(); |
| | | createSecondHead(); |
| | | createSubHead(); |
| | | } |
| | | |
| | | /** |
| | | * 创建excel第一行标题 |
| | | */ |
| | |
| | | sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), titleLastCol)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 创建第二行自定义内容 |
| | | * |
| | | * @author sgj |
| | | * @since 2026/03/09 |
| | | */ |
| | | public void createSecondHead() { |
| | | if (StringUtils.isNotBlank(timeDesc) && StringUtils.isNotBlank(createUser) && createTime != null) { |
| | | subMergedFirstRowNum++; |
| | | subMergedLastRowNum++; |
| | | //第二行,添加搜索时间,创建人, 创建时间信息 |
| | | Row secondRow = sheet.createRow(rownum); |
| | | secondRow.setHeightInPoints(30); |
| | | int titleLastCol = this.fields.size() - 1; |
| | | if (isSubList()) { |
| | | titleLastCol = titleLastCol + subFields.size() - 1; |
| | | } |
| | | |
| | | // 创建三个单元格,分别用于搜索时间、创建人、创建时间 |
| | | // 第一列:搜索时间 |
| | | Cell timeDescCell = secondRow.createCell(0); |
| | | timeDescCell.setCellValue("查询时间:"+timeDesc); |
| | | timeDescCell.setCellStyle(styles.get("secTitle")); |
| | | |
| | | // 中间列:创建人 |
| | | int createUserCol = (titleLastCol + 1) / 2; |
| | | Cell createUserCell = secondRow.createCell(createUserCol); |
| | | createUserCell.setCellValue("制表人:"+createUser); |
| | | createUserCell.setCellStyle(styles.get("secTitle")); |
| | | |
| | | // 最后一列:创建时间 |
| | | Cell createTimeCell = secondRow.createCell(titleLastCol - 1); |
| | | createTimeCell.setCellValue("制表时间:"+parseDateToStr("yyyy-MM-dd HH:mm:ss", createTime)); |
| | | createTimeCell.setCellStyle(styles.get("secTitle")); |
| | | |
| | | // 合并单元格 |
| | | sheet.addMergedRegion(new CellRangeAddress(secondRow.getRowNum(), secondRow.getRowNum(), 0, 1)); |
| | | sheet.addMergedRegion(new CellRangeAddress(secondRow.getRowNum(), secondRow.getRowNum(), createUserCol, createUserCol + 1)); |
| | | sheet.addMergedRegion(new CellRangeAddress(secondRow.getRowNum(), secondRow.getRowNum(), titleLastCol - 1, titleLastCol)); |
| | | |
| | | rownum++; |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 创建对象的子列表名称 |
| | |
| | | this.init(list, sheetName, title, Type.EXPORT); |
| | | return exportExcel(); |
| | | } |
| | | |
| | | /** |
| | | * 对list数据源将其里面的数据导入到excel表单 |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param title 标题 |
| | | * @param timeDesc 查询时间 |
| | | * @param createUser 制表人 |
| | | * @param createTime 制表时间 |
| | | * @return 结果 |
| | | */ |
| | | public AjaxResult exportExcel(List<T> list, String sheetName, String title, String timeDesc, String createUser, Date createTime) { |
| | | if (StringUtils.isBlank(timeDesc) || StringUtils.isBlank(createUser) || createTime == null) { |
| | | this.init(list, sheetName, title, Type.EXPORT); |
| | | } else { |
| | | this.init(list, sheetName, title, Type.EXPORT, timeDesc, createUser, createTime); |
| | | } |
| | | return exportExcel(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 对list数据源将其里面的数据导入到excel表单 |
| | |
| | | style.setFont(dataFont); |
| | | styles.put("data", style); |
| | | |
| | | |
| | | style = wb.createCellStyle(); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | Font dataFontTwo = wb.createFont(); |
| | | dataFontTwo.setFontName("Arial"); |
| | | dataFontTwo.setFontHeightInPoints((short) 10); |
| | | style.setFont(dataFontTwo); |
| | | styles.put("secTitle", style); |
| | | |
| | | style = wb.createCellStyle(); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |