Java Code Examples for org.apache.poi.ss.usermodel.Cell#getRowIndex()

The following examples show how to use org.apache.poi.ss.usermodel.Cell#getRowIndex() . 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: CellRangeTester.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
protected void validateCellRange(Matcher matcher, Cell cell) {
	int desiredR1 = Integer.parseInt( matcher.group(1) );
	int desiredC1 = Integer.parseInt( matcher.group(2) );
	int desiredR2 = Integer.parseInt( matcher.group(3) );
	int desiredC2 = Integer.parseInt( matcher.group(4) );
	
	int actualR1 = cell.getRowIndex() + 1;
	int actualC1 = cell.getColumnIndex() + 1;
	int actualR2 = actualR1;
	int actualC2 = actualC1;
	
	for( int i = 0; i < cell.getSheet().getNumMergedRegions(); ++i) {
		CellRangeAddress cra = cell.getSheet().getMergedRegion(i);
		if( ( cra.getFirstRow() == cell.getRowIndex() ) && ( cra.getFirstColumn() == cell.getColumnIndex() ) ) {
			assertEquals( actualR1, actualR2 );
			assertEquals( actualC1, actualC2 );
			actualR2 = cra.getLastRow() + 1;
			actualC2 = cra.getLastColumn() + 1;
		}
	}
	assertEquals( desiredR1, actualR1 );
	assertEquals( desiredC1, actualC1 );
	assertEquals( desiredR2, actualR2 );
	assertEquals( desiredC2, actualC2 );
}
 
Example 2
Source File: IterateTablesProcessor.java    From xlsmapper with Apache License 2.0 5 votes vote down vote up
private void saveSingleLabelledArrayCell(final Sheet sheet, final Object tableObj, final Cell headerCell,
        final Configuration config, final SavingWorkObject work) throws XlsMapperException {

    final LabelledArrayCellsProcessor labelledArrayCellProcessor =
            (LabelledArrayCellsProcessor) config.getFieldProcessorRegistry().getProcessor(XlsLabelledArrayCells.class);

    final List<FieldAccessor> properties = FieldAccessorUtils.getPropertiesWithAnnotation(
            tableObj.getClass(), work.getAnnoReader(), XlsLabelledArrayCells.class)
            .stream()
            .filter(p -> p.isWritable())
            .collect(Collectors.toList());

    for(FieldAccessor property : properties) {

        final XlsLabelledArrayCells anno = property.getAnnotationNullable(XlsLabelledArrayCells.class);

        Cell titleCell = null;
        try {
            titleCell = CellFinder.query(sheet, anno.label(), config)
                    .startPosition(headerCell)
                    .excludeStartPosition(true)
                    .findWhenNotFoundException();

        } catch (CellNotFoundException e) {
            if (anno.optional()) {
                continue;
            } else {
                throw e;
            }
        }

        final XlsLabelledArrayCells labelledCell = new XlsLabelledArrayCellsForIterateTable(
                anno, titleCell.getRowIndex(), titleCell.getColumnIndex());

        labelledArrayCellProcessor.saveProcess(sheet, tableObj, labelledCell, property, config, work);
    }

}
 
Example 3
Source File: POIUtils.java    From xlsmapper with Apache License 2.0 5 votes vote down vote up
/**
 * 結合を考慮してセルの罫線(下部)を取得する。
 *
 * @param cell セル
 * @return {@literal BorderStyle}
 * @throws IllegalArgumentException {@literal cell is null.}
 */
public static BorderStyle getBorderBottom(final Cell cell) {

    ArgUtils.notNull(cell, "cell");

    final Sheet sheet = cell.getSheet();
    CellRangeAddress mergedRegion = getMergedRegion(sheet, cell.getRowIndex(), cell.getColumnIndex());

    final Cell target;
    if(mergedRegion == null) {
        // 結合されていない場合
        target = cell;

    } else {
        if(mergedRegion.getLastRow() == cell.getRowIndex()) {
            // 引数のCellが下部のセルの場合
            target = cell;
        } else {
            target = getCell(sheet, cell.getColumnIndex(), mergedRegion.getLastRow());
        }

    }

    final CellStyle style = target.getCellStyle();
    if(style == null) {
        return BorderStyle.NONE;
    } else {
        return style.getBorderBottomEnum();
    }

}
 
