自定义注解实现Excel导入导出

本文档展示了如何使用自定义的@ExcelAnnotation注解来定义Excel模板的字段信息,包括字段名、排序、日期格式和数据类型。同时,提供了一个名为ExcelUtils的工具类,用于方便地导出和导入Excel数据。工具类实现了根据注解信息生成Excel,支持数据类型的自动转换,并能从Excel文件中读取数据并映射到对象。
摘要由CSDN通过智能技术生成

1 自定义@ExcelAnnotation注解

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

/**
 * 生成Excel模板时,需要有哪些字段名、字段标题、字段之间的排序、字段中内容的位置、对齐方式等信息
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelAnnotation {
    String headName();

    int order();

    String datePattern() default "yyyyMMdd HH:mm:ss";

    enum DataType {
        String, Number, Date,
    }

    /**
     * 数据类型,可以是String,Number(数字型),Date等类型
     *
     * @return
     */
    DataType type() default DataType.String;

}

2 ExcelUtils工具类

package com.grm.util;

import com.grm.annotation.ExcelAnnotation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.*;

/**
 * @author gaorimao
 */
public class ExcelUtils {

    private static ExcelUtils instance;

    private ExcelUtils() {
    }

    /**
     * 单例模式
     *
     * @return
     */
    public static ExcelUtils getInstance() {
        if (instance == null) {
            instance = new ExcelUtils();
        }
        return instance;
    }

