Java 读取excel解析合并单元格

需求

  • 解析这个excel 空格填写e ,单元格合并的拆分后填写合并前的值
    在这里插入图片描述
id	啊	哦	额	
1	2	2	3	
2	e	e	3	
3	4	4	3	
4	4	4	3	
5	0	0	2019-09-01	

code

依赖

 <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>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-schemas</artifactId>
      <version>3.15</version>
    </dependency>

代码

  • 保存实体类
package com.huifer.springsource.uuc;


class ExcelDataObject {
    /**
     * 数据
     */
    private String cellData;
    private int regionId;

    ExcelDataObject(String cellData, int regionId) {
        this.cellData = cellData;
        this.regionId = regionId;
    }

    public String getCellData() {
        return cellData;
    }

    public void setCellData(String cellData) {
        this.cellData = cellData;
    }

    public int getRegionId() {
        return regionId;
    }

    public void setRegionId(int regionId) {
        this.regionId = regionId;
    }
}
  • 核心方法
package com.huifer.springsource.uuc;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;


class ExcelReaderImpl implements ExcelReader {

    private XSSFWorkbook excelWBook;
    private XSSFSheet excelWSheet;

    private List<CellRangeAddress> mergedRegions;

    private Logger log = Logger.getLogger(this.getClass().getName());

    /**
     * 具体获取日期 或者其他值
     *
     * @param cell
     * @return
     */
    private static String getValue(Cell cell) {
        if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(cell.getBooleanCellValue());
        } else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
            //数值类型又具体区分日期类型,单独处理
            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                String pattern;
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                return simpleDateFormat.format(date);
            } else {
                return NumberToTextConverter.toText(cell.getNumericCellValue());
            }
        } else {
            return String.valueOf(cell.getStringCellValue());
        }
    }

    /**
     * 打开文件
     */
    @Override
    public void setExcelFile(String path) {
        try {
            FileInputStream ExcelFile = new FileInputStream(path);
            excelWBook = new XSSFWorkbook(ExcelFile);
            ExcelFile.close();
            switchToSheet(0);
        } catch (Exception e) {
            throw new RuntimeException("文件有问题开不开 " + e.getMessage(), e);
        }
    }

    /**
     * 根据 sheet name 设置
     */
    @Override
    public void switchToSheet(String sheetName) throws RuntimeException {
        int sheetIndex = excelWBook.getSheetIndex(sheetName);
        switchToSheet(sheetIndex);
    }

    /**
     * 返回指定 sheet
     */
    @Override
    public void switchToSheet(int number) throws RuntimeException {
        excelWSheet = excelWBook.getSheetAt(number);
        mergedRegions = excelWSheet.getMergedRegions();
    }

    /**
     * 获取整个 sheet
     */
    @Override
    public ExcelDataObject[][] getCurrentSheetData() {
        int usedRows = getRowsUsed();
        ExcelDataObject[][] data = new ExcelDataObject[usedRows][];
        for (int i = 0; i < usedRows; i++) {
            data[i] = getRowData(i);
        }
        return data;
    }

    /**
     * 获取整个 sheet
     */
    @Override
    public ExcelDataObject[][] getSheetData(String sheetName) {
        switchToSheet(sheetName);
        int usedRows = getRowsUsed();
        ExcelDataObject[][] data = new ExcelDataObject[usedRows][];
        for (int i = 0; i <= usedRows; i++) {
            data[i] = getRowData(i);
        }
        return data;
    }

    /**
     * 获取行数据
     */
    @Override
    public ExcelDataObject[] getRowData(int rowNo) {
        int usedColumns = getColumnsUsed(rowNo);
        ExcelDataObject[] rowData = new ExcelDataObject[usedColumns];
        for (int i = 0; i < usedColumns; i++) {
            rowData[i] = getCellData(rowNo, i);
        }
        return rowData;
    }

    /**
     * 获取这个单元格的值
     */
    @Override
    public ExcelDataObject getCellData(int rowNum, int colNum) {
        int region = getMergedRegion(rowNum, colNum);
        XSSFCell cell;
        try {
            cell = region >= 0 ? getMergedRegionStringValue(rowNum, colNum) :
                    excelWSheet.getRow(rowNum).getCell(colNum);
            return getStringValueFromCell(region, cell);
        } catch (Exception e) {
//            log.info("这个单元格没有值 " + e.getMessage());
            return new ExcelDataObject("e", region);
        }
    }

    /**
     * 获取这个单元格的字符串值
     *
     * @param region
     * @param cell
     * @return
     */
    private ExcelDataObject getStringValueFromCell(int region, XSSFCell cell) {

        return new ExcelDataObject(getValue(cell), region);
    }

    /**
     * 行数
     *
     * @return
     */
    private int getRowsUsed() {
        if (excelWSheet == null) {
            return 0;
        }
        return excelWSheet.getLastRowNum();
    }

    public int getRols() {
        if (excelWSheet == null) {
            return 0;
        }
        return excelWSheet.getLastRowNum() + 1;
    }


    /**
     * 列数
     *
     * @param rowNo
     * @return
     */
    private int getColumnsUsed(int rowNo) {
        if (excelWSheet == null) {
            return 0;
        }
        return excelWSheet.getRow(rowNo).getPhysicalNumberOfCells();
    }


    public int getCols() {
        return excelWSheet.getRow(0).getPhysicalNumberOfCells();
    }


    @Override
    public List<List<ExcelDataObject>> getSheet(String path) {
        setExcelFile(path);

        int rowsUsed = getRols();
        int cols = getCols();
        List<List<ExcelDataObject>> sheet = new ArrayList<>();
        for (int i = 0; i < rowsUsed; i++) {
            List<ExcelDataObject> row = new ArrayList<>();
            for (int j = 0; j < cols; j++) {
                Object cellData = getCellData(i, j);
                ExcelDataObject data = (ExcelDataObject) cellData;
                row.add(data);
                String cellData1 = data.getCellData();
                System.out.print(cellData1 + "\t");
            }
            sheet.add(row);
            System.out.println();
        }
        return sheet;
    }

    /**
     * 合并的坐标值
     *
     * @param rowNum
     * @param colNum
     * @return
     */
    private int getMergedRegion(int rowNum, int colNum) {
        for (int i = 0; i < mergedRegions.size(); i++) {
            if (mergedRegions.get(i).isInRange(rowNum, colNum)) {
                return i;
            }
        }
        return -1;
    }


    /**
     * 获取合并单元格的值
     *
     * @param row
     * @param column
     * @return
     */
    private XSSFCell getMergedRegionStringValue(int row, int column) {
        int mergedRegionNumber = getMergedRegion(row, column);
        CellRangeAddress region = excelWSheet.getMergedRegion(mergedRegionNumber);

        int firstRegionColumn = region.getFirstColumn();
        int firstRegionRow = region.getFirstRow();

        return excelWSheet.getRow(firstRegionRow).getCell(firstRegionColumn);
    }
}

  • 测试
