Java Code Examples for org.apache.poi.ss.usermodel.Row#getFirstCellNum()

The following examples show how to use org.apache.poi.ss.usermodel.Row#getFirstCellNum() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExcelExportOfTemplateUtil.java    From jeasypoi with Apache License 2.0 7 votes vote down vote up
/**
 * 先判断删除,省得影响效率
 * 
 * @param sheet
 * @param map
 * @throws Exception
 */
private void deleteCell(Sheet sheet, Map<String, Object> map) throws Exception {
	Row row = null;
	Cell cell = null;
	int index = 0;
	while (index <= sheet.getLastRowNum()) {
		row = sheet.getRow(index++);
		if (row == null) {
			continue;
		}
		for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
			cell = row.getCell(i);
			if (row.getCell(i) != null && (cell.getCellType() == Cell.CELL_TYPE_STRING || cell.getCellType() == Cell.CELL_TYPE_NUMERIC)) {
				cell.setCellType(Cell.CELL_TYPE_STRING);
				String text = cell.getStringCellValue();
				if (text.contains(IF_DELETE)) {
					if (Boolean.valueOf(eval(text.substring(text.indexOf(START_STR) + 2, text.indexOf(END_STR)).trim(), map).toString())) {
						PoiSheetUtility.deleteColumn(sheet, i);
					}
					cell.setCellValue("");
				}
			}
		}
	}
}
 
Example 2
Source File: ExcelRowReader.java    From ServerCore with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public R read(Row row) {
    if (row == null) {
        return null;
    }
    R result = getResultSupplier().get();
    if (result == null) {
        return null;
    }
    BiConverter<? super R, Object, ? extends R> cellParser = getCellParser();
    if (cellParser == null) {
        return result;
    }
    for (int cellIndex = row.getFirstCellNum(), lastCellIndex = row.getLastCellNum(); cellIndex < lastCellIndex; cellIndex++) {
        Object cellValue = null;
        Cell cell = row.getCell(cellIndex);
        ICellReader cellReader = getCellReader(cellIndex);
        if (cellReader != null) {
            cellValue = cellReader.read(cell);
        }
        result = cellParser.apply(result, new Object[]{cell, cellIndex, cellValue});
    }
    return result;
}
 
Example 3
Source File: ExcelToHtmlServer.java    From autopoi with Apache License 2.0 6 votes vote down vote up
private void ensureColumnBounds(Sheet sheet) {
	if (gotBounds)
		return;

	Iterator<Row> iter = sheet.rowIterator();
	firstColumn = (iter.hasNext() ? Integer.MAX_VALUE : 0);
	endColumn = 0;
	while (iter.hasNext()) {
		Row row = iter.next();
		short firstCell = row.getFirstCellNum();
		if (firstCell >= 0) {
			firstColumn = Math.min(firstColumn, firstCell);
			endColumn = Math.max(endColumn, row.getLastCellNum());
		}
	}
	gotBounds = true;
}
 
Example 4
Source File: ExcelOperator.java    From minsx-framework with Apache License 2.0 6 votes vote down vote up
private static Map<String, String> getCellData(Sheet sheet, Row row, int cellIndex) {
    Map<String, String> result = new LinkedHashMap<>();
    result.put("value", null);
    result.put("MRValue", null);
    if (row == null) {
        return result;
    }
    if (row.getFirstCellNum() <= cellIndex && cellIndex <= row.getLastCellNum()) {
        result.put("value", getCellData(row.getCell(cellIndex)));
        result.put("type", row.getCell(cellIndex) == null ? "NULL" : row.getCell(cellIndex).getCellTypeEnum().toString());
    }
    if (isMergedRegion(sheet, row.getRowNum(), cellIndex)) {
        result.put("value", null);
        Cell cell = getMergedRegionCell(sheet, row.getRowNum(), cellIndex);
        result.put("type", cell == null ? "NULL" : cell.getCellTypeEnum().toString());
        result.put("MRValue", getMergedRegionValue(sheet, row.getRowNum(), cellIndex));
    }
    return result;
}
 
Example 5
Source File: ExcelExportOfTemplateUtil.java    From easypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取表头数据,设置表头的序号
 * 
 * @param teplateParams
 * @param sheet
 * @return
 */
private Map<String, Integer> getTitleMap(Sheet sheet) {
    Row row = null;
    Iterator<Cell> cellTitle;
    Map<String, Integer> titlemap = new HashMap<String, Integer>();
    for (int j = 0; j < teplateParams.getHeadingRows(); j++) {
        row = sheet.getRow(j + teplateParams.getHeadingStartRow());
        cellTitle = row.cellIterator();
        int i = row.getFirstCellNum();
        while (cellTitle.hasNext()) {
            Cell cell = cellTitle.next();
            String value = cell.getStringCellValue();
            if (!StringUtils.isEmpty(value)) {
                titlemap.put(value, i);
            }
            i = i + 1;
        }
    }
    return titlemap;

}
 