Example 4
Source File: BeanRowMapper.java    From onetwo with Apache License 2.0 5 votes vote down vote up
/***
	 * 如果cellValue为null,则尝试再次自动处理
	 * @param bw
	 * @param name
	 * @param cell
	 * @param cellValue
	 */
	protected void setBeanProperty(BeanWrapper bw, String name, Cell cell, Object cellValue){
		if(ExcelUtils.isBlank(name))
			return ;
		Object value = null;
		try {
			if(cellValue==null && autoGetCellValue){
				CellValueConvertor convertor = this.getCellValueConvertor(name);
				if(convertor==null){
					Class<?> type = bw.getPropertyType(name);
					if(type!=null)
						convertor = this.getCellValueConvertor(type.getSimpleName());
				}
				if(convertor!=null){
					value = convertor.convert(cell);
				}else{
//					value = ExcelUtils.getCellValue(cell);
					value = ExcelUtils.getCellValue(cell, convertCellTypeAsString);
				}
			}else{
				value = cellValue;
			}
			/*if(value!=null && (!String.class.isInstance(value) || !ExcelUtils.isBlank((String)value)))
				bw.setPropertyValue(name, value);*/
			this.setBeanProperty(bw, name, value);
		} catch (Exception e) {
			throw new ExcelException("row:"+cell.getRowIndex()+",set property["+name+"] error, value: "+value, e);
		}
	}
 
Example 5
Source File: IterateTablesProcessor.java    From xlsmapper with Apache License 2.0 5 votes vote down vote up
private void saveSingleLabelledCell(final Sheet sheet, final Object tableObj, final Cell headerCell,
        final Configuration config, final SavingWorkObject work) throws XlsMapperException {

    final LabelledCellProcessor labelledCellProcessor =
            (LabelledCellProcessor) config.getFieldProcessorRegistry().getProcessor(XlsLabelledCell.class);

    final List<FieldAccessor> properties = FieldAccessorUtils.getPropertiesWithAnnotation(
            tableObj.getClass(), work.getAnnoReader(), XlsLabelledCell.class)
            .stream()
            .filter(p -> p.isWritable())
            .collect(Collectors.toList());

    for(FieldAccessor property : properties) {

        final XlsLabelledCell anno = property.getAnnotationNullable(XlsLabelledCell.class);

        Cell titleCell = null;
        try {
            titleCell = CellFinder.query(sheet, anno.label(), config)
                    .startPosition(headerCell)
                    .excludeStartPosition(true)
                    .findWhenNotFoundException();

        } catch (CellNotFoundException e) {
            if (anno.optional()) {
                continue;
            } else {
                throw e;
            }
        }

        final XlsLabelledCell labelledCell = new XlsLabelledCellForIterateTable(
                anno, titleCell.getRowIndex(), titleCell.getColumnIndex());

        labelledCellProcessor.saveProcess(sheet, tableObj, labelledCell, property, config, work);
    }

}
 
Example 6
Source File: AbstractExcelExtractor.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
public Topic getCellTopic(Cell cell, TopicMap tm) throws TopicMapException {
    String cellIdentifier = null;
    switch(CELL_TOPIC_IS_BASED_ON) {
        case CELL_VALUE: {
            cellIdentifier = getCellValueAsString(cell);
            break;
        }
        case CELL_SHEET_AND_LOCATION: {
            Sheet sheet = cell.getSheet();
            String sheetName = sheet.getSheetName();
            cellIdentifier = sheetName+"-"+cell.getColumnIndex()+"-"+cell.getRowIndex();
            break;
        }
        case CELL_LOCATION: {
            cellIdentifier = cell.getColumnIndex()+"-"+cell.getRowIndex();
            break;
        }
        case CELL_HASH: {
            cellIdentifier = Integer.toString(cell.hashCode());
            break;
        }
    }
    if(cellIdentifier != null) {
        String si = EXCEL_CELL_SI_PREFIX +"/"+ urlEncode(cellIdentifier);
        Topic cellTopic = getOrCreateTopic(tm, si, cellIdentifier);
        cellTopic.addType(getCellTypeTopic(tm));
        return cellTopic;
    }
    return null;
}
 