package com.huifer.springsource.uuc;


import java.util.ArrayList;
import java.util.List;

public class ExcelReaderTest {
    public static void main(String[] args) {
//        ExcelReader excelReader = new ExcelReaderImpl();
//        List<List<ExcelDataObject>> sheet = excelReader.getSheet("E:\\w_pro\\tt\\demo\\spring-source\\src\\main\\resources\\work01.xlsx");
//        System.out.println();

        getSheet();
    }

    private static void getSheet() {
        ExcelReader excelReader = new ExcelReaderImpl();
        excelReader.setExcelFile("E:\\w_pro\\tt\\demo\\spring-source\\src\\main\\resources\\work01.xlsx");

        int rowsUsed = excelReader.getRols();
        int cols = excelReader.getCols();
        List<List<ExcelDataObject>> sheet = new ArrayList<>();
        for (int i = 0; i < rowsUsed; i++) {
            List<ExcelDataObject> row = new ArrayList<>();
            for (int j = 0; j < cols; j++) {
                Object cellData = excelReader.getCellData(i, j);
                ExcelDataObject data = (ExcelDataObject) cellData;
                row.add(data);
                String cellData1 = data.getCellData();
                System.out.print(cellData1 + "\t");
            }
            sheet.add(row);
            System.out.println();
        }
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值