zoukankan      html  css  js  c++  java
  • excel工具类

    pom.xml
    <!--利用poi 读取excel-->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.16</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.16</version>
            </dependency>
    
    
            <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.4</version>
            </dependency>


    ExcelUtils.java
    package com.kakaluote.common.utils;
    
    import org.apache.commons.io.IOUtils;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.ss.util.CellRangeAddress;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.springframework.util.StringUtils;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by 敲代码的卡卡罗特
     * on 2018/11/19 22:12.
     */
    public class ExcelUtils {
        //思路是按照Workbook,Sheet,Row,Cell一层一层往下读取。首先是初始化Workbook
        public static Workbook getReadWorkBookType(String filePath) throws Exception{
            //xls-2003, xlsx-2007
            FileInputStream is = null;
    
            try {
                is = new FileInputStream(filePath);
                if (filePath.toLowerCase().endsWith("xlsx")) {
                    return new XSSFWorkbook(is);
                } else if (filePath.toLowerCase().endsWith("xls")) {
                    return new HSSFWorkbook(is);
                } else {
                    //  抛出自定义的业务异常
                }
            } catch (IOException e) {
            } finally {
                IOUtils.closeQuietly(is);
            }
            return null;
        }
    
        public static List<String> readExcel(String sourceFilePath) throws Exception {
            Workbook workbook = null;
    
            try {
                workbook = getReadWorkBookType(sourceFilePath);
                List<String> contents = new ArrayList<>();
    
                //获取第一个sheet
                Sheet sheet = workbook.getSheetAt(0);
                //第0行是表名,忽略,从第二行开始读取
                System.out.println(sheet.getLastRowNum());
                for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                    Row row = sheet.getRow(rowNum);
                    Cell cell=null;
                    short lastCellNum = row.getLastCellNum();
                    for (int j=0;j<lastCellNum;j++){
                        cell = row.getCell(j);
                        System.out.println(getCellVal(cell).trim());
                    }
    
                    contents.add(getCellVal(cell).trim());
                }
                return contents;
            }catch (Exception e){
                System.out.println(e.getMessage());
            }
            return null;
        }
        /**
         * 获取单元格的值
         *
         * @param cell
         * @return
         */
        //如果excel中的数据是数字,会发现java中对应的变成了科学计数法的,所以在获取值的时候就要做一些特殊处理,这样就能保证获取的值是我想要的值。
        public static String getCellVal(Cell cell) {
            CellType cellType = cell.getCellTypeEnum();
            //System.out.println(cell.getStringCellValue());
            switch (cellType) {
                case NUMERIC:
                    return cell.getNumericCellValue()+"";
                case STRING:
                    return cell.getStringCellValue();
                case BOOLEAN:
                    return String.valueOf(cell.getBooleanCellValue());
                case FORMULA:
                    return cell.getCellFormula();
                case BLANK:
                    return "";
                case ERROR:
                    return String.valueOf(cell.getErrorCellValue());
                default:
                    return "";
            }
        }
        /**
         * 判断是不是空白行  false表示是空白行,true表示不是空白行
         * @param row
         * @return
         */
        public static boolean isBlankLine(Row row){
            boolean flag=true;
            int i=0;
            if (row.getLastCellNum()==-1){
                flag=false;
                return flag;
            }
            for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
                Cell cell = row.getCell(c);
                if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK){
                    i++;
                    if (i==row.getLastCellNum()){
                        flag=false;
                        return flag;
                    }
    
                }
            }
            return flag;
        }
    
        /**
         * 判断指定的单元格是否是合并单元格
         *
         * @param sheet
         *            工作表
         * @param row
         *            行下标
         * @param column
         *            列下标
         * @return
         */
        public static boolean isMergedRegion(Sheet sheet, int row, int column) {
            int sheetMergeCount = sheet.getNumMergedRegions();
            for (int i = 0; i < sheetMergeCount; i++) {
                CellRangeAddress range = sheet.getMergedRegion(i);
                int firstColumn = range.getFirstColumn();
                int lastColumn = range.getLastColumn();
                int firstRow = range.getFirstRow();
                int lastRow = range.getLastRow();
                if (row >= firstRow && row <= lastRow) {
                    if (column >= firstColumn && column <= lastColumn) {
                        return true;
                    }
                }
            }
            return false;
        }
    
        /**
         * 读合并单元格值
         * @param sheet 表格
         * @param cell cell
         * @return value
         */
        public static String getMergedCellValue(Sheet sheet, Cell cell){
            int firstC;
            int lastC;
            int firstR;
            int lastR;
            String value = "";
            List<CellRangeAddress> listCombineCell = sheet.getMergedRegions();
            for (CellRangeAddress ca : listCombineCell) {
                // 获得合并单元格的起始行, 结束行, 起始列, 结束列
                firstC = ca.getFirstColumn();
                lastC = ca.getLastColumn();
                firstR = ca.getFirstRow();
                lastR = ca.getLastRow();
                if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) {
                    if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) {
                        // 获取合并单元格左上角单元格值
                        Row fRow = sheet.getRow(firstR);
                        Cell fCell = fRow.getCell(firstC);
                        value = getCellVal(fCell);
                    }
                }
            }
            return value;
        }
        
        public static void main(String[] arg){
    //        String path = "C:\Users\Administrator\Desktop\1.xlsx";
    //        try {
    //            List<String> tenantIds = readExcel(path);
    ////            for (String s:tenantIds){
    ////                System.out.println(s);
    ////            }
    //        } catch (Exception e) {
    //
    //        }
            String s="0.0";
            System.out.println("0.0".equals(s));
    
    
        }
    }
  • 相关阅读:
    RSA解密时BadPaddingException解决方法
    CAP定理的理解
    服务降级、熔断、隔离
    javascript原型 原型链
    Redis基础数据类型及应用场景
    log4j
    个人免签支付
    Spring Security Oauth2 认证
    elementUI的栅格布局
    用 Vue+ElementUI 搭建后台管理极简模板
  • 原文地址:https://www.cnblogs.com/coder-lzh/p/10030544.html
Copyright © 2011-2022 走看看