Example 7
Source File: IterateTablesProcessor.java    From xlsmapper with Apache License 2.0 5 votes vote down vote up
private void loadMultipleVerticalTableCell(final Sheet sheet, final Object tableObj,
        final Cell headerCell, final XlsIterateTables iterateTablesAnno,
        final Configuration config, final LoadingWorkObject work) throws XlsMapperException {

    int headerColumn = headerCell.getColumnIndex();
    int headerRow = headerCell.getRowIndex();

    if (iterateTablesAnno.bottom() > 0) {
        // if positive value set to bottom(), row index of table header move
        headerRow += iterateTablesAnno.bottom();
    }

    final VerticalRecordsProcessor processor =
            (VerticalRecordsProcessor) config.getFieldProcessorRegistry().getProcessor(XlsVerticalRecords.class);

    final List<FieldAccessor> properties = FieldAccessorUtils.getPropertiesWithAnnotation(
            tableObj.getClass(), work.getAnnoReader(), XlsVerticalRecords.class)
            .stream()
            .filter(p -> p.isReadable())
            .collect(Collectors.toList());

    final List<FieldAccessorProxy> accessorProxies = new ArrayList<>();
    for(FieldAccessor property : properties) {
        final XlsVerticalRecords anno = property.getAnnotationNullable(XlsVerticalRecords.class);

        if(iterateTablesAnno.tableLabel().equals(anno.tableLabel())) {

            final XlsVerticalRecords recordsAnno = new XlsVerticalRecordsForIterateTables(anno, headerColumn, headerRow);
            accessorProxies.add(new FieldAccessorProxy(recordsAnno, processor, property));
        }
    }

    // 順番を並び替えて読み込み処理を実行する
    Collections.sort(accessorProxies, new FieldAccessorProxyComparator());
    for(FieldAccessorProxy adaptorProxy : accessorProxies) {
        adaptorProxy.loadProcess(sheet, tableObj, config, work);
    }

}
 
Example 8
Source File: IterateTablesProcessor.java    From xlsmapper with Apache License 2.0 5 votes vote down vote up
private void loadMultipleHorizontalTableCell(final Sheet sheet, final Object tableObj,
        final Cell headerCell, final XlsIterateTables iterateTablesAnno,
        final Configuration config, final LoadingWorkObject work) throws XlsMapperException {

    int headerColumn = headerCell.getColumnIndex();
    int headerRow = headerCell.getRowIndex();

    if (iterateTablesAnno.bottom() > 0) {
        // if positive value set to bottom(), row index of table header move
        headerRow += iterateTablesAnno.bottom();
    }

    final HorizontalRecordsProcessor processor =
            (HorizontalRecordsProcessor) config.getFieldProcessorRegistry().getProcessor(XlsHorizontalRecords.class);

    final List<FieldAccessor> properties = FieldAccessorUtils.getPropertiesWithAnnotation(
            tableObj.getClass(), work.getAnnoReader(), XlsHorizontalRecords.class)
            .stream()
            .filter(p -> p.isReadable())
            .collect(Collectors.toList());

    final List<FieldAccessorProxy> accessorProxies = new ArrayList<>();
    for(FieldAccessor property : properties) {
        final XlsHorizontalRecords anno = property.getAnnotationNullable(XlsHorizontalRecords.class);

        if(iterateTablesAnno.tableLabel().equals(anno.tableLabel())) {

            final XlsHorizontalRecords recordsAnno = new XlsHorizontalRecordsForIterateTables(anno, headerColumn, headerRow);
            accessorProxies.add(new FieldAccessorProxy(recordsAnno, processor, property));
        }
    }

    // 順番を並び替えて読み込み処理を実行する
    Collections.sort(accessorProxies, new FieldAccessorProxyComparator());
    for(FieldAccessorProxy adaptorProxy : accessorProxies) {
        adaptorProxy.loadProcess(sheet, tableObj, config, work);
    }

}
 
Example 9
Source File: IterateTablesProcessor.java    From xlsmapper with Apache License 2.0 5 votes vote down vote up
/**
 * XlsLabelledCommentによる処理する。
 * @param sheet
 * @param tableObj
 * @param headerCell
 * @param config
 * @throws XlsMapperException
 */
private void loadSingleLabelledComment(final Sheet sheet, final Object tableObj,
        final Cell headerCell, final Configuration config, final LoadingWorkObject work) throws XlsMapperException {

    final LabelledCommentProcessor labelledCommentProcessor =
            (LabelledCommentProcessor) config.getFieldProcessorRegistry().getProcessor(XlsLabelledComment.class);

    final List<FieldAccessor> properties = FieldAccessorUtils.getPropertiesWithAnnotation(
            tableObj.getClass(), work.getAnnoReader(), XlsLabelledComment.class)
            .stream()
            .filter(p -> p.isReadable())
            .collect(Collectors.toList());

    for(FieldAccessor property : properties) {
        final XlsLabelledComment anno = property.getAnnotationNullable(XlsLabelledComment.class);

        Cell titleCell = null;
        try {
            titleCell = CellFinder.query(sheet, anno.label(), config)
                    .startPosition(headerCell)
                    .excludeStartPosition(true)
                    .findWhenNotFoundException();

        } catch (CellNotFoundException e) {
            if (anno.optional()) {
                continue;
            } else {
                throw e;
            }
        }

        final XlsLabelledComment labelledCell = new XlsLabelledCommentForIterateTable(
                anno, titleCell.getRowIndex(), titleCell.getColumnIndex());

        labelledCommentProcessor.loadProcess(sheet, tableObj, labelledCell, property, config, work);
    }
}
 
Example 10
Source File: IterateTablesProcessor.java    From xlsmapper with Apache License 2.0 5 votes vote down vote up
/**
 * XlsLabelledCellによる処理する。
 * @param sheet
 * @param tableObj
 * @param headerCell
 * @param config
 * @throws XlsMapperException
 */
private void loadSingleLabelledCell(final Sheet sheet, final Object tableObj,
        final Cell headerCell, final Configuration config, final LoadingWorkObject work) throws XlsMapperException {

    final LabelledCellProcessor labelledCellProcessor =
            (LabelledCellProcessor) config.getFieldProcessorRegistry().getProcessor(XlsLabelledCell.class);

    final List<FieldAccessor> properties = FieldAccessorUtils.getPropertiesWithAnnotation(
            tableObj.getClass(), work.getAnnoReader(), XlsLabelledCell.class)
            .stream()
            .filter(p -> p.isReadable())
            .collect(Collectors.toList());

    for(FieldAccessor property : properties) {
        final XlsLabelledCell anno = property.getAnnotationNullable(XlsLabelledCell.class);

        Cell titleCell = null;
        try {
            titleCell = CellFinder.query(sheet, anno.label(), config)
                    .startPosition(headerCell)
                    .excludeStartPosition(true)
                    .findWhenNotFoundException();

        } catch (CellNotFoundException e) {
            if (anno.optional()) {
                continue;
            } else {
                throw e;
            }
        }

        final XlsLabelledCell labelledCell = new XlsLabelledCellForIterateTable(
                anno, titleCell.getRowIndex(), titleCell.getColumnIndex());

        labelledCellProcessor.loadProcess(sheet, tableObj, labelledCell, property, config, work);
    }
}
 
Example 11
Source File: IterateTablesProcessor.java    From xlsmapper with Apache License 2.0 5 votes vote down vote up
/**
 * XlsLabelledArrayCellを処理する。
 * @param sheet
 * @param tableObj
 * @param headerCell
 * @param config
 * @throws XlsMapperException
 */
