package org.springblade.common.utils;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class ExcelsUtil {

	/**
	 * excel导入
	 * @param file
	 * @param headerAlias
	 * @param beanType
	 * @param <T>
	 * @return
	 * @throws IOException
	 */
	public static <T> List<T> excelImport(MultipartFile file, Map<String, String> headerAlias, Class<T> beanType) throws IOException {
		try (InputStream inputStream = file.getInputStream()) {
			ExcelReader reader = ExcelUtil.getReader(inputStream);
			for (Map.Entry<String, String> item : headerAlias.entrySet()) {
				reader.addHeaderAlias(item.getKey(), item.getValue());
			}
			return reader.readAll(beanType);
		}
	}
}
