数据库Excel导出操作代码过于冗长惨不忍睹,无法复用。
目录
依赖:
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.1.2</version>
</dependency>
注解配合工具类做了个小工具如下:
第一步:自定义注解:(读者请直接复制)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自定义导出Excel数据注解
*
* @author sunziwen
* @version 1.0
* @date 2018-12-29 15:00
**/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Excel {
/**
* 导出到Excel中的名字.
*/
public String name();
/**
* 日期格式, 如: yyyy-MM-dd
*/
public String dateFormat() default "";
/**
* 读取内容转表达式 (如: 0=男,1=女,2=未知)
*/
public String readConverterExp() default "";
/**
* 导出时在excel中每个列的高度 单位为字符
*/
public double height() default 14;
/**
* 导出时在excel中每个列的宽 单位为字符
*/
public double width() default 20;
/**
* 文字后缀,如% 90 变成90%
*/
public String suffix() default "";
/**
* 当值为空时,字段的默认值
*/
public String defaultValue() default "";
/**
* 提示信息
*/
public String prompt() default "";
/**
* 设置只能选择不能输入的列内容.
*/
public String[] combo() default {};
/**
* 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
*/
public boolean isExport() default true;
}
第二步:实体类:(为每个需要的字段打上@Excel注解)
import java.time.LocalDateTime;
import com.ciih.authcenter.client.util.excel.Excel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* (User)表实体类
*
* @author suzniwen
* @since 2021-04-13 16:11:55
*/
@SuppressWarnings("serial")
@EqualsAndHashCode(callSuper = true)
@Data
public class User {
@Excel(name = "编号")
@ApiModelProperty(value = "主键")
private String id;
@Excel(name = "账号")
@ApiModelProperty(value = "账号")
private String loginName;
@Excel(name = "用户名")
@ApiModelProperty(value = "用户名")
private String userName;
@ApiModelProperty(value = "用户名拼音")
private String namePinyin;
@Excel(name = "性别", readConverterExp = "1=男,0=女")
@ApiModelProperty(value = "性别")
private String gender;
@Excel(name = "证件类型",readConverterExp="1=居民身份证,2=香港居民来往内地通行证,3=澳门居民来往内地通行证,4=台湾居民来往大陆通行证,6=护照")
@ApiModelProperty(value = "证件类型")
private String credType;
@Excel(name = "证件号码")
@ApiModelProperty(value = "证件号码")
private String credNum;
@ApiModelProperty(value = "机构id")
private String orgId;
@Excel(name = "机构名称")
@ApiModelProperty(value = "机构名称")
private String orgName;
@Excel(name = "电话")
@ApiModelProperty(value = "电话")
private String phone;
@Excel(name = "邮箱")
@ApiModelProperty(value = "邮箱")
private String email;
@Excel(name = "人员类型",readConverterExp = "student=学生,teacher=教师,parent=家长,system=系统人员,developers=开发者,manager=管理员")
@ApiModelProperty(value = "人员类型")
private String personType;
@Excel(name = "应用系统角色编码")
@ApiModelProperty(value = "应用系统角色编码")
private String appRoleCode;
@ApiModelProperty(value = "创建时间")
private LocalDateTime createTime;
@ApiModelProperty(value = "更新时间")
private LocalDateTime updateTime;
}
第三步:解析工具类:(读者请直接复制)
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;