private void loadSingleLabelledArrayCell(final Sheet sheet, final Object tableObj,
        final Cell headerCell, final Configuration config, final LoadingWorkObject work) throws XlsMapperException {

    final LabelledArrayCellsProcessor labelledArrayCellProcessor =
            (LabelledArrayCellsProcessor) config.getFieldProcessorRegistry().getProcessor(XlsLabelledArrayCells.class);

    final List<FieldAccessor> properties = FieldAccessorUtils.getPropertiesWithAnnotation(
            tableObj.getClass(), work.getAnnoReader(), XlsLabelledArrayCells.class)
            .stream()
            .filter(p -> p.isReadable())
            .collect(Collectors.toList());

    for(FieldAccessor property : properties) {
        final XlsLabelledArrayCells anno = property.getAnnotationNullable(XlsLabelledArrayCells.class);

        Cell titleCell = null;
        try {
            titleCell = CellFinder.query(sheet, anno.label(), config)
                    .startPosition(headerCell)
                    .excludeStartPosition(true)
                    .findWhenNotFoundException();

        } catch (CellNotFoundException e) {
            if (anno.optional()) {
                continue;
            } else {
                throw e;
            }
        }

        final XlsLabelledArrayCells wrapAnno = new XlsLabelledArrayCellsForIterateTable(
                anno, titleCell.getRowIndex(), titleCell.getColumnIndex());

        labelledArrayCellProcessor.loadProcess(sheet, tableObj, wrapAnno, property, config, work);
    }
}
 
Example 12
Source File: HorizontalRecordsProcessor.java    From xlsmapper with Apache License 2.0 4 votes vote down vote up
/**
 * 上部のセルと同じ値の場合マージする
 * @param currentCell
 * @param sheet
 * @param mergedRanges
 * @param config
 * @return
 */
private boolean processSavingMergedCell(final Cell currentCell, final Sheet sheet,
        final List<CellRangeAddress> mergedRanges, final Configuration config) {

    final int row = currentCell.getRowIndex();
    final int column = currentCell.getColumnIndex();

    if(row <= 0) {
        return false;
    }

    // 上のセルと比較する
    final String value = POIUtils.getCellContents(currentCell, config.getCellFormatter());
    String upperValue = POIUtils.getCellContents(POIUtils.getCell(sheet, column, row-1), config.getCellFormatter());

    // 結合されている場合、結合の先頭セルを取得する
    int startRow = row - 1;
    CellRangeAddress currentMergedRange = null;
    for(CellRangeAddress range : mergedRanges) {
        // 列が範囲外の場合
        if((range.getFirstColumn() > column) || (column > range.getLastColumn())) {
            continue;
        }

        // 行が範囲外の場合
        if((range.getFirstRow() > startRow) || (startRow > range.getLastRow())) {
            continue;
        }

        upperValue = POIUtils.getCellContents(POIUtils.getCell(sheet, column, range.getFirstRow()), config.getCellFormatter());
        currentMergedRange = range;
        break;
    }

    if(!value.equals(upperValue)) {
        // 値が異なる場合は結合しない
        return false;
    }

    // 既に結合済みの場合は一端解除する
    if(currentMergedRange != null) {
        startRow = currentMergedRange.getFirstRow();
        mergedRanges.remove(currentMergedRange);
        POIUtils.removeMergedRange(sheet, currentMergedRange);
    }

    final CellRangeAddress newRange = POIUtils.mergeCells(sheet, column, startRow, column, row);
    mergedRanges.add(newRange);
    return true;

}
 
Example 13
Source File: IterateTablesProcessor.java    From xlsmapper with Apache License 2.0 4 votes vote down vote up
private void saveMultipleHorizontalTableCell(final Sheet sheet, final Object tableObj, final Cell headerCell,
        final XlsIterateTables iterateTables, final Configuration config, final SavingWorkObject work) throws XlsMapperException {

    final int headerColumn = headerCell.getColumnIndex();
    int headerRow = headerCell.getRowIndex();

    if (iterateTables.bottom() > 0) {
        // if positive value set to bottom(), row index of table header move
        headerRow += iterateTables.bottom();
    }

    final HorizontalRecordsProcessor processor =
            (HorizontalRecordsProcessor) config.getFieldProcessorRegistry().getProcessor(XlsHorizontalRecords.class);

    final List<FieldAccessor> properties = FieldAccessorUtils.getPropertiesWithAnnotation(
            tableObj.getClass(), work.getAnnoReader(), XlsHorizontalRecords.class)
            .stream()
            .filter(p -> p.isWritable())
            .collect(Collectors.toList());

    final List<FieldAccessorProxy> accessorProxies = new ArrayList<>();
    for(FieldAccessor property : properties) {

        final XlsHorizontalRecords anno = property.getAnnotationNullable(XlsHorizontalRecords.class);

        // 処理対象と同じテーブルラベルのとき、マッピングを実行する。
        if(iterateTables.tableLabel().equals(anno.tableLabel())) {
            final XlsHorizontalRecords recordsAnno = new XlsHorizontalRecordsForIterateTables(anno, headerColumn, headerRow);
            accessorProxies.add(new FieldAccessorProxy(recordsAnno, processor, property));

        }

    }

    // 順番を並び替えて保存処理を実行する
    Collections.sort(accessorProxies, new FieldAccessorProxyComparator());
    for(FieldAccessorProxy adaptorProxy : accessorProxies) {
        adaptorProxy.saveProcess(sheet, tableObj, config, work);
    }

}
 
