Commit 1c7a5faae81286e89423bf707bfae505c50c1661
1 parent
23868c4f
Excel支持注解align对齐方式
Showing
2 changed files
with
38 additions
and
1 deletions
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
| @@ -106,6 +106,27 @@ public @interface Excel | @@ -106,6 +106,27 @@ public @interface Excel | ||
| 106 | public boolean isStatistics() default false; | 106 | public boolean isStatistics() default false; |
| 107 | 107 | ||
| 108 | /** | 108 | /** |
| 109 | + * 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右) | ||
| 110 | + */ | ||
| 111 | + Align align() default Align.AUTO; | ||
| 112 | + | ||
| 113 | + public enum Align | ||
| 114 | + { | ||
| 115 | + AUTO(0), LEFT(1), CENTER(2), RIGHT(3); | ||
| 116 | + private final int value; | ||
| 117 | + | ||
| 118 | + Align(int value) | ||
| 119 | + { | ||
| 120 | + this.value = value; | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + public int value() | ||
| 124 | + { | ||
| 125 | + return this.value; | ||
| 126 | + } | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + /** | ||
| 109 | * 字段类型(0:导出导入;1:仅导出;2:仅导入) | 130 | * 字段类型(0:导出导入;1:仅导出;2:仅导入) |
| 110 | */ | 131 | */ |
| 111 | Type type() default Type.ALL; | 132 | Type type() default Type.ALL; |
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
| @@ -473,6 +473,21 @@ public class ExcelUtil<T> | @@ -473,6 +473,21 @@ public class ExcelUtil<T> | ||
| 473 | style.setFont(totalFont); | 473 | style.setFont(totalFont); |
| 474 | styles.put("total", style); | 474 | styles.put("total", style); |
| 475 | 475 | ||
| 476 | + style = wb.createCellStyle(); | ||
| 477 | + style.cloneStyleFrom(styles.get("data")); | ||
| 478 | + style.setAlignment(HorizontalAlignment.LEFT); | ||
| 479 | + styles.put("data1", style); | ||
| 480 | + | ||
| 481 | + style = wb.createCellStyle(); | ||
| 482 | + style.cloneStyleFrom(styles.get("data")); | ||
| 483 | + style.setAlignment(HorizontalAlignment.CENTER); | ||
| 484 | + styles.put("data2", style); | ||
| 485 | + | ||
| 486 | + style = wb.createCellStyle(); | ||
| 487 | + style.cloneStyleFrom(styles.get("data")); | ||
| 488 | + style.setAlignment(HorizontalAlignment.RIGHT); | ||
| 489 | + styles.put("data3", style); | ||
| 490 | + | ||
| 476 | return styles; | 491 | return styles; |
| 477 | } | 492 | } |
| 478 | 493 | ||
| @@ -555,7 +570,8 @@ public class ExcelUtil<T> | @@ -555,7 +570,8 @@ public class ExcelUtil<T> | ||
| 555 | { | 570 | { |
| 556 | // 创建cell | 571 | // 创建cell |
| 557 | cell = row.createCell(column); | 572 | cell = row.createCell(column); |
| 558 | - cell.setCellStyle(styles.get("data")); | 573 | + int align = attr.align().value(); |
| 574 | + cell.setCellStyle(styles.get("data" + (align >= 1 && align <= 3 ? align : ""))); | ||
| 559 | 575 | ||
| 560 | // 用于读取对象中的属性 | 576 | // 用于读取对象中的属性 |
| 561 | Object value = getTargetValue(vo, field, attr); | 577 | Object value = getTargetValue(vo, field, attr); |