读取Excel工具类

时间:2019-10-23
本文章向大家介绍读取Excel工具类,主要包括读取Excel工具类使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

由于项目中要从前端导入excel,并将excel中多sheet页的数据导入到数据中,所以写了一个读取excel的工具类。

import com.cykj.reports.common.MyException;
import lombok.extern.slf4j.Slf4j;
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;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 读取Excel工具类
 * 1.根据文件路径读取excel所有sheet页的标题行或数据,返回Map集合
 * 2.上传excel文件读取excel所有sheet页的标题行或数据,返回Map集合
 *
 * @author ligong.zhang
 */
@Slf4j
public class ReadExcelUtils {
//    private Logger logger = LoggerFactory.getLogger(ReadExcelUtils.class);
    private Workbook wb;
    private Sheet sheet;
    private Row row;

    /**
     * 构造方法
     * 1.1根据文件路径初始化workboot
     * @param filepath
     */
    public ReadExcelUtils(String filepath) {
        if(filepath==null || filepath==""){
            return;
        }
        String ext = filepath.substring(filepath.lastIndexOf("."));
        try {
            InputStream is = new FileInputStream(filepath);
            if(".xls".equals(ext)){
                wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(ext)){
                wb = new XSSFWorkbook(is);
            }else{
                wb=null;
            }
        } catch (FileNotFoundException e) {
            log.error("FileNotFoundException", e);
            throw new MyException("文件没有发现异常...");
        } catch (IOException e) {
            log.error("IOException", e);
            throw new MyException("io异常...");
        }
    }

    /**
     * 构造方法
     * 1.2根据上传的文件对象初始化workboot
     * @param multipartFile
     */
    public ReadExcelUtils(MultipartFile multipartFile) {
        if(multipartFile==null){
            return;
        }
        //判断文件是否是excel文件
        String fileName = multipartFile.getOriginalFilename();
        if (!is2003Excel(fileName) && !is2007Excel(fileName)) {
            log.error("上传文件格式不正确或未上传文件...");
            throw new MyException("上传文件格式不正确或未上传文件...");
        }

        try {
            InputStream is = multipartFile.getInputStream();
            //根据后缀,得到不同的Workbook子类,即HSSFWorkbook或XSSFWorkbook
            if (is2003Excel(fileName)) {
                wb = new HSSFWorkbook(is);
            } else if (is2007Excel(fileName)){
                wb = new XSSFWorkbook(is);
            } else {
                wb = null;
            }
        } catch (FileNotFoundException e) {
            log.error("FileNotFoundException", e);
        } catch (IOException e) {
            log.error("IOException", e);
        }
    }

    // 是否是2003的excel,返回true是2003
    public static boolean is2003Excel(String fileName) {
        return fileName.matches("^.+\\.(?i)(xls)$");    //分隔符在linux上是否有问题
    }
    // 是否是2007的excel,返回true是2007
    public static boolean is2007Excel(String fileName) {
        return fileName.matches("^.+\\.(?i)(xlsx)$");
    }


    /**
     * 2.1读取Excel所有sheet页的表头的内容
     *
     * @param
     * @return String 表头内容的数组
     * @author zlg
     */
    public Map<String,String[]> readExcelTitle() {
        if(wb==null){
            throw new MyException("Workbook对象为空!");
        }
        Map<String,String[]> titleMap = new HashMap<>();
        // sheet循环
        int sheetNum = sheetCirculation(wb);
        for (int i = 0; i < sheetNum; i++) {
            Sheet sheet = wb.getSheetAt(i);
            String sheetName = sheet.getSheetName();
            System.out.println(sheetName);
            //读取sheet的标题行
            String[] titles = readTitle(sheet);
            titleMap.put(sheetName,titles);
        }
        return titleMap;
    }

    //读取每个sheet页的标题行
    public String[] readTitle(Sheet sheet){
        row = sheet.getRow(0);  //标题行
        // 标题总列数
        int colNum = row.getPhysicalNumberOfCells();
        System.out.println("colNum:" + colNum);
        String[] title = new String[colNum];    //每个sheet页的标题列可能不同,数组长度可能不同
        for (int i = 0; i < colNum; i++) {
            // title[i] = getStringCellValue(row.getCell((short) i));
            title[i] = row.getCell(i).getCellFormula();
        }
        return title;
    }


