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));
    
    
        }
    }
  • 相关阅读:
    DB2 for Z/os Statement prepare
    Foreign key (referential) constraints on DB2 LUW v105
    复制Informational constraints on LUW DB2 v105
    DB2 SQL Mixed data in character strings
    DB2 create partitioned table
    MVC中使用EF的技巧集(一)
    Asp.Net MVC 开发技巧(二)
    Linq使用技巧及查询示例(一)
    Asp.Net MVC 开发技巧(一)
    Asp.Net MVC Identity 2.2.1 使用技巧(八)
  • 原文地址:https://www.cnblogs.com/coder-lzh/p/10030544.html
Copyright © 2011-2022 走看看