java springboot 导入不定合并行记录

2018-10-16  本文已影响0人  简陌刀丶阿吉

1、导入模版


合并行导入模版

2、思路
(1)正常去取每一行数据。
(2)判断取到的行中记录是否存在合并行。
(3)取出合并行的记录,并赋值到当前行。
(4)整合后的数据就是行数据【数据合并行和标题合并行操作类似】

3、导入用到的主要的三个方法,其余逻辑,在实际业务中进行判断。

/**
 * 获取合并单元格的值
 * 
 * @param sheet
 * @param row
 * @param column
 * @return
 */ 
private static String getMergedRegionValue(HSSFSheet 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;
}

/**
 * 判断指定的单元格是否是合并单元格
 * 
 * @param sheet
 * @param row
 * @param column
 * @return
 * 
 */
public static boolean isMergedRegion(HSSFSheet 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) {
                return true;
            }
        }
    }
    return false;
}

/**
 * * 获取单元格的值
 * 
 * @param cell
 * @return
 */
public static String getCellValue(Cell cell) {
    if (cell == null)
        return "";
    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue();
    } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        return String.valueOf(cell.getBooleanCellValue());
    } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        return cell.getCellFormula();
    } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        return String.valueOf(cell.getNumericCellValue());
    }
    return "";
}
上一篇 下一篇

猜你喜欢

热点阅读