POI上传Excel的小问题处理

package com.platform.utils.excel;

import com.platform.utils.RRException;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * Excel文件导入的基本功能类
 * 可导入EXCEL2003 和 EXCEL2007格式。
 *
 * @date 2017年10月28日 13:11:27
 */
public class ExcelImport {
    /**
     * excel2003扩展名
     */
    public static final String EXCEL03_EXTENSION = ".xls";

    /**
     * excel2007扩展名
     */
    public static final String EXCEL07_EXTENSION = ".xlsx";

    private ExcelImport() {
    }

    /**
     * 解析EXCEL数据为 List<String[]>
     *
     * @param excelFile 要解析的上传EXCEL文件
     * @return List<String[]) 行(列)
     */
    public static List<String[]> getExcelData07(MultipartFile excelFile) {
        List<String[]> resultList = null;

        if (null == excelFile || excelFile.isEmpty()) {
            throw new RRException("文件内容为空!");

        }

        Excel2007Reader excel07 = new Excel2007Reader();
        try {
            excel07.process(excelFile.getInputStream(), false);
        } catch (Exception e) {
            throw new RRException("excel解析失败!");
        }
        resultList = excel07.getSheetData(0);

        return resultList;
    }

    /**
     * 解析EXCEL数据为 List<String[]>
     *
     * @param excelFile 要解析的上传EXCEL文件
     * @return List<String[]) 行(列)
     */
    public static List<String[]> getExcelData03(MultipartFile excelFile) {
        List<String[]> resultList = null;

        if (null == excelFile || excelFile.isEmpty()) {
            throw new RRException("文件内容为空!");

        }

        Excel2003Reader excel03 = new Excel2003Reader();// 实例化excel处理对象
        try {
            excel03.process(excelFile.getInputStream());
        } catch (IOException e) {
            throw new RRException("excel解析失败!");
        }
        resultList = excel03.getSheetData(0);

        return resultList;
    }

    /**
     * 通过解析MultipartFile对象获取excel内容,并且将其拼装为List<String[]>对象返回
     *
     * @param excelFile
     * @return
     * @throws Exception
     */
    public static List<String[]> getExcelData(MultipartFile excelFile)
            throws RRException {
        List<String[]> resultList = null;

        if (!excelFile.isEmpty()) {// 上传的文件不能为空
            String excelFileName = excelFile.getOriginalFilename();// 文件名(带后缀)
            if (excelFileName.toLowerCase().endsWith(EXCEL03_EXTENSION)) {// 如果文件是以.xls为后缀
                Excel2003Reader excel03 = new Excel2003Reader();// 实例化excel处理对象
                try {
                    excel03.process(excelFile.getInputStream());
                } catch (IOException e) {
                    throw new RRException("excel解析失败!");
                }
                resultList = excel03.getSheetData(0);
            } else if (excelFileName.toLowerCase().endsWith(EXCEL07_EXTENSION)) {// 如果文件是以.xlsx为后缀
                Excel2007Reader excel07 = new Excel2007Reader();
                try {
                    excel07.process(excelFile.getInputStream(), false);
                } catch (Exception e) {
                    throw new RRException("excel解析失败!");
                }
                resultList = excel07.getSheetData(0);
            }
        }

        return resultList;
    }

    /**
     * 通过解析MultipartFile对象获取excel内容,并且将其拼装为Map<Integer, List<String[]>>对象返回
     *
     * @param excelFile
     * @return
     * @throws Exception
     */
    public static Map<Integer, List<String[]>> getExcelDataAll(MultipartFile excelFile)
            throws RRException {
        Map<Integer, List<String[]>> result = null;

        if (!excelFile.isEmpty()) {// 上传的文件不能为空
            String excelFileName = excelFile.getOriginalFilename();// 文件名(带后缀)
            if (excelFileName.toLowerCase().endsWith(EXCEL03_EXTENSION)) {// 如果文件是以.xls为后缀
                Excel2003Reader excel03 = new Excel2003Reader();// 实例化excel处理对象
                try {
                    excel03.process(excelFile.getInputStream());
                } catch (IOException e) {
                    throw new RRException("excel解析失败!");
                }
                result = excel03.getSheetData();
            } else if (excelFileName.toLowerCase().endsWith(EXCEL07_EXTENSION)) {// 如果文件是以.xlsx为后缀
                Excel2007Reader excel07 = new Excel2007Reader();
                try {
                    excel07.process(excelFile.getInputStream(), true);
                } catch (Exception e) {
                    throw new RRException("excel解析失败!");
                }
                result = excel07.getSheetData();
            }
        }

        return result;
    }
}
RRException 
package com.platform.utils;

/**
 * 自定义异常
 *
 * 
 * @date 2017年11月18日 下午13:13:23
 */
public class RRException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    private String msg;
    private int code = 500;

    public RRException(String msg) {
        super(msg);
        this.msg = msg;
    }

    public RRException(String msg, Throwable e) {
        super(msg, e);
        this.msg = msg;
    }

    public RRException(String msg, int code) {
        super(msg);
        this.msg = msg;
        this.code = code;
    }

    public RRException(String msg, int code, Throwable e) {
        super(msg, e);
        this.msg = msg;
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }


}

 

利用POI上传Excel 出现如果这一行前几个是空的  传到后台的list里面第一个数据是从有数据的列开始的  这样会造成取数据的时候报错

解决办法:

遍历接受的list,获取头的长度,如果两个长度不同,新定义一个长度和头信息相同的数组,遍历这个数组  将没有数据的在数组里面加上一个空的字符串,再讲数组放到一个list里面

调用set方法

set方法是将原来位置上的那个给取代了,并将原来位置上对象的返回。

 for(int i = 1;i<list.size();i++){
            if(list.get(i-1).length!=list.get(i).length){
                Integer count = list.get(0).length-list.get(i).length;
                String[] a = new String[count];
                for(int j=0;j<a.length;j++){
                    a[j]="";
                }
                List l = new ArrayList(Arrays.asList(list.get(i)));
                l.addAll(Arrays.asList(a));
                String[] arr = (String[]) l.toArray(new String[list.get(0).length]);
                list.set(i,arr);
            }

 

posted @ 2018-10-08 15:00  *眉间缘*  阅读(373)  评论(0编辑  收藏  举报