excel导入功能

excel导入功能,第1张

需要用到ExcelUtils工具类

package test.excel;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtils {

    private final static String excel2003L =".xls";    //2003- 版本的excel
    private final static String excel2007U =".xlsx";   //2007+ 版本的excel

    /**
     * @Description:获取IO流中的数据,组装成List>对象
     * @param in,fileName
     * @return
     * @throws IOException
     */
    public static List> getListByExcel(InputStream in, String fileName) throws Exception{
        List> list = null;

        //创建Excel工作薄
        Workbook work = getWorkbook(in,fileName);
        if(null == work){
            throw new Exception("创建Excel工作薄为空!");
        }
        Sheet sheet = null;  //页数
        Row row = null;  //行数
        Cell cell = null;  //列数

        list = new ArrayList>();
        //遍历Excel中所有的sheet
        for (int i = 0; i < work.getNumberOfSheets(); i++) {
            sheet = work.getSheetAt(i);
            if(sheet==null){continue;}

            //遍历当前sheet中的所有行
            for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                row = sheet.getRow(j);
                if(row==null){continue;}

                //遍历所有的列
                List li = new ArrayList();
                for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                    cell = row.getCell(y);
                    li.add(getValue(cell));
                }
                list.add(li);
            }
        }

        return list;

    }

    /**
     * @Description:根据文件后缀,自适应上传文件的版本
     * @param inStr,fileName
     * @return
     * @throws Exception
     */
    public static  Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{
        Workbook wb = null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        if(excel2003L.equals(fileType)){
            wb = new HSSFWorkbook(inStr);  //2003-
        }else if(excel2007U.equals(fileType)){
            wb = new XSSFWorkbook(inStr);  //2007+
        }else{
            throw new Exception("解析的文件格式有误!");
        }
        return wb;
    }

    /**
     * @Description:对表格中数值进行格式化
     * @param cell
     * @return
     */
    //解决excel类型问题,获得数值
    public static String getValue(Cell cell) {
        String value = "";
        if(null==cell){
            return value;
        }
        switch (cell.getCellType()) {
            //数值型
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    //如果是date类型则 ,获取该cell的date值
                    Date date = DateUtil.getJavaDate(cell.getNumericCellValue());
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    value = format.format(date);;
                }else {// 纯数字
                    BigDecimal big=new BigDecimal(cell.getNumericCellValue());
                    value = big.toString();
                    //解决1234.0  去掉后面的.0
                    if(null!=value&&!"".equals(value.trim())){
                        String[] item = value.split("[.]");
                        if(1 

ExcelUtils的主要作用是把Excel转化成 List>类型的数据,方便遍历

TestExcel

package test.excel;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.util.List;

public class TestExcel {

    @Test
    public void importExcel(){

        String filepath = "/Users/Desktop/sys_user.xlsx";
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(filepath));
            //把Excel文件转化成 List>类型的数据
            List> list = ExcelUtils.getListByExcel(inputStream, filepath);
            System.out.println(list);
            //定义第一行
            List firstRows = null;
            //数据不为空
            if(list != null && list.size() > 0){
                //拿到第一行
                firstRows = list.get(0);
            }
            //遍历每一行
            for (int i = 1; i < list.size(); i++) {
                //拿到这一行
                List rows = list.get(i);
                //初始化对象
                Demo demo = new Demo();
                //遍历这一行
                for (int j = 0; j < rows.size(); j++) {
                    //定义cellVal,拿到每一个的值
                    String cellVal = (String) rows.get(j);
                    //把对象,标题名,值 都传过去
                    TestExcel.setFieldValueByFieldName(demo, firstRows.get(j).toString().trim(), cellVal);
                }
                System.out.println(demo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @Test
//    public void testRef(){
//        tech.niua.admin.test.domain.Test test = new tech.niua.admin.test.domain.Test();
//        test.setId(100L);
//        test.setName("wangzhen");
//        Object id = TestExcel.getFieldValueByFieldName("id", test);
//        Object username = TestExcel.getFieldValueByFieldName("username", test);
//        System.out.println(username);
//    }

    private static void setFieldValueByFieldName(Object object, String fieldName, Object val) {
        try {
            //反射拿到所有的object的属性值
            Field[] fields = object.getClass().getDeclaredFields();
            //遍历一遍
            for (int i = 0; i < fields.length; i++) {
                //定义一个Field类型的值,遍历给它赋值
                Field field = fields[i];
                //如果传过来的fieldName等于field数据中的名称名
                if(fieldName.equals(field.getName())){
                    //把它set进去
                    field.set(object, val.toString());
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    private static Object getFieldValueByFieldName(String fieldName, Object object) {
        try {
            Field field = object.getClass().getField(fieldName);
            //设置对象的访问权限,保证对private的属性的访问
            return  field.get(object);
        } catch (Exception e) {
//            e.printStackTrace();
            return null;
        }
    }
}
 
 

主要步骤:

定义一个文件输入流,把excel文件放进输入流

调用ExcelUtils.getListByExcel(inputStream, filepath)  【这里把输入流和路径名当作参数,路径名当作参数是为了拿到文件名的后缀,判断xls还是xlsx,到ExcelUtils里判断版本】拿到excel文件转换成List>类型的数据

如果数据不为空的话,拿到数据中的第一行 list.get(0)  即标题名

for循环遍历每一行 list.get(i) ,在每一行初始化Demo对象(目的要给它赋值)

嵌套for循环遍历每一列rows.get(j),拿到每一个格的值

调用setFieldValueByFieldName方法,在一行内的每一次列遍历的时候都调用一次,而且把对应的标题名也当作参数传进去

每一行遍历结束,一个完整的Demo对象就被赋值成功了,就可以在业务中把它添加进数据库了

setFieldValueByFieldName方法

把对象,标题名,数值 传过去

通过反射拿到对象的属性值,遍历对象的属性值,如果属性名等于传过来的标题名,就可以对齐进行set,把数值set进对象里,做到了赋值

package test.excel;

import tech.niua.common.annotation.Excel;

/**
 * Demo实体类
 */
public class Demo {
    public String id;
    @Excel(name = "姓名")
    public String name;
    public String createTime;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "Demo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", createTime='" + createTime + '\'' +
                '}';
    }
}

 如果在实体类中添加注解

 

--改进--

因为通过上传获取服务器文件路径信息,把服务器文件路径返回到前端,再通过传递路径信息到后端对文件路径的文件进行 *** 作

有两个缺点:

1.两次请求,慢

2.客户端可以知道服务器的路径,不安全

点击上传之间上传成功

Controller层

 /**
     * Excel文件导入
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/imports")
    @PreAuthorize("hasAuthority('/signature/imports')")
    //传进来二进制文件数据
    public ResultJson uploadFile(MultipartFile file) throws Exception {
            //实例化实体类
            Signature signature =new Signature();
            //调用将文件转化成List的方法
            List objects = ObjectList.getObjectList(file,signature);
            //将List转化成List类型
            List list =  (List) (List)objects;
            //插入
            boolean flag = signatureService.saveOrUpdateBatch(list);
            if(flag){
                return ResultJson.ok();
            }
            return ResultJson.failure(ResultCode.NOT_UPDATE);

    } 
 

 封装的文件转List方法

package tech.niua.common.excelimport;

import org.springframework.web.multipart.MultipartFile;
import tech.niua.common.annotation.Excel;
import tech.niua.common.config.NiuaConfig;
import tech.niua.common.utils.file.FileUploadUtils;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class ObjectList {
    /**
     * 封装的将Excel文件信息转换成List的方法,需要传实体类
     * @param file
     * @param object
     * @return
     * @throws Exception
     */
    public static List getObjectList(MultipartFile file, Object object) throws Exception {
        Class cls = object.getClass();
        // 上传文件路径C:/Users/10565/Desktop/niuniu/upload
        String filePath = NiuaConfig.getUploadPath();
        // 上传并返回新文件名称 2022/04/28/3015c5d0-8e05-4e02-b1f6-81440f373b56.xlsx
        String fileName = FileUploadUtils.upload(filePath, file);
        //完整的上传文件的路径
        fileName =filePath+ File.separator+fileName;
        //把上传的文件转换成输入流
        FileInputStream fileInputStream = new FileInputStream(new File(fileName));
        //输入流 和 文件路径  作为参数传入 获取List>类型数据的方法中
        List> list = ExcelUtils.getListByExcel(fileInputStream,fileName);
        //将数据转换成List类型的数据
        List firstRows = null;
        if (list != null && list.size() > 0) {
            firstRows = list.get(0);
        }
        List excelDate = new ArrayList<>();
        for (int i = 1; i < list.size(); i++) {
            List rows = list.get(i);
            Object obj = cls.newInstance();
            for (int j = 0; j < rows.size(); j++) {
                String cellVal = (String) rows.get(j);
                ObjectList.setFieldValueByFieldName(obj, firstRows.get(j).toString().trim(), cellVal);
            }
            excelDate.add(obj);
        }
        return excelDate;
    }
    public static void setFieldValueByFieldName(Object object, String fieldName, Object val) {
        try {
            Field[] fields = object.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                //拿到当前实体类字段的Excel注解
                Excel excel = field.getAnnotation(Excel.class);
                if(fieldName.equals(excel.name())||fieldName.equals(field.getName())){
                    //把属性值set进对象
                    if(field.getType()==Integer.class){
                        field.set(object,Integer.valueOf(val.toString()));
                    } else if(field.getType()==Long.class){
                        field.set(object,Long.valueOf(val.toString()));
                    }else
                        field.set(object, val);
                    return;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

}
 
 
package tech.niua.common.excelimport;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * Created by sangyinghao
 * Date :2022/4/26
 * Description : excel导入工具类
 * Version :1.0
 */
public class ExcelUtils {

    private final static String excel2003L =".xls";    //2003- 版本的excel
    private final static String excel2007U =".xlsx";   //2007+ 版本的excel

    /**
     * @Description:获取IO流中的数据,组装成List>对象
     * @param in,fileName
     * @return
     * @throws IOException
     */
    public static List> getListByExcel(InputStream in, String fileName) throws Exception{
        List> list = null;

        //创建Excel工作薄
        Workbook work = getWorkbook(in,fileName);
        if(null == work){
            throw new Exception("创建Excel工作薄为空!");
        }
        Sheet sheet = null;  //页数
        Row row = null;  //行数
        Cell cell = null;  //列数

        list = new ArrayList>();
        //遍历Excel中所有的sheet
        for (int i = 0; i < work.getNumberOfSheets(); i++) {
            sheet = work.getSheetAt(i);
            if(sheet==null){continue;}

            //遍历当前sheet中的所有行
            for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                row = sheet.getRow(j);
                if(row==null){continue;}

                //遍历所有的列
                List li = new ArrayList();
                for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                    cell = row.getCell(y);
                    li.add(getValue(cell));
                }
                list.add(li);
            }
        }

        return list;

    }

    /**
     * @Description:根据文件后缀,自适应上传文件的版本
     * @param inStr,fileName
     * @return
     * @throws Exception
     */
    public static  Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{
        Workbook wb = null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        if(excel2003L.equals(fileType)){
            wb = new HSSFWorkbook(inStr);  //2003-
        }else if(excel2007U.equals(fileType)){
            wb = new XSSFWorkbook(inStr);  //2007+
        }else{
            throw new Exception("解析的文件格式有误!");
        }
        return wb;
    }

    /**
     * @Description:对表格中数值进行格式化
     * @param cell
     * @return
     */
    //解决excel类型问题,获得数值
    public static String getValue(Cell cell) {
        String value = "";
        if(null==cell){
            return value;
        }
        switch (cell.getCellType()) {
            //数值型
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    //如果是date类型则 ,获取该cell的date值
                    Date date = DateUtil.getJavaDate(cell.getNumericCellValue());
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    value = format.format(date);;
                }else {// 纯数字
                    BigDecimal big=new BigDecimal(cell.getNumericCellValue());
                    value = big.toString();
                    //解决1234.0  去掉后面的.0
                    if(null!=value&&!"".equals(value.trim())){
                        String[] item = value.split("[.]");
                        if(1 
 

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/756738.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-30
下一篇2022-04-30

发表评论

登录后才能评论

评论列表(0条)