    /**
     * excel的导出
     *
     * @param response
     * @param infos
     */
    public static void exportExcel(HttpServletResponse response, List<?> infos) {
        try {
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
            XSSFSheet sheet = xssfWorkbook.createSheet();
            sheet.createRow(0);

            Map<Field, Integer> map = new LinkedHashMap<>();
            for (Object o : infos) {
                Field[] fields = o.getClass().getDeclaredFields();
                for (Field field : fields) {
                    if (field.isAnnotationPresent(ExcelAnnotation.class)) {
                        ExcelAnnotation annotation = field.getAnnotation(ExcelAnnotation.class);
                        map.put(field, annotation.order());
                    }
                }
            }
            List<Map.Entry<Field, Integer>> list = new ArrayList<>(map.entrySet());
            Collections.sort(list, Comparator.comparing(Map.Entry::getValue));

            List<Field> excelFields = new ArrayList<>();
            for (Map.Entry<Field, Integer> map1 : list) {
                excelFields.add(map1.getKey());
            }

            List<ExcelAnnotation> annotations = new ArrayList<>();
            for (Field excelField : excelFields) {
                annotations.add(excelField.getAnnotation(ExcelAnnotation.class));
            }
            addDataToExcel(xssfWorkbook, infos, excelFields, annotations, sheet);
            xssfWorkbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static <T> void addDataToExcel(XSSFWorkbook wb, List<T> dataset, List<Field> excelFields, List<ExcelAnnotation> attributes,
                                    Sheet sheet)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException {
        XSSFCellStyle style = wb.createCellStyle();
        // 居中
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        // excel放入第一行列的名称
        Row row = sheet.createRow(0);
        for (int j = 0; j < excelFields.size(); j++) {
            Cell cell = row.createCell(j);
            ExcelAnnotation oneAttribute = attributes.get(j);
            cell.setCellValue(oneAttribute.headName());
            cell.setCellStyle(style);
        }
        // 添加数据到excel
        for (int i = 0; i < dataset.size(); i++) {
            // 数据行号从1开始,因为第0行放的是列的名称
            row = sheet.createRow(i + 1);
            for (int j = 0; j < attributes.size(); j++) {
                Cell cell = row.createCell(j);
                ExcelAnnotation annotation = attributes.get(j);
                style = wb.createCellStyle();
                // 居中
                style.setAlignment(HorizontalAlignment.CENTER);
                style.setVerticalAlignment(VerticalAlignment.CENTER);
                // 四个边框
                style.setBorderBottom(BorderStyle.THIN);
                style.setBorderLeft(BorderStyle.THIN);
                style.setBorderRight(BorderStyle.THIN);
                style.setBorderTop(BorderStyle.THIN);
                cell.setCellStyle(style);
                // 根据属性名获取属性值
                String cellValue = BeanUtils.getProperty(dataset.get(i), excelFields.get(j).getName());
                if (ExcelAnnotation.DataType.Date.equals(annotation.type())) {
                    String date = DateUtils
                            .dateStringToOtherDateString(cellValue, DateUtils.EXCEL_DATE_FORMAT, annotation.datePattern());
                    cell.setCellValue(date);
                } else {
                    cell.setCellValue(cellValue);
                }
            }
        }

    }

    /**
     * excel的导入
     *
     * @param file  file
     * @param clazz
     * @return
     * @throws IOException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    public static List<?> importExcel(MultipartFile file, Class<?> clazz)
            throws IOException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Workbook workbook = WorkbookFactory.create(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);
        Row titleCell = sheet.getRow(0);
        List<Object> dataList = new ArrayList<>(sheet.getLastRowNum());
        Object datum;
        Map<String, Field> fieldMap = getFieldMap(clazz);
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            datum = clazz.newInstance();
            int minCell = row.getFirstCellNum();
            int maxCell = row.getLastCellNum();
            for (int cellNum = minCell; cellNum <= maxCell; cellNum++) {
                Cell title = titleCell.getCell(cellNum);
                if (title == null) {
                    continue;
                }
                String tag = title.getStringCellValue();
                Field field = fieldMap.get(tag);
                if (field == null) {
                    continue;
                }
                Class<?> type = field.getType();
                Object value = null;
                Cell cell = row.getCell(cellNum);
                if (cell == null) {
                    continue;
                }
                if (type.equals(Date.class)) {
                    value = cell.getDateCellValue();
                    ExcelAnnotation annotation = field.getAnnotation(ExcelAnnotation.class);
                    String datePattern = annotation.datePattern();
                    Date date = DateUtils.dateStringToDate((String) value, DateUtils.DEFAULT_DATE_FORMAT);
                    PropertyUtils.setProperty(datum, field.getName(), date);
                } else if (type.equals(Byte.class)) {
                    Double numericCellValue = cell.getNumericCellValue();
                    value = numericCellValue.byteValue();
                    PropertyUtils.setProperty(datum, field.getName(), value);
                } else if (type.equals(Short.class)) {
                    Double numericCellValue = cell.getNumericCellValue();
                    value = numericCellValue.shortValue();
                    PropertyUtils.setProperty(datum, field.getName(), value);
                } else if (type.equals(Integer.class)) {
                    Double numericCellValue = cell.getNumericCellValue();
                    value = numericCellValue.intValue();
                    PropertyUtils.setProperty(datum, field.getName(), value);
                } else if (type.equals(Long.class)) {
                    Double numericCellValue = cell.getNumericCellValue();
                    value = numericCellValue.longValue();
                    PropertyUtils.setProperty(datum, field.getName(), value);
                } else if (type.equals(Float.class)) {
                    Double numericCellValue = cell.getNumericCellValue();
                    value = numericCellValue.floatValue();
                    PropertyUtils.setProperty(datum, field.getName(), value);
                } else if (type.equals(Double.class)) {
                    Double numericCellValue = cell.getNumericCellValue();
                    PropertyUtils.setProperty(datum, field.getName(), numericCellValue);
                } else {
                    value = cell.getStringCellValue();
                    PropertyUtils.setProperty(datum, field.getName(), value);
                }
            }
            dataList.add(datum);
        }
        return dataList;
    }

    /**
     * key :headName  val:该名称对应的字段
     *
     * @param clazz
     * @param <T>
     * @return
     */
    private static <T> Map<String, Field> getFieldMap(Class<T> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        Map<String, Field> fieldMap = new HashMap<>();
        for (Field field : fields) {
            if (field.isAnnotationPresent(ExcelAnnotation.class)) {
                ExcelAnnotation annotation = field.getAnnotation(ExcelAnnotation.class);
                fieldMap.put(annotation.headName(), field);
            }
        }
        return fieldMap;
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己封装的excel导出/导入,可以根据注解来导出excel.本项目一共有13个类,里面还包含了一个反射工具,一个编码工具,10分值了。下面是测试代码 public class Test { public static void main(String[] arg) throws FileNotFoundException, IOException{ testBean(); testMap(); } public static void testBean() throws FileNotFoundException, IOException{ List l = new ArrayList(); for(int i=0;i<100;i++){ l.add(new MyBean()); } //很轻松,只需要二句话就能导出excel BeanExport be = ExportExcel.BeanExport(MyBean.class); be.createBeanSheet("1月份", "1月份人员信息").addData(l); be.createBeanSheet("2月份","2月份人员信息").addData(l); be.writeFile("E:/test/bean人员信息8.xlsx"); } //如果不想用注解,还能根据MAP导出. public static void testMap () throws FileNotFoundException, IOException{ List l = new ArrayList(); l.add(new MapHeader("姓名","name",5000)); l.add(new MapHeader("年龄","age",4000)); l.add(new MapHeader("生日","birthdate",3000)); l.add(new MapHeader("地址","address",5000)); l.add(new MapHeader("双精度","d",4000)); l.add(new MapHeader("float","f",6000)); List<Map> lm = new ArrayList<Map>(); for(int i=0;i<100;i++){ Map map = new HashMap(); map.put("name","闪电球"); map.put("age",100); map.put("birthdate",new Date()); map.put("address","北京市广东省AAA号123楼!"); map.put("d",22.222d); map.put("f",295.22f); lm.add(map); } MapExport me = ExportExcel.mapExport(l); me.createMapSheel("1月份","广东省人员信息").addData(lm); me.createMapSheel("2月份", "北京市人员信息").addData(lm); me.writeFile("E:/test/map人员信息9.xlsx"); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值