Example 14
Source File: IterateTablesProcessor.java    From xlsmapper with Apache License 2.0 4 votes vote down vote up
private void saveMultipleVerticalTableCell(final Sheet sheet, final Object tableObj, final Cell headerCell,
        final XlsIterateTables iterateTablesAnno, final Configuration config, final SavingWorkObject work) throws XlsMapperException {

    int headerColumn = headerCell.getColumnIndex();
    int headerRow = headerCell.getRowIndex();

    if (iterateTablesAnno.bottom() > 0) {
        // if positive value set to bottom(), row index of table header move
        headerRow += iterateTablesAnno.bottom();
    }

    final VerticalRecordsProcessor processor =
            (VerticalRecordsProcessor) config.getFieldProcessorRegistry().getProcessor(XlsVerticalRecords.class);

    final List<FieldAccessor> properties = FieldAccessorUtils.getPropertiesWithAnnotation(
            tableObj.getClass(), work.getAnnoReader(), XlsVerticalRecords.class)
            .stream()
            .filter(p -> p.isWritable())
            .collect(Collectors.toList());

    final List<FieldAccessorProxy> accessorProxies = new ArrayList<>();
    for(FieldAccessor property : properties) {

        final XlsVerticalRecords anno = property.getAnnotationNullable(XlsVerticalRecords.class);

        // 処理対象と同じテーブルラベルのとき、マッピングを実行する。
        if(iterateTablesAnno.tableLabel().equals(anno.tableLabel())) {
            final XlsVerticalRecords recordsAnno = new XlsVerticalRecordsForIterateTables(anno, headerColumn, headerRow);
            accessorProxies.add(new FieldAccessorProxy(recordsAnno, processor, property));

        }

    }

    // 順番を並び替えて保存処理を実行する
    Collections.sort(accessorProxies, new FieldAccessorProxyComparator());
    for(FieldAccessorProxy adaptorProxy : accessorProxies) {
        adaptorProxy.saveProcess(sheet, tableObj, config, work);
    }

}
 
Example 15
Source File: ConditionalFormattingEvaluator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static CellReference getRef(Cell cell) {
    return new CellReference(cell.getSheet().getSheetName(), cell.getRowIndex(), cell.getColumnIndex(), false, false);
}
 
Example 16
Source File: HorizontalRecordsProcessor.java    From xlsmapper with Apache License 2.0 4 votes vote down vote up
/**
 * 表の開始位置(見出し)の位置情報を取得する。
 *
 * @param sheet
 * @param anno
 * @param accessor
 * @param config
 * @return 表の開始位置。指定したラベルが見つからない場合、設定によりnullを返す。
 * @throws AnnotationInvalidException アノテーションの値が不正で、表の開始位置が位置が見つからない場合。
 * @throws CellNotFoundException 指定したラベルが見つからない場合。
 */
