JAVA读取EXCEL_自动生成实体类


代码实现
PropertyAnno.java

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

@Target(ElementType.FIELD)//规定用于全局变量
@Retention(RetentionPolicy.RUNTIME)//定义为运行时注解
/**
 * excel读取所用注解
 * @author Administrator
 *
 */
public @interface PropertyAnno {
	
	String value() default "";
	
}

  AnnoCreator.java

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
 * 注解解释工具类
 * @author Administrator
 *
 */
public class AnnoCreator {
	
	/**
	 * 注解解释方法
	 * @return Map<属性名(String), 注解名(String)>
	 */
	public static Map<String, String> annoCreator(Class<?> clazz) {
		Map<String, String> map = Collections.synchronizedMap(new HashMap<>());
		for (Field field : clazz.getDeclaredFields()) {
			//获取属性上所有注解
			Annotation[] annotations = field.getAnnotations();
			//判断属性上是否存在注解
			if(annotations.length < 1) {
				continue;
			}
			//判断属性上是否存在需要注解
			if(annotations[0] instanceof PropertyAnno) {
				map.put(field.getName(), ((PropertyAnno)annotations[0]).value());
			}
		}
		return map;
	}

}

  ReadExcel

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcel<T> {

    /**
     * 错误信息
     */
    private String errorMsg;

    /**
     * 表格数据对象
     */
    private Class<T> clazz;

    /**
     * 改写初始化方法,将对象传入T
     */
    @SuppressWarnings("unchecked")
    public ReadExcel() {
        @SuppressWarnings("rawtypes")
        Class clazz = getClass();
        while (clazz != Object.class) {
            Type t = clazz.getGenericSuperclass();
            if (t instanceof ParameterizedType) {
                Type[] args = ((ParameterizedType) t).getActualTypeArguments();
                if (args[0] instanceof Class) {
                    this.clazz = (Class<T>) args[0];
                    break;
                }
            }
            clazz = clazz.getSuperclass();
        }
    }

    /**
     * 根据文件名读取Excel文件
     * 
     * @param filePath 文件完整路径
     * @return Map<表名(sheet名,String), 表中的属性(List<T>)>
     */
    public Map<String, List<T>> read(String filePath) {
        Map<String, List<T>> map = null;
        InputStream is = null;
        try {
            // 验证文件是否正确
            if (!validateExcel(filePath)) {
                System.out.println(errorMsg);
                return null;
            }
            // 判断文件的类型,是2003还是2007
            boolean isExcel2003 = true;
            if (isExcel2007(filePath)) {
                isExcel2003 = false;
            }
            // 创建流进行读取
            File file = new File(filePath);
            is = new FileInputStream(file);
            // 根据版本选择创建Workbook的方式
            Workbook workbook = null;
            if (isExcel2003) {
                workbook = new HSSFWorkbook(is);
            } else {
                workbook = new XSSFWorkbook(is);
            }
            map = readExcel(workbook);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    // 关闭连接
                    is.close();
                } catch (IOException e) {
                    is = null;
                    e.printStackTrace();
                }
            }
        }
        return map;
    }

    /**
     * 读取Excel表格
     */
    @SuppressWarnings("deprecation")
    private Map<String, List<T>> readExcel(Workbook workbook) {
        Map<String, List<T>> map = Collections.synchronizedMap(new HashMap<>());
        List<T> list = null;
        // 获取表格数量
        int numberOfSheets = workbook.getNumberOfSheets();

        for (int sheetNum = 0; sheetNum < numberOfSheets; sheetNum++) {
            list = new ArrayList<>();
            // 获取注解
            Map<String, String> creatorMap = AnnoCreator.annoCreator(clazz);
            // 获取指定的Excel
            Sheet sheet = workbook.getSheetAt(sheetNum);
            // 获取Excel的行数
            int totalRows = sheet.getPhysicalNumberOfRows();
            // Excel的列数
            int totalCells = 0;
            if (totalRows >= 1 && sheet.getRow(0) != null) {
                // 获取Excel的列数
                totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
            }
            List<String> li = new ArrayList<>();
            // 循环Excel的行
            for (int r = 0; r < totalRows; r++) {
                // 获取对象
                T t = null;
                try {
                    t = clazz.newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    e.printStackTrace();
                }
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                // 遍历Excel的列
                for (int c = 0; c < totalCells; c++) {
                    Cell cell = row.getCell(c);
                    String cellValue = "";
                    if (null != cell) {
                        // 判断数据的类型
                        switch (cell.getCellType()) {
                        case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                            DecimalFormat df = new DecimalFormat("0");
                            cellValue = df.format(cell.getNumericCellValue());
                            break;
                        case HSSFCell.CELL_TYPE_STRING: // 字符串
                            cellValue = cell.getStringCellValue();
                            break;
                        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                            cellValue = cell.getBooleanCellValue() + "";
                            break;
                        case HSSFCell.CELL_TYPE_FORMULA: // 公式
                            cellValue = cell.getCellFormula() + "";
                            break;
                        case HSSFCell.CELL_TYPE_BLANK: // 空值
                            cellValue = "";
                            break;
                        case HSSFCell.CELL_TYPE_ERROR: // 故障
                            cellValue = "非法字符";
                            break;
                        default:
                            cellValue = "未知类型";
                            break;
                        }
                    }
                    //此处可修改为自己的表格样式,例如打印第二行第二列的数据:
                    /* if(r == 1 && c == 1){
                        System.out.println(cellValue);
                    } */
                    if (r == 0) {
                        // 存储第一行数据(列名)
                        li.add(cellValue);
                    } else {
                        // 存储第一行后数据
                        fill(creatorMap, li.get(c), cellValue, t);
                    }
                }
                if (r != 0) {
                    list.add(t);
                }
            }
            map.put(sheet.getSheetName(), list);
        }
        return map;

    }
    
    /**
     * 判断是否Excel格式是否为2003
     */
    private static boolean isExcel2003(String filePath) {
        return filePath.matches("^.+\\.(?i)(xls)$");
    }

    /**
     * 判断是否Excel格式是否为2007
     */
    private static boolean isExcel2007(String filePath) {
        return filePath.matches("^.+\\.(?i)(xlsx)$");
    }

    /**
     * 验证Excel文件
     */
    private boolean validateExcel(String filePath) {

        // 检查文件名格式是否正确
        if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
            System.out.println("文件名不是Excel格式");
            return false;
        }
        // 检查文件是否存在
        File file = new File(filePath);
        if (file == null || !file.exists()) {
            System.out.println("文件不存在");
            return false;
        }
        return true;
    }

    /**
     * 为对象进行赋值
     * 
     * @param map 注解map(注解=属性名)
     * @param str 当前读取到的列名
     * @param obj 当前读到的数据
     * @param t   需要进行赋值的对象
     */
    private void fill(Map<String, String> map, String str, String obj, T t) {
        for (Entry<String, String> entry : map.entrySet()) {
            try {
                if (str.equals(entry.getValue())) {
                    // 为对象进行赋值
                    Field f = clazz.getDeclaredField(entry.getKey().trim());
                    f.setAccessible(true);
                    f.set(t, obj);
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

  BasicsEntity.java

/**
 * excel内容实体类(示例)
 * 注意:实体类属性需定义为String类型
 * @author Administrator
 *
 */
public class BasicsEntity {
    
    @PropertyAnno("列名")//被注解字段与excel中列名对应
    private String name;//字段名
    
    @PropertyAnno("类型")
    private String type;//字段类型
    
    @PropertyAnno("长度")
    private String length;//字段长度
    
    @PropertyAnno("小数点长度")
    private String decimal;//小数点长度
    
    @PropertyAnno("默认值")
    private String defaults;//默认值

    @PropertyAnno("备注")
    private String annotation;//备注
    
    @PropertyAnno("是否为非空(Y或N)")
    private String isNull;//是否为非空(Y或N)
    
    @PropertyAnno("是否为主键(Y或N)")
    private String isPrincipal;//是否为主键(Y或N)

    @PropertyAnno("是否启用主键自增(Y或N)")
    private String isProgressive;//是否启用主键自增(Y或N)

    @Override
    public String toString() {
        return "BasicsEntity [name=" + name + ", type=" + type + ", length=" + length + ", decimal=" + decimal
                + ", defaults=" + defaults + ", annotation=" + annotation + ", isNull=" + isNull + ", isPrincipal="
                + isPrincipal + ", isProgressive=" + isProgressive + "]";
    }
}

  ReadTest.java

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.myutils.entity.BasicsEntity;
/**
 * 测试类
 * @author Administrator
 */
public class ReadTest {
    
    public static void main(String[] args) {
        //调用方法时必须写成以下格式,否则反射无法获取到实体类
        ReadExcel<BasicsEntity> util = new ReadExcel<BasicsEntity>(){};
        //调用方法,返回实体类集合
        Map<String, List<BasicsEntity>> map = util.read("D:数据库文档\\测试表格.xlsx");
        for (Entry<String, List<BasicsEntity>> baMap : map.entrySet()) {
            System.out.println("表名:" + baMap.getKey().split("\\|")[0]);
            System.out.println("表备注:" + baMap.getKey().split("\\|")[1]);
            for (BasicsEntity basicsEntity : baMap.getValue()) {
                System.out.println(basicsEntity.toString());
            }
        }
    }
}

  

pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cn</groupId>
  <artifactId>myExcel</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
     <dependency>
	    <groupId>org.apache.directory.studio</groupId>
	    <artifactId>org.apache.commons.codec</artifactId>
	    <version>1.8</version>
	</dependency>
	 <dependency>
		<groupId>net.sourceforge.jexcelapi</groupId>
		<artifactId>jxl</artifactId>
		<version>2.6.12</version>
	</dependency>
	<dependency>
		<groupId>org.apache.poi</groupId>
		<artifactId>poi-ooxml</artifactId>
		<version>3.9</version>
	</dependency>
<dependency>
   <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.54</version>
</dependency>

</dependencies>
</project>

  

测试使用表格:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM