Example usage for org.apache.poi.ss.usermodel Row getFirstCellNum

List of usage examples for org.apache.poi.ss.usermodel Row getFirstCellNum

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Row getFirstCellNum.

Prototype

short getFirstCellNum();

Source Link

Document

Get the number of the first cell contained in this row.

Usage

From source file:org.aludratest.app.excelwizard.WorkbookTracker.java

License:Apache License

void validate() {
    CellStyle warningCellStyle = this.workbook.createCellStyle();
    warningCellStyle.setFillForegroundColor(HSSFColor.RED.index);
    warningCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    for (Map.Entry<String, List<String>> headerConfig : expectedSheetColumnHeaders.entrySet()) {
        String sheetName = headerConfig.getKey();
        Sheet sheet = this.workbook.getSheet(sheetName);
        Row headerRow = sheet.getRow(0);
        List<String> expectedHeaders = headerConfig.getValue();
        int lastCellNum = headerRow.getLastCellNum();
        int firstCellNum = headerRow.getFirstCellNum();
        for (int i = firstCellNum; i < lastCellNum; i++) {
            Cell headerCell = headerRow.getCell(i);
            if (headerCell != null) {
                String actualHeader = headerCell.getStringCellValue();
                if (actualHeader != null && actualHeader.trim().length() > 0) {
                    if (!expectedHeaders.contains(actualHeader)) {
                        this.warnings.add("Unmappable column '" + actualHeader + "' in sheet '" + sheetName
                                + "' of file '" + file.getName() + "'");
                        headerCell.setCellStyle(warningCellStyle);
                        this.status = STATUS_MODIFIED;
                    }//from  w  w w.  ja v a2s.com
                }
            }
        }
    }
}

From source file:org.apache.any23.plugin.officescraper.ExcelExtractor.java

License:Apache License

private void writeRowMetadata(URI rowURI, Row row, ExtractionResult er) {
    final int firstCellNum = row.getFirstCellNum();
    final int lastCellNum = row.getLastCellNum();
    er.writeTriple(rowURI, excel.firstCell, RDFUtils.literal(firstCellNum));
    er.writeTriple(rowURI, excel.lastCell, RDFUtils.literal(lastCellNum));
}

From source file:org.apache.metamodel.excel.DefaultSpreadsheetReaderDelegate.java

License:Apache License

/**
 * Gets the column offset (first column to include). This is dependent on
 * the row used for column processing and whether the skip empty columns
 * property is set./*from  w  w  w. ja v  a 2 s .  com*/
 * 
 * @param row
 * @return
 */
private int getColumnOffset(Row row) {
    final int offset;
    if (_configuration.isSkipEmptyColumns()) {
        offset = row.getFirstCellNum();
    } else {
        offset = 0;
    }
    return offset;
}

From source file:org.bbreak.excella.core.tag.excel2java.MapsParser.java

License:Open Source License

/**
 * ?/*  w w w .ja v  a 2s  . c o m*/
 * 
 * @param sheet 
 * @param tagCell ???
 * @param data BookController?parseBook(), parseSheet()?<BR>
 * SheetParser?parseSheet?????<BR>
 * TagParser??????<BR>
 * @return ?
 * @throws ParseException 
 */
