java-excel-从excel中读取数据

如果觉得写得可以 或者太差 就 评论一下或者赞一下呗,多谢支持!!


1.需要的jar包

     <!-- https://mvnrepository.com/artifact/org.apache.poi/poi excle 文件 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>

        </dependency>

2.代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 */
public class GetDataFromExcel {
    /** 总行数 */
    private int totalRows = 0;
    /** 总列数 */
    private int totalCells = 0;
    /** 构造方法 */
    public GetDataFromExcel(){}

    /**
     * 读取文件
     * @param fileName
     * @return
     */
    public List<ArrayList<String>> read(String fileName) {
        List<ArrayList<String>> dataLst = new ArrayList<ArrayList<String>>();
        /** 检查文件名是否为空或者是否是Excel格式的文件 */
        if (fileName == null || !fileName.matches("^.+\\.(?i)((xls)|(xlsx))$")) {
            return dataLst;
        }

        boolean isExcel2003 = true;
        /** 对文件的合法性进行验证 */
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }

        /** 检查文件是否存在 */
        File file = new File(fileName);
        if (file == null || !file.exists()) {
            return dataLst;
        }
        try {
            /** 调用本类提供的根据流读取的方法 */
            dataLst = read(new FileInputStream(file), isExcel2003);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        /** 返回最后读取的结果 */
        return dataLst;
    }

    /**
     * 根据流读取Excel文件
     * @param inputStream
     * @param isExcel2003
     * @return
     */
    public List<ArrayList<String>> read(InputStream inputStream, boolean isExcel2003) {
        List<ArrayList<String>> dataLst = null;
        try {
            /** 根据版本选择创建Workbook的方式 */
            Workbook wb = isExcel2003 ? new HSSFWorkbook(inputStream)
                    : new XSSFWorkbook(inputStream);
            dataLst = read(wb);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataLst;
    }

    /**
     * 得到总行数
     * @return
     */
    public int getTotalRows() {
        return totalRows;
    }
    /**
     * <li>Description:[得到总列数]</li>*
     * @return
     */
    public int getTotalCells() {
        return totalCells;
    }

    /**
     * 读取数据
     * @param wb
     * @return
     */
    private List<ArrayList<String>> read(Workbook wb) {
        List<ArrayList<String>> dataLst = new ArrayList<ArrayList<String>>();

        /** 得到第一个shell */
        Sheet sheet = wb.getSheetAt(0);
        this.totalRows = sheet.getPhysicalNumberOfRows();
        if (this.totalRows >= 1 && sheet.getRow(0) != null) {
            this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
        }

        /** 循环Excel的行 */
        for (int r = 0; r < this.totalRows; r++) {
            Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }

            ArrayList<String> rowLst = new ArrayList<String>();
            /** 循环Excel的列 */
            for (short c = 0; c < this.getTotalCells(); c++) {
                Cell cell = row.getCell(c);
                String cellValue = "";
                if (cell == null) {
                    rowLst.add(cellValue);
                    continue;
                }

                /** 处理数字型的,自动去零 */
                if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
                    /** 在excel里,日期也是数字,在此要进行判断 */
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                        cellValue = String.valueOf(DateUtil.getExcelDate(cell.getDateCellValue()));
                    }else{
                        cellValue = getRightStr(cell.getNumericCellValue() + "");
                    }
                }
                /** 处理字符串型 */
                else if (Cell.CELL_TYPE_STRING == cell.getCellType()){
                    cellValue = cell.getStringCellValue();
                }
                /** 处理布尔型 */
                else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
                    cellValue = cell.getBooleanCellValue() + "";
                }
                /** 其它的,非以上几种数据类型 */
                else {
                    cellValue = cell.toString() + "";
                }
                rowLst.add(cellValue);
            }
            dataLst.add(rowLst);
        }
        return dataLst;
    }

    /**
     * @param sNum
     * @return
     */
    private String getRightStr(String sNum) {
        DecimalFormat decimalFormat = new DecimalFormat("#.000000");
        String resultStr = decimalFormat.format(new Double(sNum));
        if (resultStr.matches("^[-+]?\\d+\\.[0]+$")) {
            resultStr = resultStr.substring(0, resultStr.indexOf("."));
        }
        return resultStr;
    }
  
    public static void main(String[] args) throws Exception {
        List<ArrayList<String>> dataLst = new GetDataFromExcel()
                .read("d:/123.xls");
        for (ArrayList<String> innerLst : dataLst) {
            StringBuffer rowData = new StringBuffer();
            for (String dataStr : innerLst) {
                rowData.append("-->").append(dataStr);
            }
            if (rowData.length() > 0) {
                System.out.println(rowData.deleteCharAt(0).toString());
            }
        }
    }
}

对于 以上 代码  我更喜欢  在项目里把它弄成一个工具类, 然后  给他传入 excel地址,  然后输出一个 list  来使用, 至于里面怎么实现的,没有特殊要求  可以不做过多关注. >.<


猜你喜欢

转载自blog.csdn.net/q18792880831/article/details/80587603