Example 6
Source File: AbstractSheet.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @param workbook
 * @param sheetName
 */
public AbstractSheet(Workbook workbook, String sheetName) {
	sheet = workbook.getSheet(sheetName);
	if (sheet != null) {
		firstRowNum = sheet.getFirstRowNum();
		Row firstRow = sheet.getRow(firstRowNum);
		if (firstRow == null) {
			firstCellNum = 1;
		} else {
			firstCellNum = firstRow.getFirstCellNum();
		}
		findLastRow();
	} else {
		firstRowNum = 0;
		lastRowNum = 0;
		firstCellNum = 0;
	}
	createStyles(workbook);
}
 
Example 7
Source File: ExcelExportOfTemplateUtil.java    From autopoi with Apache License 2.0 6 votes vote down vote up
/**
 * 先判断删除,省得影响效率
 * 
 * @param sheet
 * @param map
 * @throws Exception
 */
private void deleteCell(Sheet sheet, Map<String, Object> map) throws Exception {
	Row row = null;
	Cell cell = null;
	int index = 0;
	while (index <= sheet.getLastRowNum()) {
		row = sheet.getRow(index++);
		if (row == null) {
			continue;
		}
		for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
			cell = row.getCell(i);
			if (row.getCell(i) != null && (cell.getCellType() == Cell.CELL_TYPE_STRING || cell.getCellType() == Cell.CELL_TYPE_NUMERIC)) {
				cell.setCellType(Cell.CELL_TYPE_STRING);
				String text = cell.getStringCellValue();
				if (text.contains(IF_DELETE)) {
					if (Boolean.valueOf(eval(text.substring(text.indexOf(START_STR) + 2, text.indexOf(END_STR)).trim(), map).toString())) {
						PoiSheetUtility.deleteColumn(sheet, i);
					}
					cell.setCellValue("");
				}
			}
		}
	}
}
 
Example 8
Source File: ExcelToHtmlServer.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
private void ensureColumnBounds(Sheet sheet) {
	if (gotBounds)
		return;

	Iterator<Row> iter = sheet.rowIterator();
	firstColumn = (iter.hasNext() ? Integer.MAX_VALUE : 0);
	endColumn = 0;
	while (iter.hasNext()) {
		Row row = iter.next();
		short firstCell = row.getFirstCellNum();
		if (firstCell >= 0) {
			firstColumn = Math.min(firstColumn, firstCell);
			endColumn = Math.max(endColumn, row.getLastCellNum());
		}
	}
	gotBounds = true;
}
 
Example 9
Source File: ExcelExportOfTemplateUtil.java    From easypoi with Apache License 2.0 6 votes vote down vote up
private void parseTemplate(Sheet sheet, Map<String, Object> map) throws Exception {
    deleteCell(sheet, map);
    Row row = null;
    int index = 0;
    while (index <= sheet.getLastRowNum()) {
        row = sheet.getRow(index++);
        if (row == null) {
            continue;
        }
        for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
            if (row.getCell(i) != null
                && !tempCreateCellSet.contains(row.getRowNum() + "_"
                                               + row.getCell(i).getColumnIndex())) {
                setValueForCellByMap(row.getCell(i), map);
            }
        }
    }
}
 
Example 10
Source File: ImportExcelUtils.java    From MicroCommunity with Apache License 2.0 6 votes vote down vote up
/**
 * 获取Sheet页内容
 *
 * @param sheet
 * @return
 */
public static final List<Object[]> listFromSheet(Sheet sheet) {

    List<Object[]> list = new ArrayList<Object[]>();
    for (int r = sheet.getFirstRowNum(); r <= sheet.getLastRowNum(); r++) {
        Row row = sheet.getRow(r);
        if (row == null || row.getPhysicalNumberOfCells() == 0) continue;
        Object[] cells = new Object[row.getLastCellNum()];
        for (int c = row.getFirstCellNum(); c <= row.getLastCellNum(); c++) {
            Cell cell = row.getCell(c);
            if (cell == null) continue;
            //判断是否为日期类型
            cells[c] = getValueFromCell(cell);
        }
        list.add(cells);
    }
    return list;
}
 
Example 11
Source File: FileUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static boolean isRowEmpty(Row row) {
    for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
        Cell cell = row.getCell(c);

        if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
            return false;
        }
    }

    return true;
}
 
Example 12
Source File: DynamicSheetMapper.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
private SheetMapper<T> getPoiMapper(int startRow, Sheet sheet) {
    Row row = sheet.getRow(startRow);

    List<CsvColumnKey> keys = new ArrayList<CsvColumnKey>(row.getLastCellNum() - row.getFirstCellNum());
    for(short i = row.getFirstCellNum(); i <= row.getLastCellNum(); i++) {
        Cell cell = row.getCell(i);
        if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
            keys.add(new CsvColumnKey(cell.getStringCellValue(), i));
        }
    }

    return getPoiMapper(new MapperKey<CsvColumnKey>(keys.toArray(new CsvColumnKey[0])));


}
 
Example 13
Source File: HSSFUnmarshaller.java    From poiji with MIT License 5 votes vote down vote up
private boolean isRowEmpty(Row row) {
    for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
        Cell cell = row.getCell(c, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
        if (cell != null && cell.getCellType() != CellType.BLANK) {
            return false;
        }
    }
    return true;
}
 
Example 14
Source File: PersonSheetConfigurator.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public PersonSheetConfigurator(XSSFWorkbook workbook, Sheet sheet) {
	this.sheetIndex = workbook.getSheetIndex(sheet);
	Row row = sheet.getRow(sheet.getFirstRowNum());
	this.firstRow = sheet.getFirstRowNum() + 1;
	this.lastRow = sheet.getLastRowNum();
	memoColumn = row.getLastCellNum() + 1;
	for (int i = row.getFirstCellNum(); i <= row.getLastCellNum(); i++) {
		Cell cell = row.getCell(i);
		if (null != cell) {
			String str = this.getCellStringValue(cell);
			if (StringUtils.isNotEmpty(str)) {
				if (nameItems.contains(str)) {
					this.nameColumn = i;
				} else if (uniqueItems.contains(str)) {
					this.uniqueColumn = i;
				} else if (employeeItems.contains(str)) {
					this.employeeColumn = i;
				} else if (mobileItems.contains(str)) {
					this.mobileColumn = i;
				} else if (mailItems.contains(str)) {
					this.mailColumn = i;
				} else if (genderTypeItems.contains(str)) {
					this.genderTypeColumn = i;
				} else {
					Matcher matcher = attributePattern.matcher(str);
					if (matcher.matches()) {
						String attribute = matcher.group(1);
						this.attributes.put(attribute, new Integer(i));
					}
				}
			}
		}
	}
}
 
Example 15
Source File: ExcelExportOfTemplateUtil.java    From easypoi with Apache License 2.0 5 votes vote down vote up
/**
 * 先判断删除,省得影响效率
 * @param sheet
 * @param map
 * @throws Exception 
 */
private void deleteCell(Sheet sheet, Map<String, Object> map) throws Exception {
    Row row = null;
    Cell cell = null;
    int index = 0;
    while (index <= sheet.getLastRowNum()) {
        row = sheet.getRow(index++);
        if (row == null) {
            continue;
        }
        for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
            cell = row.getCell(i);
            if (row.getCell(i) != null
                && (cell.getCellType() == Cell.CELL_TYPE_STRING || cell.getCellType() == Cell.CELL_TYPE_NUMERIC)) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
                String text = cell.getStringCellValue();
                if (text.contains(IF_DELETE)) {
                    if (Boolean.valueOf(eval(
                        text.substring(text.indexOf(START_STR) + 2, text.indexOf(END_STR))
                            .trim(), map).toString())) {
                        PoiSheetUtility.deleteColumn(sheet, i);
                    }
                    cell.setCellValue("");
                }
            }
        }
    }
}
 
Example 16
Source File: ExcelToHtmlServer.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
private void printSheetContent(Sheet sheet) {
	// printColumnHeads(sheet);
	MergedRegionHelper mergedRegionHelper = new MergedRegionHelper(sheet);
	CellValueHelper cellValueHelper = new CellValueHelper(wb, cssRandom);
	out.format("<tbody>%n");
	Iterator<Row> rows = sheet.rowIterator();
	int rowIndex = 1;
	while (rows.hasNext()) {
		Row row = rows.next();
		out.format("  <tr style='height:%spx;'>%n", row.getHeight() / 15);
		// out.format("    <td class='%s'>%d</td>%n", ROW_HEAD_CLASS,
		// row.getRowNum() + 1);
		for (int i = firstColumn; i < endColumn; i++) {
			if (mergedRegionHelper.isNeedCreate(rowIndex, i)) {
				String content = "&nbsp;";
				CellStyle style = null;
				if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
					Cell cell = row.getCell(i);
					if (cell != null) {
						style = cell.getCellStyle();
						content = cellValueHelper.getHtmlValue(cell);
					}
				}
				if (mergedRegionHelper.isMergedRegion(rowIndex, i)) {
					Integer[] rowAndColSpan = mergedRegionHelper.getRowAndColSpan(rowIndex, i);
					out.format("    <td rowspan='%s' colspan='%s' class='%s' >%s</td>%n", rowAndColSpan[0], rowAndColSpan[1], styleName(style), content);
				} else {
					out.format("    <td class='%s'>%s</td>%n", styleName(style), content);
				}
			}

		}
		out.format("  </tr>%n");
		rowIndex++;
	}
	out.format("</tbody>%n");
}
 
Example 17
Source File: ExcelToHtmlServer.java    From easypoi with Apache License 2.0 5 votes vote down vote up
private void printSheetContent(Sheet sheet) {
    //printColumnHeads(sheet);
    MergedRegionHelper mergedRegionHelper = new MergedRegionHelper(sheet);
    CellValueHelper cellValueHelper = new CellValueHelper(wb, cssRandom);
    out.format("<tbody>%n");
    Iterator<Row> rows = sheet.rowIterator();
    int rowIndex = 1;
    while (rows.hasNext()) {
        Row row = rows.next();
        out.format("  <tr style='height:%spx;'>%n", row.getHeight() / 15);
        //out.format("    <td class='%s'>%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1);
        for (int i = firstColumn; i < endColumn; i++) {
            if (mergedRegionHelper.isNeedCreate(rowIndex, i)) {
                String content = "&nbsp;";
                CellStyle style = null;
                if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
                    Cell cell = row.getCell(i);
                    if (cell != null) {
                        style = cell.getCellStyle();
                        content = cellValueHelper.getHtmlValue(cell);
                    }
                }
                if (mergedRegionHelper.isMergedRegion(rowIndex, i)) {
                    Integer[] rowAndColSpan = mergedRegionHelper.getRowAndColSpan(rowIndex, i);
                    out.format("    <td rowspan='%s' colspan='%s' class='%s' >%s</td>%n",
                        rowAndColSpan[0], rowAndColSpan[1], styleName(style), content);
                } else {
                    out.format("    <td class='%s'>%s</td>%n", styleName(style), content);
                }
            }

        }
        out.format("  </tr>%n");
        rowIndex++;
    }
    out.format("</tbody>%n");
}
 
Example 18
Source File: FileDatasetXlsDataReader.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean checkIfRowIsEmpty(Row row) {
	if (row == null) {
		return true;
	}
	if (row.getLastCellNum() <= 0) {
		return true;
	}
	for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
		Cell cell = row.getCell(cellNum);
		if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK && org.apache.commons.lang.StringUtils.isNotBlank(cell.toString())) {
			return false;
		}
	}
	return true;
}
 
Example 19
Source File: ExcelToHtmlServer.java    From autopoi with Apache License 2.0 5 votes vote down vote up
private void printSheetContent(Sheet sheet) {
	// printColumnHeads(sheet);
	MergedRegionHelper mergedRegionHelper = new MergedRegionHelper(sheet);
	CellValueHelper cellValueHelper = new CellValueHelper(wb, cssRandom);
	out.format("<tbody>%n");
	Iterator<Row> rows = sheet.rowIterator();
	int rowIndex = 1;
	while (rows.hasNext()) {
		Row row = rows.next();
		out.format("  <tr style='height:%spx;'>%n", row.getHeight() / 15);
		// out.format("    <td class='%s'>%d</td>%n", ROW_HEAD_CLASS,
		// row.getRowNum() + 1);
		for (int i = firstColumn; i < endColumn; i++) {
			if (mergedRegionHelper.isNeedCreate(rowIndex, i)) {
				String content = "&nbsp;";
				CellStyle style = null;
				if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
					Cell cell = row.getCell(i);
					if (cell != null) {
						style = cell.getCellStyle();
						content = cellValueHelper.getHtmlValue(cell);
					}
				}
				if (mergedRegionHelper.isMergedRegion(rowIndex, i)) {
					Integer[] rowAndColSpan = mergedRegionHelper.getRowAndColSpan(rowIndex, i);
					out.format("    <td rowspan='%s' colspan='%s' class='%s' >%s</td>%n", rowAndColSpan[0], rowAndColSpan[1], styleName(style), content);
				} else {
					out.format("    <td class='%s'>%s</td>%n", styleName(style), content);
				}
			}

		}
		out.format("  </tr>%n");
		rowIndex++;
	}
	out.format("</tbody>%n");
}
 
Example 20
Source File: ExcelImportProecess.java    From youkefu with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private static boolean isRowEmpty(Row row) {
	for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
		Cell cell = row.getCell(c);
		if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK)
			return false;
	}
	return true;
}