From 1c7a5faae81286e89423bf707bfae505c50c1661 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Thu, 3 Dec 2020 13:26:46 +0800 Subject: [PATCH] =?UTF-8?q?Excel=E6=94=AF=E6=8C=81=E6=B3=A8=E8=A7=A3align?= =?UTF-8?q?=E5=AF=B9=E9=BD=90=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/annotation/Excel.java | 21 +++++++++++++++++++ .../com/ruoyi/common/utils/poi/ExcelUtil.java | 18 +++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java index b7b8f979..bfe119e4 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java @@ -105,6 +105,27 @@ public @interface Excel */ public boolean isStatistics() default false; + /** + * 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右) + */ + Align align() default Align.AUTO; + + public enum Align + { + AUTO(0), LEFT(1), CENTER(2), RIGHT(3); + private final int value; + + Align(int value) + { + this.value = value; + } + + public int value() + { + return this.value; + } + } + /** * 字段类型(0:导出导入;1:仅导出;2:仅导入) */ diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index d9f4efd7..9003a9c7 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -473,6 +473,21 @@ public class ExcelUtil style.setFont(totalFont); styles.put("total", style); + style = wb.createCellStyle(); + style.cloneStyleFrom(styles.get("data")); + style.setAlignment(HorizontalAlignment.LEFT); + styles.put("data1", style); + + style = wb.createCellStyle(); + style.cloneStyleFrom(styles.get("data")); + style.setAlignment(HorizontalAlignment.CENTER); + styles.put("data2", style); + + style = wb.createCellStyle(); + style.cloneStyleFrom(styles.get("data")); + style.setAlignment(HorizontalAlignment.RIGHT); + styles.put("data3", style); + return styles; } @@ -555,7 +570,8 @@ public class ExcelUtil { // 创建cell cell = row.createCell(column); - cell.setCellStyle(styles.get("data")); + int align = attr.align().value(); + cell.setCellStyle(styles.get("data" + (align >= 1 && align <= 3 ? align : ""))); // 用于读取对象中的属性 Object value = getTargetValue(vo, field, attr);