    /**
     * 2.2读取Excel所有sheet页的数据内容
     *
     * @param
     * @return 1Map:key(sheet页名称)  2map:sheet页对象  3map:每个sheet页的行对象  object:每一列的值
     * @author zlg
     */
    public Map<String,Map<Integer, Map<Integer,String>>> readExcelContent() {
        if(wb==null){
            throw new MyException("Workbook对象为空!");
        }

        Map<String,Map<Integer, Map<Integer,String>>> dataMap = new HashMap<>();

        // sheet循环
        int sheetNum = sheetCirculation(wb);
        for (int i = 0; i < sheetNum; i++) {
            Sheet sheet = wb.getSheetAt(i);
            String sheetName = sheet.getSheetName();
//            System.out.println(sheetName);

            Map<Integer, Map<Integer, String>> content = readSheet(sheet);
            dataMap.put(sheetName,content);

        }

       return dataMap;
    }

    //读取每个sheet页的数据
    private Map<Integer, Map<Integer,String>> readSheet(Sheet sheet){
        //每读取一个sheet页,生成一个content
        Map<Integer, Map<Integer,String>> content = new HashMap<>();    //行的map集合

        // 得到总行数
        int rowNum = sheet.getLastRowNum();
        row = sheet.getRow(0);      //第一行
        int colNum = row.getPhysicalNumberOfCells();
        // 正文内容应该从第二行开始,第一行为表头的标题
        for (int i = 1; i <= rowNum; i++) {     //从第二行开始
            row = sheet.getRow(i);
            int j = 0;
            Map<Integer,String> cellValue = new HashMap<>();    //列的map集合
            while (j < colNum) {
                String obj = getCellFormatValue(row.getCell(j));
                cellValue.put(j, obj);
                j++;
            }
            content.put(i, cellValue);
        }
        return content;

    }

    //获取sheet页的总数量
    private int sheetCirculation(Workbook wb) {
        int sheetCount = -1;
        sheetCount = wb.getNumberOfSheets();
        return sheetCount;
    }

    /**
     * 3.根据Cell类型设置数据
     *
     * @param cell
     * @return
     * @author zlg
     */
    private String getCellFormatValue(Cell cell) {
        String cellValue = "";
        if (null != cell) {
            // 以下是判断数据的类型
            switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                    cellValue = cell.getNumericCellValue() + "";
                    break;

                case HSSFCell.CELL_TYPE_STRING: // 字符串
                    cellValue = cell.getStringCellValue();
                    cellValue = cellValue.trim();   // 去掉字符串前后空格
                    cellValue = cellValue.replaceAll("\r\n|\r|\n", ""); // 去掉所有换行和回车
                    cellValue = cellValue.replaceAll("\'","&acute;");   // 替换单引号
                    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;
            }
        }

        /*Object cellvalue = "";
        if (cell != null) {
            // 判断当前Cell的Type
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:// 如果当前Cell的Type为NUMERIC
                case Cell.CELL_TYPE_FORMULA: {
                    // 判断当前的cell是否为Date
                    if (DateUtil.isCellDateFormatted(cell)) {
                        // 如果是Date类型则,转化为Data格式
                        // data格式是带时分秒的:2013-7-10 0:00:00
                        // cellvalue = cell.getDateCellValue().toLocaleString();
                        // data格式是不带带时分秒的:2013-7-10
                        Date date = cell.getDateCellValue();
                        cellvalue = date;
                    } else {// 如果是纯数字

                        // 取得当前Cell的数值
//                        cellvalue = String.valueOf(cell.getNumericCellValue());
                        cellvalue = cell.getRichStringCellValue().getString();
                    }
                    break;
                }
                case Cell.CELL_TYPE_STRING:// 如果当前Cell的Type为STRING
                    // 取得当前的Cell字符串
                    cellvalue = cell.getRichStringCellValue().getString();
                    break;
                default:// 默认的Cell值
                    cellvalue = "";
            }
        } else {
            cellvalue = "";
        }*/
        return cellValue;
    }


    //用main方法进行测试
    public static void main(String[] args) {
        try {
            String filepath = "C:/Users/ligong.zhang/Desktop/文档/GMS需求开发/excel/GMS waived cases.xlsx";
            ReadExcelUtils excelReader = new ReadExcelUtils(filepath);
            // 对读取Excel表格标题测试
//          String[] title = excelReader.readExcelTitle();
//          System.out.println("获得Excel表格的标题:");
//          for (String s : title) {
//              System.out.print(s + " ");
//          }

            // 对读取Excel表格内容测试
            Map<String,Map<Integer, Map<Integer,String>>> map = excelReader.readExcelContent();
            System.out.println("获得Excel表格的内容:");
            for(Map.Entry<String,Map<Integer, Map<Integer,String>>> entry:map.entrySet()){
                String key = entry.getKey();
                System.out.println(key+":"+entry.getValue());
                System.out.println("=========================================");

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

原文地址:https://www.cnblogs.com/itzlg/p/11728525.html