有时候我们不仅需要将excel文件中的数据导入到数据库,同时我们还需要将数据库中的数据或者表字段导出,接下来我们就具体看看如何进行数据库模板导出~
我记得需要导入easypoi的相关注解(如果没记错的话):
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
以下是我的实体类:
@Data
@TableName("data_standard_excel_dto")
@ApiOperation("导出数据标准excel")
@Component
public class exportExcelDto implements Serializable {
@ExcelProperty(value = "数据标准编号",index = 1)
// @TableField("data_standard_code")
private String dataStandardCode;
@ExcelProperty(value ="中文名",index = 2)
// @TableField("cn_value")
private String cnvalue;
@ExcelProperty(value ="英文名",index = 3)
// @TableField("en_value")
private String envalue;
@ExcelProperty(value ="标准说明",index = 4)
// @TableField("data_standard_explain")
private String dataStandardExplain;
@ExcelProperty(value ="来源机构",index = 5)
// @TableField("source_orgnization")
private String sourceOrgnization;
@ExcelProperty(value ="数据类型",index = 6)
// @TableField("data_standard_type")
private String dataStandardType;
@ExcelProperty(value ="数据长度",index = 7)
// @TableField("data_standard_length")
private Double dataStandardLength;
@ExcelProperty(value ="数据精度",index = 8)
// @TableField("data_standard_accuracy")
private Double dataStandardAccuracy;
@ExcelProperty(value ="默认值",index = 9)
// @TableField("data_standard_default")
private String dataStandardDefault;
@ExcelProperty(value ="取值范围的最大值",index = 10)
// @TableField("data_standard_value_max")
private String dataStandardvalueMax;
@ExcelProperty(value ="取值范围的最小值",index = 11)
// @TableField("data_standard_value_min")
private String dataStandardvalueMin;
@ExcelProperty(value ="引用码表编号",index = 12)
// @TableField("data_standard_enumeration_range")
private String dataStandardEnumerationRange;
@ExcelProperty(value ="标准状态(0:未发布 1:已发布 2:已停用)",index=13)
// @TableField("data_standard_status")
private Integer dataStandardStatus;
@ExcelProperty(value ="是否为空(0:可为空 1:不为空)",index = 14)
// @TableField("data_standard_is_blank")
private Integer dataStandardIsBlank;
}
工具类:
// 定义一个名为generateExcelHeader的方法,该方法接受一个类参数clazz
public static List<String> generateExcelHeader(Class<?> clazz){
// 创建一个名为headerList的List,用于存储Excel表头
List<String> headerList = new ArrayList<>();
// 获取类中所有声明的字段(不包括父类中的字段)
Field[] fields = clazz.getDeclaredFields();
// 遍历所有字段
for (Field field : fields) {
// 获取字段上名为ExcelProperty的注解
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
// 如果注解不为空,将注解的值添加到headerList中
if (excelProperty != null) {
headerList.add(Arrays.toString(excelProperty.value()));
}
}
// 返回headerList
return headerList;
}
service实现方法:
@Override
public HSSFWorkbook exportToExcel() throws IOException {
List<String> excelHeader=generateExcelHeader(exportExcelDto.class);
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet=wb.createSheet("数据标准管理模板");
HSSFRow row=sheet.createRow(0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
for (int i = 0; i < excelHeader.size(); i++) {
HSSFCell cell=row.createCell(i);
cell.setCellValue(excelHeader.get(i));
cell.setCellStyle(style);
sheet.setColumnWidth(cell.getColumnIndex(),100*50);
}
return wb;
}
成功后,需要前端进行接收,所以前端的代码我就爱莫能助了~
标签:index,TableField,数据库,ExcelProperty,Excel,value,standard,private,模板 From: https://blog.csdn.net/qq_71416673/article/details/136989934