@Override
public List<Map<?, ?>> parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    List<Map<?, ?>> resultList = new ArrayList<Map<?, ?>>();

    // 
    int tagRowIdx = tagCell.getRowIndex();
    // 
    int keyRowIdx;
    // 
    int valueRowFromIdx;
    // 
    int valueRowToIdx = sheet.getLastRowNum();

    try {
        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        // ?
        keyRowIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_KEY_ROW, DEFAULT_KEY_ROW_ADJUST);
        if (keyRowIdx < 0 || keyRowIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_KEY_ROW);
        }

        // ?
        valueRowFromIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_FROM,
                DEFAULT_VALUE_ROW_FROM_ADJUST);
        if (valueRowFromIdx < 0 || valueRowFromIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_FROM);
        }

        // ?
        valueRowToIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_TO, valueRowToIdx - tagRowIdx);
        if (valueRowToIdx > sheet.getLastRowNum() || valueRowToIdx < 0) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_TO);
        }

        // ???
        if (valueRowFromIdx > valueRowToIdx) {
            throw new ParseException(tagCell,
                    "?" + PARAM_DATA_ROW_FROM + "," + PARAM_DATA_ROW_TO);
        }

    } catch (Exception e) {
        if (e instanceof ParseException) {
            throw (ParseException) e;
        } else {
            throw new ParseException(tagCell, e);
        }
    }

    // ??
    List<Integer> targetColNums = new ArrayList<Integer>();
    Row keyRow = sheet.getRow(keyRowIdx);
    if (keyRow == null) {
        // ?null??
        return resultList;
    }
    int firstCellNum = keyRow.getFirstCellNum();
    int lastCellNum = keyRow.getLastCellNum();
    for (int cellCnt = firstCellNum; cellCnt < lastCellNum; cellCnt++) {
        Cell cell = keyRow.getCell(cellCnt);
        Object cellValue = PoiUtil.getCellValue(cell);
        if (cellValue instanceof String) {
            String keyName = (String) cellValue;
            if (keyName.startsWith(BookController.COMMENT_PREFIX)) {
                continue;
            }
        }
        if (cellValue != null) {
            targetColNums.add(cellCnt);
        }
    }

    if (targetColNums.size() > 0) {
        // ????

        // ??
        for (int rowCnt = valueRowFromIdx; rowCnt <= valueRowToIdx; rowCnt++) {
            Row dataRow = sheet.getRow(rowCnt);
            if (dataRow == null) {
                continue;
            }
            Map<Object, Object> map = new LinkedHashMap<Object, Object>();
            for (Integer colCnt : targetColNums) {
                Cell keyCell = keyRow.getCell(colCnt);
                Cell valueCell = dataRow.getCell(colCnt);

                Object key = PoiUtil.getCellValue(keyCell);
                Object value = PoiUtil.getCellValue(valueCell);

                map.put(key, value);
            }
            resultList.add(map);
        }
    }

    return resultList;
}

From source file:org.bbreak.excella.core.tag.excel2java.ObjectsParser.java

License:Open Source License

/**
 * ?//from   w  w w .  j  a v a2 s .c  o m
 * 
 * @param sheet 
 * @param tagCell ???
 * @param data BookController?parseBook(), parseSheet()?<BR>
 *              SheetParser?parseSheet?????<BR>
 *              TagParser??????<BR>
 * @return ?
 * @throws ParseException 
 */