private Optional<CellPosition> getHeaderPosition(final Sheet sheet, final XlsHorizontalRecords anno,
        final FieldAccessor accessor, final Configuration config) throws AnnotationInvalidException, CellNotFoundException {

    if(Utils.isNotEmpty(anno.headerAddress())) {
        try {
            return Optional.of(CellPosition.of(anno.headerAddress()));
        } catch(IllegalArgumentException e) {
            throw new AnnotationInvalidException(anno, MessageBuilder.create("anno.attr.invalidAddress")
                    .var("property", accessor.getNameWithClass())
                    .varWithAnno("anno", XlsHorizontalRecords.class)
                    .var("attrName", "headerAddress")
                    .var("attrValue", anno.headerAddress())
                    .format());

        }

    } else if(Utils.isNotEmpty(anno.tableLabel())) {
        try {
            final Cell labelCell = CellFinder.query(sheet, anno.tableLabel(), config).findWhenNotFoundException();
            int initColumn = labelCell.getColumnIndex();
            int initRow = labelCell.getRowIndex() + anno.bottom();

            return Optional.of(CellPosition.of(initRow, initColumn));

        } catch(CellNotFoundException ex) {
            if(anno.optional()) {
                return Optional.empty();
            } else {
                throw ex;
            }
        }

    } else {
        // column, rowのアドレスを直接指定の場合
        if(anno.headerRow() < 0) {
            throw  new AnnotationInvalidException(anno, MessageBuilder.create("anno.attr.min")
                    .var("property", accessor.getNameWithClass())
                    .varWithAnno("anno", XlsHorizontalRecords.class)
                    .var("attrName", "headerRow")
                    .var("attrValue", anno.headerRow())
                    .var("min", 0)
                    .format());
        }

        if(anno.headerColumn() < 0) {
            throw new AnnotationInvalidException(anno, MessageBuilder.create("anno.attr.min")
                    .var("property", accessor.getNameWithClass())
                    .varWithAnno("anno", XlsHorizontalRecords.class)
                    .var("attrName", "column")
                    .var("attrValue", anno.headerColumn())
                    .var("min", 0)
                    .format());

        }

        return Optional.of(CellPosition.of(anno.headerRow(), anno.headerColumn()));
    }

}
 
Example 17
Source File: VerticalRecordsProcessor.java    From xlsmapper with Apache License 2.0 4 votes vote down vote up
/**
 * 上部のセルと同じ値の場合マージする
 * @param currentCell
 * @param sheet
 * @param mergedRanges
 * @return
 */
private boolean processSavingMergedCell(final Cell currentCell, final Sheet sheet,
        final List<CellRangeAddress> mergedRanges, final Configuration config) {

    final int row = currentCell.getRowIndex();
    final int column = currentCell.getColumnIndex();

    if(column <= 0) {
        return false;
    }

    // 上のセルと比較する
    final String value = POIUtils.getCellContents(currentCell, config.getCellFormatter());
    String upperValue = POIUtils.getCellContents(POIUtils.getCell(sheet, column-1, row), config.getCellFormatter());

    // 結合されている場合、結合の先頭セルを取得する
    int startColumn = column - 1;
    CellRangeAddress currentMergedRange = null;
    for(CellRangeAddress range : mergedRanges) {
        // 列が範囲外の場合
        if((range.getFirstColumn() > startColumn) || (startColumn > range.getLastColumn())) {
            continue;
        }

        // 行が範囲外の場合
        if((range.getFirstRow() > row) || (row > range.getLastRow())) {
            continue;
        }

        upperValue = POIUtils.getCellContents(POIUtils.getCell(sheet, range.getFirstColumn(), row), config.getCellFormatter());
        currentMergedRange = range;
        break;
    }

    if(!value.equals(upperValue)) {
        // 値が異なる場合は結合しない
        return false;
    }

    // 既に結合済みの場合は一端解除する
    if(currentMergedRange != null) {
        startColumn = currentMergedRange.getFirstColumn();
        POIUtils.removeMergedRange(sheet, currentMergedRange);
    }

    final CellRangeAddress newRange = POIUtils.mergeCells(sheet, startColumn, row, column, row);
    mergedRanges.add(newRange);
    return true;

}
 
Example 18
Source File: PageHandler.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * <p>
 * Process a CellImage from the images list and place the image on the sheet.
 * </p><p>
 * This involves changing the row height as necesssary and determining the column spread of the image.
 * </p>
 * @param cellImage
 * The image to be placed on the sheet.
 */
private void processCellImage( HandlerState state, Drawing drawing, CellImage cellImage ) {
	Coordinate location = cellImage.location;
	
	Cell cell = state.currentSheet.getRow( location.getRow() ).getCell( location.getCol() );

	IImageContent image = cellImage.image;		
	
	StyleManagerUtils smu = state.getSmu();
	float ptHeight = cell.getRow().getHeightInPoints();
	if( image.getHeight() != null ) {
		ptHeight = smu.fontSizeInPoints( image.getHeight().toString() );
	}

	// Get image width
	int endCol = cell.getColumnIndex();
       double lastColWidth = ClientAnchorConversions.widthUnits2Millimetres( (short)state.currentSheet.getColumnWidth( endCol ) )
       		+ 2.0;
       int dx = smu.anchorDxFromMM( lastColWidth, lastColWidth );
       double mmWidth = 0.0;
       if( smu.isAbsolute(image.getWidth())) {
           mmWidth = image.getWidth().convertTo(DimensionType.UNITS_MM);
       } else if(smu.isPixels(image.getWidth())) {
           mmWidth = ClientAnchorConversions.pixels2Millimetres( image.getWidth().getMeasure() );
       }
	// Allow image to span multiple columns
	CellRangeAddress mergedRegion = getMergedRegionBegunBy( state.currentSheet, location.getRow(), location.getCol() );
	if( (cellImage.spanColumns) || ( mergedRegion != null ) ) {
        log.debug( "Image size: ", image.getWidth(), " translates as mmWidth = ", mmWidth );
        if( mmWidth > 0) {
            double mmAccumulatedWidth = 0;
            int endColLimit = cellImage.spanColumns ? 256 : mergedRegion.getLastColumn();
            for( endCol = cell.getColumnIndex(); mmAccumulatedWidth < mmWidth && endCol < endColLimit; ++ endCol ) {
                lastColWidth = ClientAnchorConversions.widthUnits2Millimetres( (short)state.currentSheet.getColumnWidth( endCol ) )
                		+ 2.0;
                mmAccumulatedWidth += lastColWidth;
                log.debug( "lastColWidth = ", lastColWidth, "; mmAccumulatedWidth = ", mmAccumulatedWidth);
            }
            if( mmAccumulatedWidth > mmWidth ) {
                mmAccumulatedWidth -= lastColWidth;
                --endCol;
                double mmShort = mmWidth - mmAccumulatedWidth;
                dx = smu.anchorDxFromMM( mmShort, lastColWidth );
            }
        }
	} else {
		float widthRatio = (float)(mmWidth / lastColWidth);
		ptHeight = ptHeight / widthRatio;
	}

	int rowsSpanned = state.findRowsSpanned( cell.getRowIndex(), cell.getColumnIndex() );
	float neededRowHeightPoints = ptHeight;
	
	for( int i = 0; i < rowsSpanned; ++i ) {
		int rowIndex = cell.getRowIndex() + 1 + i;
		neededRowHeightPoints -= state.currentSheet.getRow(rowIndex).getHeightInPoints();
	}
	
	if( neededRowHeightPoints > cell.getRow().getHeightInPoints()) {
		cell.getRow().setHeightInPoints( neededRowHeightPoints );
	}
	
	// ClientAnchor anchor = wb.getCreationHelper().createClientAnchor();
	ClientAnchor anchor = state.getWb().getCreationHelper().createClientAnchor();
       anchor.setCol1(cell.getColumnIndex());
       anchor.setRow1(cell.getRowIndex());
       anchor.setCol2(endCol);
       anchor.setRow2(cell.getRowIndex() + rowsSpanned);
       anchor.setDx2(dx);
       anchor.setDy2( smu.anchorDyFromPoints( ptHeight, cell.getRow().getHeightInPoints() ) );
       anchor.setAnchorType(ClientAnchor.MOVE_DONT_RESIZE);
    drawing.createPicture(anchor, cellImage.imageIdx);
}
 
Example 19
Source File: CellReference.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public CellReference(Cell cell) {
    this(cell.getRowIndex(), cell.getColumnIndex(), false, false);
}
 
Example 20
Source File: CellAddress.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new CellAddress object.
 *
 * @param cell the Cell to get the location of
 */
public CellAddress(Cell cell) {
    this(cell.getRowIndex(), cell.getColumnIndex());
}