java使用poi读取excel表格数据工具类

package com.eyric.excel;

import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import com.eyric.excel.dto.ExcelData;
import com.eyric.excel.dto.ExcelLineData;
import com.eyric.excel.dto.ExcelSheetData;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * 读取Excel数据工具类
 *
 * @time 2017-06-03
 */
public class ExcelReader {
    private static POIFSFileSystem fs;
    private static HSSFWorkbook wb;
    private static HSSFSheet sheet;
    private static HSSFRow row;

    /**
     * 获取多个sheetExcel表格数据
     *
     * @param fileName Excel 数据表格
     * @return
     */
    public ExcelData readMultiSheetExcel(String fileName) {
        InputStream is = null;
        File file = new File(fileName);
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        ExcelData excelData = new ExcelData();
        try {
            fs = new POIFSFileSystem(is);
            wb = new HSSFWorkbook(fs);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Integer sheetNum = wb.getNumberOfSheets();
        excelData.setSheetSum(sheetNum);
        excelData.setFileName(file.getName());

        //循环获取所有sheet数据
        List sheetDatas = new ArrayList<>();
        for (int i = 0; i < sheetNum; i++) {
            ExcelSheetData sheetData = new ExcelSheetData();
            sheet = wb.getSheetAt(i);
            sheetData.setLineSum(sheet.getPhysicalNumberOfRows());
            sheetData.setSheetName(sheet.getSheetName());

            List lineDatas = readExcelContentBySheet(sheet);
            sheetData.setLineData(lineDatas);
            sheetDatas.add(sheetData);
        }
        excelData.setSheetData(sheetDatas);
        return excelData;
    }


    private List readExcelContentBySheet(HSSFSheet sheet) {
        List lineDatas = new ArrayList<>();
        // 得到总行数
        int rowNum = sheet.getLastRowNum();
        for (int i = 0; i <= rowNum; i++) {
            int j = 0;
            row = sheet.getRow(i);
            if (Objects.isNull(row)) {
                continue;
            }

            int colNum = row.getPhysicalNumberOfCells();
            ExcelLineData lineData = new ExcelLineData();
            List colData = new ArrayList<>();
            lineData.setColSum(colNum);
            while (j < colNum) {
                String value = getCellValue(row.getCell((short) j)).trim();
                colData.add(value);
                j++;
            }
            lineData.setColData(colData);
            lineDatas.add(lineData);
        }

        return lineDatas;
    }


    /**
     * 获取单元格数据
     *
     * @param cell Excel单元格
     * @return String 单元格数据内容
     */
    private String getCellValue(HSSFCell cell) {
        if (Objects.isNull(cell)) {
            return "";
        }

        String value = "";
        switch (cell.getCellType()) {
            case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                //如果为时间格式的内容
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    //注:format格式 yyyy-MM-dd hh:mm:ss 中小时为12小时制,若要24小时制,则把小h变为H即可,yyyy-MM-dd HH:mm:ss
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();
                    break;
                } else {
                    value = new DecimalFormat("0").format(cell.getNumericCellValue());
                }
                break;
            case HSSFCell.CELL_TYPE_STRING: // 字符串
                value = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                value = cell.getBooleanCellValue() + "";
                break;
            case HSSFCell.CELL_TYPE_FORMULA: // 公式
                value = cell.getCellFormula() + "";
                break;
            case HSSFCell.CELL_TYPE_BLANK: // 空值
                value = "";
                break;
            case HSSFCell.CELL_TYPE_ERROR: // 故障
                value = "非法字符";
                break;
            default:
                value = "未知类型";
                break;
        }
        return value;
    }

    public static void main(String[] args) {
        ExcelReader excelReader = new ExcelReader();
        ExcelData excelData = excelReader.readMultiSheetExcel("D:\\P38.xls");
        System.out.println(excelData.toString());
      
    }

}
package com.eyric.excel.dto;

import lombok.Data;

import java.util.List;

/**
 * @Auther: lwt
 * @Date: 2018/7/31 16:02
 * @Since: 0.1.0
 * @Description:
 */
@Data
public class ExcelData {
    private int sheetSum;
    private String fileName;
    private List sheetData;
}
package com.eyric.excel.dto;

import lombok.Data;

import java.util.List;

/**
 * @Auther: lwt
 * @Date: 2018/7/31 15:52
 * @Since: 0.1.0
 * @Description:
 */
@Data
public class ExcelSheetData {
    /**
     * 工作簿名称
     */
    private String sheetName;
    /**
     * 表格总行数
     */
    private int lineSum;
    /**
     * 行数据集合
     */
    private List lineData;
}
package com.eyric.excel.dto;

import lombok.Data;

import java.util.List;

/**
 * @Auther: lwt
 * @Date: 2018/7/31 15:54
 * @Since: 0.1.0
 * @Description:
 */
@Data
public class ExcelLineData {
    /**
     * 行编号
     */
    private int lineNumber;
    /**
     * 行总列数
     */
    private int colSum;
    /**
     * 列数据集合
     */
    private List colData;
}

 

你可能感兴趣的:(java)