@Override
public List<Object> parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    List<Object> resultList = new ArrayList<Object>();
    Class<?> clazz = null;

    // 
    int tagRowIdx = tagCell.getRowIndex();
    // 
    int propertyRowIdx;
    // 
    int valueRowFromIdx;
    // 
    int valueRowToIdx = sheet.getLastRowNum();

    try {
        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        clazz = Class.forName(paramDef.get(PARAM_CLASS));

        // ?
        propertyRowIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_PROPERTY_ROW,
                DEFAULT_PROPERTY_ROW_ADJUST);
        if (propertyRowIdx < 0 || propertyRowIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_PROPERTY_ROW);
        }

        // ?
        valueRowFromIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_FROM,
                DEFAULT_VALUE_ROW_FROM_ADJUST);
        if (valueRowFromIdx < 0 || valueRowFromIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_FROM);
        }

        // ?
        valueRowToIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_TO, valueRowToIdx - tagRowIdx);
        if (valueRowToIdx > sheet.getLastRowNum() || valueRowToIdx < 0) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_TO);
        }

        // ???
        if (valueRowFromIdx > valueRowToIdx) {
            throw new ParseException(tagCell,
                    "?" + PARAM_DATA_ROW_FROM + "," + PARAM_DATA_ROW_TO);
        }

    } catch (Exception e) {
        if (e instanceof ParseException) {
            throw (ParseException) e;
        } else {
            throw new ParseException(tagCell, e);
        }
    }

    // ???
    Map<Integer, Class<?>> propertyClassMap = new HashMap<Integer, Class<?>>();
    // ????
    Map<Integer, String> propertyNameMap = new HashMap<Integer, String>();
    // ??
    Map<String, List<ObjectsPropertyParser>> customPropertyParserMap = new HashMap<String, List<ObjectsPropertyParser>>();

    // ??
    List<Integer> targetColNums = new ArrayList<Integer>();
    Row propertyRow = sheet.getRow(propertyRowIdx);
    if (propertyRow == null) {
        // ?null??
        return resultList;
    }
    int firstCellNum = propertyRow.getFirstCellNum();
    int lastCellNum = propertyRow.getLastCellNum();
    for (int cellCnt = firstCellNum; cellCnt < lastCellNum; cellCnt++) {
        Cell cell = propertyRow.getCell(cellCnt);
        if (cell == null) {
            continue;
        }
        try {
            String propertyName = cell.getStringCellValue();
            if (propertyName.startsWith(BookController.COMMENT_PREFIX)) {
                continue;
            }

            Object obj = clazz.newInstance();
            Class<?> propertyClass = PropertyUtils.getPropertyType(obj, propertyName);
            if (propertyClass != null) {
                propertyClassMap.put(cellCnt, propertyClass);
                propertyNameMap.put(cellCnt, propertyName);
                targetColNums.add(cellCnt);
            } else {
                // ????
                for (ObjectsPropertyParser parser : customPropertyParsers) {
                    if (parser.isParse(sheet, cell)) {
                        List<ObjectsPropertyParser> propertyParsers = customPropertyParserMap.get(propertyName);
                        if (propertyParsers == null) {
                            propertyParsers = new ArrayList<ObjectsPropertyParser>();
                        }
                        // ???????
                        if (!propertyParsers.contains(parser)) {
                            propertyParsers.add(parser);
                        }
                        customPropertyParserMap.put(propertyName, propertyParsers);

                        if (!targetColNums.contains(cellCnt)) {
                            propertyNameMap.put(cellCnt, propertyName);
                            targetColNums.add(cellCnt);
                        }
                    }
                }
            }

        } catch (Exception e) {
            throw new ParseException(cell, e);
        }
    }

    if (targetColNums.size() > 0) {
        // ????

        // ??
        for (int rowCnt = valueRowFromIdx; rowCnt <= valueRowToIdx; rowCnt++) {
            Row dataRow = sheet.getRow(rowCnt);
            if (dataRow == null) {
                continue;
            }
            Object obj;
            try {
                obj = clazz.newInstance();
                for (Integer colCnt : targetColNums) {
                    Cell cell = dataRow.getCell(colCnt);

                    try {
                        Class<?> propertyClass = propertyClassMap.get(colCnt);
                        String propertyName = propertyNameMap.get(colCnt);
                        // ?
                        if (customPropertyParserMap.containsKey(propertyName)) {
                            List<ObjectsPropertyParser> propertyParsers = customPropertyParserMap
                                    .get(propertyName);
                            Map<String, String> params = TagUtil.getParams(propertyName);
                            Object cellValue = PoiUtil.getCellValue(cell);

                            // ??
                            for (ObjectsPropertyParser propertyParser : propertyParsers) {
                                propertyParser.parse(obj, cellValue, TagUtil.getTag(propertyName), params);
                            }
                        } else {
                            Object value = null;
                            if (cell != null) {
                                value = PoiUtil.getCellValue(cell, propertyClass);
                            }
                            PropertyUtils.setProperty(obj, propertyName, value);
                        }
                    } catch (Exception e) {
                        throw new ParseException(cell, e);
                    }
                }
            } catch (Exception e) {
                if (e instanceof ParseException) {
                    throw (ParseException) e;
                } else {
                    throw new ParseException(tagCell, e);
                }
            }
            resultList.add(obj);
        }
    }
    return resultList;
}

From source file:org.bbreak.excella.reports.listener.BreakAdapter.java

License:Open Source License

/**
 * ?????????/*w  w w . j a  va 2  s . com*/
 */
protected void parseRow(Sheet sheet, SheetParser sheetParser, SheetData sheetData, Row row, int rowIndex) {
    int firstColNum = row.getFirstCellNum();
    int lastColNum = row.getLastCellNum() - 1;

    for (int colIndex = firstColNum; colIndex <= lastColNum; colIndex++) {
        Cell cell = row.getCell(colIndex);
        if (cell != null) {
            if (cell.getCellTypeEnum() == CellType.STRING
                    && cell.getStringCellValue().contains(BreakParamParser.DEFAULT_TAG)) {
                // 
                if (isInMergedRegion(sheet, row, cell)) {
                    setRowBreakMergedRegion(sheet, row, cell);
                } else {
                    setRowBreak(sheet, row, cell);
                }
            }
        }
    }

}

From source file:org.bbreak.excella.reports.listener.RemoveAdapter.java

License:Open Source License

@Override
public void postParse(Sheet sheet, SheetParser sheetParser, SheetData sheetData) throws ParseException {

    int firstRowNum = sheet.getFirstRowNum();
    int lastRowNum = sheet.getLastRowNum();

    for (int rowIndex = firstRowNum; rowIndex <= lastRowNum; rowIndex++) {

        Row row = sheet.getRow(rowIndex);
        if (row != null) {
            int firstColNum = row.getFirstCellNum();
            int lastColNum = row.getLastCellNum() - 1;
            boolean isRowFlag = false;

            for (int colIndex = firstColNum; colIndex <= lastColNum; colIndex++) {
                Cell cell = row.getCell(colIndex);
                if (cell != null) {
                    if (cell.getCellTypeEnum() == CellType.STRING
                            && cell.getStringCellValue().contains(RemoveParamParser.DEFAULT_TAG)) {
                        // ??
                        String[] paramArray = getStrParam(sheet, rowIndex, colIndex);

                        // ??
                        String removeUnit = paramArray[0];
                        // ??
                        row.removeCell(cell);

                        // ????
                        if (removeUnit.equals("") || removeUnit.equals(ROW)) {
                            removeRegion(sheet, rowIndex, -1);
                            removeControlRow(sheet, rowIndex);
                            isRowFlag = true;
                            break;
                        } else if (removeUnit.equals(CELL) || removeUnit.equals(COLUMN)) {
                            // ???????
                            removeCellOrCol(paramArray, removeUnit, sheet, row, cell, rowIndex, colIndex);
                        }/* w w w .j  a  va  2 s  . c o m*/
                        lastColNum = row.getLastCellNum() - 1;
                        colIndex--;
                    }
                    // ??
                    if (isControlRow(sheet, sheetParser, row, cell)) {
                        removeControlRow(sheet, rowIndex);
                        isRowFlag = true;
                        break;
                    }
                }
            }
            // ???
            if (isRowFlag) {
                lastRowNum = sheet.getLastRowNum();
                rowIndex--;
            }
        }
    }
}

From source file:org.bbreak.excella.trans.tag.sheet2java.SheetToJavaExecuter.java

License:Open Source License

/**
 * ??????<BR>/*from  w w w  .j  ava  2 s.  c o m*/
 * ???????<BR>
 * 
 * @param targetSheet ?
 * @param targetColumnInfoList 
 * @return 
 * @throws ParseException 
 */
protected List<Object> parseTargetSheet(Sheet targetSheet, SheetToJavaParseInfo sheetInfo,
        List<SheetToJavaSettingInfo> targetColumnInfoList) throws ParseException {

    // ??
    List<Object> results = new ArrayList<Object>();

    int logicalRowNum = sheetInfo.getLogicalNameRowNum() - 1;
    int valueStartRowNum = sheetInfo.getValueRowNum() - 1;
    int valueEndRowNum = targetSheet.getLastRowNum();

    // ????index?
    Map<String, Integer> colLogicalNameMap = new HashMap<String, Integer>();

    // colLogicalNameMap?
    Row row = targetSheet.getRow(logicalRowNum);
    if (row != null) {

        // ?????
        int firstColIdx = row.getFirstCellNum();
        int lastColIdx = row.getLastCellNum();

        for (int colIdx = firstColIdx; colIdx <= lastColIdx; colIdx++) {
            Cell cell = row.getCell(colIdx);
            if (cell != null) {
                try {
                    // ???
                    String logicalCellValue = cell.getStringCellValue();
                    if (!logicalCellValue.startsWith(BookController.COMMENT_PREFIX)) {
                        colLogicalNameMap.put(logicalCellValue, colIdx);
                    }
                } catch (Exception e) {
                    throw new ParseException(cell, e);
                }
            }
        }
    }

    // ?????????????
    List<Class<?>> classList = new ArrayList<Class<?>>();

    // ?SettingInfo?
    Map<Class<?>, List<SheetToJavaSettingInfo>> settingInfoListMap = new HashMap<Class<?>, List<SheetToJavaSettingInfo>>();
    // ???????
    Map<Class<?>, List<String>> uniquePropertyListMap = new HashMap<Class<?>, List<String>>();
    for (SheetToJavaSettingInfo settingInfo : targetColumnInfoList) {

        // ??
        Class<?> clazz = settingInfo.getClazz();
        List<SheetToJavaSettingInfo> settingInfoList = settingInfoListMap.get(clazz);
        if (settingInfoList == null) {
            // ?????????
            settingInfoList = new ArrayList<SheetToJavaSettingInfo>();
        }
        List<String> uniquePropertyList = uniquePropertyListMap.get(clazz);
        if (uniquePropertyList == null) {
            // ?????????
            uniquePropertyList = new ArrayList<String>();
        }

        // ??
        settingInfoList.add(settingInfo);
        if (settingInfo.isUnique()) {
            uniquePropertyList.add(settingInfo.getPropertyName());
        }

        // ???
        if (!classList.contains(clazz)) {
            classList.add(clazz);
        }

        // ??
        settingInfoListMap.put(clazz, settingInfoList);
        uniquePropertyListMap.put(clazz, uniquePropertyList);
    }

    // ???
    for (Class<?> clazz : classList) {

        // ??
        List<Object> objList = new ArrayList<Object>();

        Object obj = null;
        try {

            // ???
            for (int valueRowIdx = valueStartRowNum; valueRowIdx <= valueEndRowNum; valueRowIdx++) {
                Row valueRow = targetSheet.getRow(valueRowIdx);
                if (valueRow == null) {
                    continue;
                }

                boolean isProcessRow = true;
                for (SheetToJavaListener propertyListener : sheetToJavaListeners) {
                    if (!propertyListener.preProcessRow(valueRow)) {
                        isProcessRow = false;
                    }
                }
                if (!isProcessRow) {
                    continue;
                }

                obj = Class.forName(clazz.getName()).newInstance();

                // ???
                List<SheetToJavaSettingInfo> settingInfoList = settingInfoListMap.get(clazz);
                for (SheetToJavaSettingInfo settingInfo : settingInfoList) {

                    // ??
                    String propertyName = settingInfo.getPropertyName();
                    // 
                    Object value = settingInfo.getValue();
                    // ?
                    Object settingValue = value;
                    Cell valueCell = null;

                    if (value instanceof String) {
                        // ??
                        String settingValueStr = (String) value;
                        if (settingValueStr.startsWith(TAG_PREFIX)) {
                            // ??
                            if (settingValueStr.startsWith(TAG_LOGICAL_NAME_PREFIX)) {
                                // ?????
                                String logicalKey = TagUtil.getParam(settingValueStr, LNAME_TAG_PARAM_PREFIX,
                                        LNAME_TAG_PARAM_SUFFIX);
                                Integer logicalKeyCol = colLogicalNameMap.get(logicalKey);
                                if (logicalKeyCol == null) {
                                    Cell errorCell = null;
                                    for (SheetToJavaSettingInfo columnInfo : targetColumnInfoList) {
                                        if (columnInfo.getValue().equals(settingValueStr)) {
                                            errorCell = columnInfo.getValueCell();
                                        }
                                    }
                                    throw new ParseException(errorCell,
                                            "????:" + logicalKey);
                                }

                                valueCell = valueRow.getCell(logicalKeyCol);
                                if (valueCell != null) {
                                    Class<?> propertyClass = PropertyUtils.getPropertyType(obj,
                                            settingInfo.getPropertyName());
                                    try {
                                        settingValue = PoiUtil.getCellValue(valueCell, propertyClass);
                                    } catch (RuntimeException e) {
                                        throw new ParseException(valueCell,
                                                "???????(" + propertyClass + ")", e);
                                    }
                                } else {
                                    // ?null??
                                    settingValue = null;
                                    valueCell = null;
                                }

                            } else {
                                // ?????
                                // ??
                                parseCustomProperty(valueCell, colLogicalNameMap, obj, valueRow,
                                        settingValueStr);
                                // ??
                                continue;
                            }
                        }
                    }

                    // 
                    try {
                        // ?????
                        for (SheetToJavaListener propertyListener : sheetToJavaListeners) {
                            propertyListener.preSetProperty(valueCell, obj, propertyName, settingValue);
                        }

                        PropertyUtils.setProperty(obj, propertyName, settingValue);

                        // ????
                        for (SheetToJavaListener propertyListener : sheetToJavaListeners) {
                            propertyListener.postSetProperty(valueCell, obj, propertyName, settingValue);
                        }
                    } catch (ParseException parseEx) {
                        throw parseEx;
                    } catch (RuntimeException e) {
                        throw new ParseException(valueCell,
                                "??????(" + propertyName + "=" + settingValue + "["
                                        + settingValue.getClass().getCanonicalName() + "]" + ")",
                                e);
                    }
                }

                for (SheetToJavaListener propertyListener : sheetToJavaListeners) {
                    if (!propertyListener.postProcessRow(valueRow, obj)) {
                        isProcessRow = false;
                    }
                }
                if (!isProcessRow) {
                    continue;
                }

                List<String> uniquePropertyList = uniquePropertyListMap.get(clazz);
                if (!isDuplicateObj(obj, objList, uniquePropertyList)) {
                    // ???????
                    objList.add(obj);
                }
            }

            // ????
            results.addAll(objList);
        } catch (ParseException parseEx) {
            throw parseEx;
        } catch (Exception e) {
            throw new ParseException(e.toString());
        }
    }

    return results;
}

