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

The following examples show how to use org.apache.poi.ss.usermodel.Row#getSheet() . 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: HSSFSheet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove a row from this sheet.  All cells contained in the row are removed as well
 *
 * @param row representing a row to remove.
 */
@Override
public void removeRow(Row row) {
    HSSFRow hrow = (HSSFRow) row;
    if (row.getSheet() != this) {
        throw new IllegalArgumentException("Specified row does not belong to this sheet");
    }
    for (Cell cell : row) {
        HSSFCell xcell = (HSSFCell) cell;
        if (xcell.isPartOfArrayFormulaGroup()) {
            String msg = "Row[rownum=" + row.getRowNum() + "] contains cell(s) included in a multi-cell array formula. You cannot change part of an array.";
            xcell.notifyArrayFormulaChanging(msg);
        }
    }

    if (_rows.size() > 0) {
        Integer key = Integer.valueOf(row.getRowNum());
        HSSFRow removedRow = _rows.remove(key);
        if (removedRow != row) {
            //should not happen if the input argument is valid
            throw new IllegalArgumentException("Specified row does not belong to this sheet");
        }
        if (hrow.getRowNum() == getLastRowNum()) {
            _lastrow = findLastRow(_lastrow);
        }
        if (hrow.getRowNum() == getFirstRowNum()) {
            _firstrow = findFirstRow(_firstrow);
        }
        _sheet.removeRow(hrow.getRowRecord());
    }
}
 
Example 2
Source File: AbstractRowDirective.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public boolean matchEnd(T model, Row row){
	Row lastRow = row;
	Sheet sheet = row.getSheet();
	while(!isMatchEnd(model, lastRow)){
		logger.info("find diretive[{}] list row: {}", getName(), lastRow.getRowNum());
		model.addMatchRow(lastRow);
		
		if(lastRow.getRowNum()+1>sheet.getPhysicalNumberOfRows())
			throw new ExcelException("not end tag matched for: " + model.getDirectiveStart());
		
		lastRow = row.getSheet().getRow(lastRow.getRowNum()+1);
	}
	return true;
}
 
Example 3
Source File: ExcelUtils.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public static void removeCellRange(Row row){
	Sheet sheet = row.getSheet();
	for(Cell cell : row){
		for(int i=0; i< sheet.getNumMergedRegions(); i++){
			CellRangeAddress cr = sheet.getMergedRegion(i);
			if(cr.getFirstRow()==row.getRowNum() && cr.getFirstColumn()==cell.getColumnIndex()){
				sheet.removeMergedRegion(i);
			}
		}
	}
}
 
Example 4
Source File: ForeachRowDirective.java    From onetwo with Apache License 2.0 4 votes vote down vote up
@Override
	public void excecute(ETRowContext rowContext, ForeachRowDirectiveModel forModel, ExcelTemplateValueProvider provider){
		Row endRow = forModel.getEndRow();
		Sheet sheet = endRow.getSheet();
		List<ForeachRowInfo> foreachRows = forModel.getMatchRows();

		Object listObject = provider.parseValue(forModel.getDataSource());
		if(!ExcelUtils.isMultiple(listObject)){
			throw new ExcelException("the ["+forModel.getDataSource()+"] must be a Collection or Array object");
		}

		List<?> datalist = ExcelUtils.tolist(listObject);
		forModel.setDataList(datalist);
		if(datalist.isEmpty()){
			return ;
		}
		
		int rownumb = forModel.getStartRow().getRowNum();
		//add row space
		ExcelUtils.addRow(sheet, rownumb, datalist.size()*forModel.getMatchRows().size());
		int dataIndex = 0;

		ETemplateContext templateContext = provider.getTemplateContext();
		//TODO 覆盖了外部变量,以后修改
		for(Object data : datalist){
			templateContext.put(forModel.getItemVar(), data);
			templateContext.put(forModel.getIndexVar(), dataIndex);
			try {
				Row firstRow = null;
//				logger.info("sheet.getPhysicalNumberOfRows(): {}", sheet.getPhysicalNumberOfRows());
				for(ForeachRowInfo repeateRow : foreachRows){
					endRow = generateRow(repeateRow, provider, rownumb);
					processCommonRow(endRow, provider);
					if(firstRow==null){
						firstRow = endRow;
					}
					
					if(provider.isDebug()){
						logger.info("repeateRow: {}", repeateRow.getOriginRownum());
						logger.info("create row {} : {} ", rownumb);
						logger.info("firstRow: {}", firstRow.getRowNum());
					}
					rownumb++;
				}
//				logger.info("sheet.getPhysicalNumberOfRows(): {}", sheet.getPhysicalNumberOfRows());
				
//				rowContext.getSheetContext().getEngineer().parseRow(rowContext.getSheetContext(), firstRow);
			} finally{
				//clear
				templateContext.remove(forModel.getItemVar());
				templateContext.remove(forModel.getIndexVar());
			}
			dataIndex++;
		}
		
//		return endRow.getRowNum();
	}