java excel合并单元格读取

转自  https://blog.csdn.net/qq_21454973/article/details/80581459 并稍微改动60行 读取cell内容。

文件: 

输出: 

厂家1_1000000_自然人11_196493_
厂家1_1000000_自然人12_164194_
厂家1_1000000_自然人13_17269_
厂家1_1000000_自然人14_56635_
厂家1_1000000_自然人15_565406_
厂家2_2000000_自然人21_483501_
厂家2_2000000_自然人22_621527_
厂家2_2000000_自然人23_415647_
厂家2_2000000_自然人24_54321_
厂家2_2000000_自然人25_425002_

pom.xml

<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.15</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.15</version>
</dependency>

代码:

package com.navitek.utils;



import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;

/**
 * @Author: syl
 * @Date: 2019/7/3 0003 16:39
 * @Description:
 */

public class ExcelUtils {

    public static void main(String[] args) {
        getAllByExcel("E:\\all_temp\\temp.xls");

    }
    public static void getAllByExcel(String filepath) {
        try {
            // 同时支持Excel 2003、2007
            File excelFile = new File(filepath); // 创建文件对象
            FileInputStream is = new FileInputStream(excelFile); // 文件流
            Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel

            String[] res = readExcel(workbook, 0, 1, 0);
            for (int i = 0; i < res.length; i++) {
                System.out.println(res[i]);
            }

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

    }

    private static String[] readExcel(Workbook wb, int sheetIndex, int startReadLine, int tailLine) {
        Sheet sheet = wb.getSheetAt(sheetIndex);
        Row row = null;
        String[] res = new String[sheet.getLastRowNum() - tailLine + 1];

        for (int i = startReadLine; i < sheet.getLastRowNum() - tailLine + 1; i++) {

            row = sheet.getRow(i);
            res[i] = "";
            for (Cell c : row) {
                boolean isMerge = isMergedRegion(sheet, i, c.getColumnIndex());
                // 判断是否具有合并单元格
                if (isMerge) {
                    String rs = getMergedRegionValue(sheet, row.getRowNum(), c.getColumnIndex());
                    //System.out.print(rs + "_"+ row.getRowNum()+"_"+c.getColumnIndex() +"_");
                    res[i] += rs+ "_";
                } else {
                    //System.out.print(c.getRichStringCellValue() + "");
                    res[i] += getCellValue(c)+ "_";
                }
            }
            //System.out.println();
        }

        if(startReadLine > 0){
            String[] result = new String[res.length - startReadLine];
            for (int i = 0; i < startReadLine; i++) {
                for (int j = 0; j < res.length; j++) {
                    if(j == res.length - 1)
                        continue;
                    res[j] = res[j+1];
                }
            }
            for (int i = 0; i < result.length; i++) {
                result[i] = res[i];
            }
            return result;
        }else{
            return res;
        }
    }

    private 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;
    }

    public static String getMergedRegionValue(Sheet sheet, int row, int column) {
        int sheetMergeCount = sheet.getNumMergedRegions();

        for (int i = 0; i < sheetMergeCount; i++) {
            CellRangeAddress ca = sheet.getMergedRegion(i);
            int firstColumn = ca.getFirstColumn();
            int lastColumn = ca.getLastColumn();
            int firstRow = ca.getFirstRow();
            int lastRow = ca.getLastRow();

            if (row >= firstRow && row <= lastRow) {

                if (column >= firstColumn && column <= lastColumn) {
                    Row fRow = sheet.getRow(firstRow);
                    Cell fCell = fRow.getCell(firstColumn);
                    return getCellValue(fCell);
                }
            }
        }

        return null;
    }

    private static String getCellValue(Cell cell) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String cellValue = "";
        int cellType = cell.getCellType();
        switch (cellType) {
            case Cell.CELL_TYPE_STRING: // 文本
                cellValue = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_NUMERIC: // 数字、日期
                if (DateUtil.isCellDateFormatted(cell)) {
                    cellValue = fmt.format(cell.getDateCellValue()); // 日期型
                } else {
                    cellValue = String.valueOf((int) cell.getNumericCellValue()); // 数字
                }
                break;
            case Cell.CELL_TYPE_BOOLEAN: // 布尔型
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_BLANK: // 空白
                cellValue = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_ERROR: // 错误
                cellValue = "错误";
                break;
            case Cell.CELL_TYPE_FORMULA: // 公式
                cellValue = "错误";
                break;
            default:
                cellValue = "错误";
        }
        return cellValue;
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值