java通过注解顺序通过映射导出excel
阅读原文时间:2023年07月09日阅读:3

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelAttribute {
/** 对应的列名称 */
String name() default "";

/\*\* 列序号 \*/  
int sort();

/\*\* 字段类型对应的格式 \*/  
String format() default "";

}

导出的类

package com.ihrm.common.poi.utils;

import com.ihrm.domain.poi.ExcelAttribute;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Getter
@Setter
public class ExcelExportUtil {

private int rowIndex;  
private int styleIndex;  
private String templatePath;  
private Class clazz;  
private  Field fields\[\];

public ExcelExportUtil(Class clazz,int rowIndex,int styleIndex) {  
    this.clazz = clazz;  
    this.rowIndex = rowIndex;  
    this.styleIndex = styleIndex;  
    fields = clazz.getDeclaredFields();  
}

/\*\*  
 \* 基于注解导出  
 \*/  
public void export(HttpServletResponse response,InputStream is, List<T> objs,String fileName) throws Exception {

    XSSFWorkbook workbook = new XSSFWorkbook(is);  
    Sheet sheet = workbook.getSheetAt(0);

    CellStyle\[\] styles = getTemplateStyles(sheet.getRow(styleIndex));

    AtomicInteger datasAi = new AtomicInteger(rowIndex);  
    for (T t : objs) {  
        Row row = sheet.createRow(datasAi.getAndIncrement());  
        for(int i=0;i<styles.length;i++) {  
            Cell cell = row.createCell(i);  
            cell.setCellStyle(styles\[i\]);  
            for (Field field : fields) {  
                if(field.isAnnotationPresent(ExcelAttribute.class)){  
                    field.setAccessible(true);  
                    ExcelAttribute ea = field.getAnnotation(ExcelAttribute.class);  
                    if(i == ea.sort()) {  
                        cell.setCellValue(field.get(t).toString());  
                    }  
                }  
            }  
        }  
    }  
    fileName = URLEncoder.encode(fileName, "UTF-8");  
    response.setContentType("application/octet-stream");  
    response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("ISO8859-1")));  
    response.setHeader("filename", fileName);  
    workbook.write(response.getOutputStream());  
}

public CellStyle\[\] getTemplateStyles(Row row) {  
    CellStyle \[\] styles = new CellStyle\[row.getLastCellNum()\];  
    for(int i=0;i<row.getLastCellNum();i++) {  
        styles\[i\] = row.getCell(i).getCellStyle();  
    }  
    return styles;  
}  

}

model的例子

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;

//转正申请
@Entity
@Table(name = "em_positive")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmployeePositive implements Serializable {
private static final long serialVersionUID = 2391824518947910773L;
/**
* 员工ID
*/
@Id
private String userId;
/**
* 转正日期
*/
@ExcelAttribute(sort=1)
private Date dateOfCorrection;
/**
* 转正评价
*/
@ExcelAttribute(sort=2)
private String correctionEvaluation;
/**
* 附件
*/
private String enclosure;
/**
* 单据状态 1是未执行,2是已执行
*/
private Integer estatus;
/**
* 创建时间
*/
private Date createTime;
}

来源于传智播客