From 372a56e49e35d4b2cc8db397152521f598974be2 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Tue, 5 Mar 2019 15:29:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=8D=A2prompt=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/poi/ExcelUtil.java | 52 +++++-------------- .../java/com/ruoyi/system/domain/SysUser.java | 3 +- 2 files changed, 13 insertions(+), 42 deletions(-) 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 80963cf0..1a8277e5 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 @@ -9,8 +9,6 @@ import com.ruoyi.common.reflect.ReflectUtils; import com.ruoyi.common.support.Convert; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.StringUtils; -import org.apache.poi.hssf.usermodel.DVConstraint; -import org.apache.poi.hssf.usermodel.HSSFDataValidation; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined; @@ -20,7 +18,6 @@ import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFDataValidation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -329,7 +326,7 @@ public class ExcelUtil if (attr.combo().length > 0) { // 这里默认设了2-101列只能选择不能输入. - setHSSFValidation(sheet, attr.combo(), 1, 100, i, i); + setXSSFValidation(sheet, attr.combo(), 1, 100, i, i); } } if (Type.EXPORT.equals(type)) @@ -422,11 +419,11 @@ public class ExcelUtil Object value = getTargetValue(vo, field, attr); String dateFormat = attr.dateFormat(); String readConverterExp = attr.readConverterExp(); - if (StringUtils.isNotEmpty(dateFormat)) + if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) { cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value)); } - else if (StringUtils.isNotEmpty(readConverterExp)) + else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)) { cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp)); } @@ -440,40 +437,15 @@ public class ExcelUtil } catch (Exception e) { - log.error("导出Excel失败{}", e.getMessage()); + log.error("导出Excel失败{}", e); } } } } - /** - * 设置单元格上提示 - * - * @param sheet 要设置的sheet. - * @param promptTitle 标题 - * @param promptContent 内容 - * @param firstRow 开始行 - * @param endRow 结束行 - * @param firstCol 开始列 - * @param endCol 结束列 - * @return 设置好的sheet. - */ - public static Sheet setHSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow, - int firstCol, int endCol) - { - // 构造constraint对象 - DVConstraint constraint = DVConstraint.createCustomFormulaConstraint("DD1"); - // 四个参数分别是:起始行、终止行、起始列、终止列 - CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol); - // 数据有效性对象 - HSSFDataValidation dataValidationView = new HSSFDataValidation(regions, constraint); - dataValidationView.createPromptBox(promptTitle, promptContent); - sheet.addValidationData(dataValidationView); - return sheet; - } - /** * 设置 POI XSSFSheet 单元格提示 + * * @param sheet 表单 * @param promptTitle 提示标题 * @param promptContent 提示内容 @@ -482,11 +454,13 @@ public class ExcelUtil * @param firstCol 开始列 * @param endCol 结束列 */ - public static void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow, int firstCol, int endCol) { - DataValidationHelper dvHelper = sheet.getDataValidationHelper(); - DataValidationConstraint constraint = dvHelper.createCustomConstraint("*"); + public void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow, + int firstCol, int endCol) + { + DataValidationHelper helper = sheet.getDataValidationHelper(); + DataValidationConstraint constraint = helper.createCustomConstraint("DD1"); CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol); - DataValidation dataValidation = dvHelper.createValidation(constraint, regions); + DataValidation dataValidation = helper.createValidation(constraint, regions); dataValidation.createPromptBox(promptTitle, promptContent); dataValidation.setShowPromptBox(true); sheet.addValidationData(dataValidation); @@ -503,8 +477,7 @@ public class ExcelUtil * @param endCol 结束列 * @return 设置好的sheet. */ - public static Sheet setHSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, - int endCol) + public void setXSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) { DataValidationHelper helper = sheet.getDataValidationHelper(); // 加载下拉列表内容 @@ -525,7 +498,6 @@ public class ExcelUtil } sheet.addValidationData(dataValidation); - return sheet; } /** diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUser.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUser.java index 8d4084d5..fbb69b8f 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUser.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUser.java @@ -5,7 +5,6 @@ import com.ruoyi.common.annotation.Excel.Type; import com.ruoyi.common.base.BaseEntity; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; - import java.util.Date; import java.util.List; @@ -19,7 +18,7 @@ public class SysUser extends BaseEntity private static final long serialVersionUID = 1L; /** 用户ID */ - @Excel(name = "用户序号", prompt = "用户流水号") + @Excel(name = "用户序号", prompt = "用户编号") private Long userId; /** 部门ID */