From source file:org.bbreak.excella.trans.tag.sheet2sql.SheetToSqlExecuter.java

License:Open Source License

/**
 * ?????Insert?Sql?<BR>//from  w w  w.jav  a 2s . co m
 * ???????<BR>
 * 
 * @param targetSheet ?
 * @param targetColumnInfoList 
 * @return Sql
 * @throws ParseException 
 */
protected List<Object> parseTargetSheet(Sheet targetSheet, SheetToSqlParseInfo sheetInfo,
        List<SheetToSqlSettingInfo> targetColumnInfoList) throws ParseException {

    // ??
    List<Object> results = new ArrayList<Object>();

    int logicalRowNum = sheetInfo.getLogicalNameRowNum() - 1;
    int valueStartRowNum = sheetInfo.getValueRowNum() - 1;
    int valueEndRowNum = targetSheet.getLastRowNum();

    // ????index?
    Map<String, Integer> colLogicalNameMap = new HashMap<String, Integer>();

    // colLogicalNameMap?
    Row row = targetSheet.getRow(logicalRowNum);
    if (row != null) {

        // ?????
        int firstColIdx = row.getFirstCellNum();
        int lastColIdx = row.getLastCellNum();

        for (int colIdx = firstColIdx; colIdx <= lastColIdx; colIdx++) {
            Cell cell = row.getCell(colIdx);
            if (cell != null) {
                try {
                    // ???
                    String logicalCellValue = cell.getStringCellValue();
                    if (!logicalCellValue.startsWith(BookController.COMMENT_PREFIX)) {
                        colLogicalNameMap.put(logicalCellValue, colIdx);
                    }
                } catch (Exception e) {
                    throw new ParseException(cell, e);
                }
            }
        }
    }

    // ?????????????
    List<String> tableNameList = new ArrayList<String>();

    // ?SettingInfo?
    Map<String, List<SheetToSqlSettingInfo>> settingInfoListMap = new HashMap<String, List<SheetToSqlSettingInfo>>();
    // ???????
    Map<String, List<String>> uniqueColumnListMap = new HashMap<String, List<String>>();
    for (SheetToSqlSettingInfo settingInfo : targetColumnInfoList) {

        // ??
        String tableName = settingInfo.getTableName();
        List<SheetToSqlSettingInfo> settingInfoList = settingInfoListMap.get(tableName);
        if (settingInfoList == null) {
            // ?????????
            settingInfoList = new ArrayList<SheetToSqlSettingInfo>();
        }
        List<String> uniqueColumnList = uniqueColumnListMap.get(tableName);
        if (uniqueColumnList == null) {
            // ?????????
            uniqueColumnList = new ArrayList<String>();
        }

        // ??
        settingInfoList.add(settingInfo);
        if (settingInfo.isUnique()) {
            uniqueColumnList.add(settingInfo.getColumnName());
        }

        // ???
        if (!tableNameList.contains(tableName)) {
            tableNameList.add(tableName);
        }

        // ??
        settingInfoListMap.put(tableName, settingInfoList);
        uniqueColumnListMap.put(tableName, uniqueColumnList);
    }

    // ?????
    for (String tableName : tableNameList) {

        // SQL???????
        List<SheetToSqlInfo> infoList = new ArrayList<SheetToSqlInfo>();

        SheetToSqlInfo info = null;

        // ???
        for (int valueRowIdx = valueStartRowNum; valueRowIdx <= valueEndRowNum; valueRowIdx++) {

            Map<String, String> columnValueMap = new HashMap<String, String>();
            List<String> columnNameList = new ArrayList<String>();

            // SheetToSqlInfo?
            info = new SheetToSqlInfo();
            info.setTableName(tableName);
            info.setColumnValueMap(columnValueMap);
            info.setColumnNameList(columnNameList);

            Row valueRow = targetSheet.getRow(valueRowIdx);
            if (valueRow == null) {
                continue;
            }

            // ???
            List<SheetToSqlSettingInfo> settingInfoList = settingInfoListMap.get(tableName);
            for (SheetToSqlSettingInfo settingInfo : settingInfoList) {
                // ??
                String columnName = settingInfo.getColumnName();
                // 
                Object value = settingInfo.getValue();
                // 
                String dataType = settingInfo.getDataType();

                // ??value
                Object target = value;
                Cell cell = null;

                if (value instanceof String) {
                    // ???
                    String settingValueStr = (String) value;
                    if (settingValueStr.startsWith(TAG_LOGICAL_NAME_PREFIX)) {
                        // ?????
                        String logicalKey = TagUtil.getParam(settingValueStr, LNAME_TAG_PARAM_PREFIX,
                                LNAME_TAG_PARAM_SUFFIX);
                        Integer logicalKeyCol = colLogicalNameMap.get(logicalKey);
                        if (logicalKeyCol == null) {
                            throw new ParseException(settingInfo.getValueCell(),
                                    "????:" + logicalKey);
                        }

                        // ?????????
                        cell = valueRow.getCell(logicalKeyCol);
                        target = PoiUtil.getCellValue(cell);
                    }
                }

                // ??
                try {
                    String valueStr = dataConverter.convert(target, dataType, settingInfo);

                    // ??
                    columnValueMap.put(columnName, valueStr);

                    // ????
                    columnNameList.add(columnName);
                } catch (ParseException parseEx) {
                    // ????????????????
                    if (cell != null) {
                        parseEx.setCell(cell);
                    }
                    throw parseEx;
                }
            }

            List<String> uniqueColumnList = uniqueColumnListMap.get(tableName);
            if (!isDuplicateObj(info, infoList, uniqueColumnList)) {
                // ???????
                infoList.add(info);
            }
        }

        // SQL????????
        List<String> sqlList = createInsertSqlList(infoList);
        results.addAll(sqlList);
    }

    return results;
}

From source file:org.bdxjug.api.infrastructure.sheet.xlsx.XlsxSheet.java

License:Apache License

private static boolean isRowEmpty(Row row) {
    for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
        Cell cell = row.getCell(c);//from  w w w  . j av a 2s  . c o  m
        if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK)
            return false;
    }
    return true;
}