org.apache.poi.xssf.usermodel.XSSFComment Java Examples

The following examples show how to use org.apache.poi.xssf.usermodel.XSSFComment. 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: XlsxSaxAnalyser.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
private void readComments(ReadSheet readSheet) {
    if (!xlsxReadContext.readWorkbookHolder().getExtraReadSet().contains(CellExtraTypeEnum.COMMENT)) {
        return;
    }
    CommentsTable commentsTable = commentsTableMap.get(readSheet.getSheetNo());
    if (commentsTable == null) {
        return;
    }
    Map<CellAddress, XSSFComment> cellComments = commentsTable.getCellComments();
    for (XSSFComment xssfComment : cellComments.values()) {
        CellExtra cellExtra = new CellExtra(CellExtraTypeEnum.COMMENT, xssfComment.getString().toString(),
            xssfComment.getRow(), xssfComment.getColumn());
        xlsxReadContext.readSheetHolder().setCellExtra(cellExtra);
        xlsxReadContext.analysisEventProcessor().extra(xlsxReadContext);
    }
}
 
Example #2
Source File: PoijiHandler.java    From poiji with MIT License 6 votes vote down vote up
@Override
public void cell(String cellReference, String formattedValue, XSSFComment comment) {
    CellAddress cellAddress = new CellAddress(cellReference);
    int row = cellAddress.getRow();
    int headers = options.getHeaderStart();
    int column = cellAddress.getColumn();
    if (row <= headers) {
        columnIndexPerTitle.put(
                options.getCaseInsensitive() ? formattedValue.toLowerCase() : formattedValue,
                column
        );
        titlePerColumnIndex.put(column, getTitleNameForMap(formattedValue, column));
    }
    if (row + 1 <= options.skip()) {
        return;
    }
    if (limit != 0 && internalCount > limit) {
        return;
    }
    internalRow = row;
    setFieldValue(formattedValue, type, column);
}
 
Example #3
Source File: XSSFEventParser.java    From hadoopoffice with Apache License 2.0 6 votes vote down vote up
@Override
public void cell(String cellReference, String formattedValue, XSSFComment comment) {
	// create empty column, if needed
	
	CellAddress currentCellAddress = new CellAddress(cellReference);
	for (int i=this.currentColumn;i<currentCellAddress.getColumn();i++) {
		this.spreadSheetCellDAOCurrentRow.add(null);
		this.currentColumn++;
	}
	// add column
	SpreadSheetCellDAO currentDAO = null;
	if (comment!=null) {
		currentDAO = new SpreadSheetCellDAO(formattedValue,comment.getString().getString(), "", cellReference,this.sheetName);
	} else {
		currentDAO = new SpreadSheetCellDAO(formattedValue,"", "", cellReference,this.sheetName);
	}
	this.currentColumn++;
	this.spreadSheetCellDAOCurrentRow.add(currentDAO);
}
 
Example #4
Source File: XSSFSaxReadHandler.java    From myexcel with Apache License 2.0 5 votes vote down vote up
@Override
public void cell(String cellReference, String formattedValue,
                 XSSFComment comment) {
    isBlank = false;
    if (cellReference == null) {
        return;
    }
    int thisCol = (new CellReference(cellReference)).getCol();
    handleField(thisCol, formattedValue);
}
 
Example #5
Source File: CellMethodSpec.java    From zerocell with Apache License 2.0 5 votes vote down vote up
public static MethodSpec build(List<ColumnInfoType> columns) {
    final CodeBlock.Builder builder = CodeBlock.builder();
    LoggerFactory.getLogger(ZeroCellAnnotationProcessor.class)
                 .info("Found {} columns in source class", columns.size());
    columns.forEach(column -> {
        String staticFieldName = "COL_" + column.getIndex();
        String fieldName = column.getFieldName();

        String beanSetterProperty = beanSetterPropertyName(fieldName);

        builder.beginControlFlow("if ($L == column)", staticFieldName)
            .addStatement("assertColumnName($S, formattedValue)", column.getName());

        converterStatementFor(builder, column, beanSetterProperty);

        builder.addStatement("return").endControlFlow();
    });

    return  MethodSpec.methodBuilder("cell")
            .addAnnotation(Override.class)
            .addModifiers(Modifier.PUBLIC)
            .addParameter(String.class, "cellReference")
            .addParameter(String.class, "formattedValue", Modifier.FINAL)
            .addParameter(XSSFComment.class, "xssfComment", Modifier.FINAL)
            .addStatement("if (java.util.Objects.isNull(cur)) return")
            .addComment("Gracefully handle missing CellRef here in a similar way as XSSFCell does")
            .beginControlFlow("if(cellReference == null)")
            .addStatement("cellReference = new $T(currentRow, currentCol).formatAsString()", org.apache.poi.ss.util.CellAddress.class)
            .endControlFlow()
            .addStatement("int column = new $T(cellReference).getCol()", org.apache.poi.hssf.util.CellReference.class)
            .addStatement("currentCol = column")
            .addCode(builder.build())
            .build();
}
 
Example #6
Source File: EntityExcelSheetHandler.java    From zerocell with Apache License 2.0 5 votes vote down vote up
@Override
public void cell(String cellReference, String formattedValue, XSSFComment xssfComment) {
    if (cellReference == null) {
        cellReference = new CellAddress(currentRow, currentCol).formatAsString();
    }
    int column = new CellReference(cellReference).getCol();
    currentCol = column;

    ColumnInfo currentColumnInfo = columns.get(column);
    if (Objects.isNull(currentColumnInfo)) {
        return;
    }

    if (isHeaderRow && !entityHandler.isSkipHeaderRow()) {
        if (!currentColumnInfo.getName().equalsIgnoreCase(formattedValue.trim())) {
            throw new ZeroCellException(String.format("Expected Column '%s' but found '%s'", currentColumnInfo.getName(), formattedValue));
        }
    }
    // Prevent from trying to write to a null instance
    if (Objects.isNull(cur)) return;
    if (entityHandler.isSkipEmptyRows()) {
        if (formattedValue == null || formattedValue.isEmpty()) {
            emptyColumnCounter.increment();
        }
    }
    writeColumnField(cur, formattedValue, currentColumnInfo, currentRow);
}
 
Example #7
Source File: XLSX2CSV.java    From azeroth with Apache License 2.0 5 votes vote down vote up
public void cell(String cellReference, String formattedValue, XSSFComment comment) {
    if (firstCellOfRow) {
        firstCellOfRow = false;
    } else {
        _resultRowTmp.append(ExcelValidator.FIELD_SPLIT);
    }

    // gracefully handle missing CellRef here in a similar way as
    // XSSFCell does
    if (cellReference == null) {
        cellReference = new CellAddress(currentRow, currentCol).formatAsString();
    }

    // Did we miss any cells?
    int thisCol = (new CellReference(cellReference)).getCol();
    int missedCols = thisCol - currentCol - 1;
    for (int i = 0; i < missedCols; i++) {
        _resultRowTmp.append(ExcelValidator.FIELD_SPLIT);
    }
    currentCol = thisCol;

    // Number or string?
    try {
        Double.parseDouble(formattedValue);
        _resultRowTmp.append(formattedValue);
    } catch (NumberFormatException e) {
        _resultRowTmp.append(formattedValue);
    }
}
 
Example #8
Source File: ExcelSource.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/**
 * POI calls this with the value parsed from the cell.
 * @param cellReference The cell reference
 * @param formattedValue The value of the cell
 * @param comment a comment
 */
@Override
public void cell(String cellReference, String formattedValue, XSSFComment comment) {
    this.output.valueParsed(formattedValue);
    int thisCol = (new CellReference(cellReference)).getCol();

    //Fill missing columns
    int missedCols = thisCol - currentCol - 1;
    for (int i=0; i<missedCols; i++) {
        this.output.valueParsed("");
    }
    currentCol = thisCol;
}
 
Example #9
Source File: XLSX2CSV.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public void cell(String cellReference, String formattedValue, XSSFComment comment) {
	if (firstCellOfRow) {
		firstCellOfRow = false;
	} else {
		_resultRowTmp.append(ExcelValidator.FIELD_SPLIT);
	}

	// gracefully handle missing CellRef here in a similar way as
	// XSSFCell does
	if (cellReference == null) {
		cellReference = new CellAddress(currentRow, currentCol).formatAsString();
	}

	// Did we miss any cells?
	int thisCol = (new CellReference(cellReference)).getCol();
	int missedCols = thisCol - currentCol - 1;
	for (int i = 0; i < missedCols; i++) {
		_resultRowTmp.append(ExcelValidator.FIELD_SPLIT);
	}
	currentCol = thisCol;

	// Number or string?
	try {
		Double.parseDouble(formattedValue);
		_resultRowTmp.append(formattedValue);
	} catch (NumberFormatException e) {
		_resultRowTmp.append(formattedValue);
	}
}
 
Example #10
Source File: XSSFSheetXMLHandler.java    From myexcel with Apache License 2.0 4 votes vote down vote up
/**
 * Output an empty-cell comment.
 */
private void outputEmptyCellComment(CellAddress cellRef) {
    XSSFComment comment = comments.findCellComment(cellRef);
    output.cell(cellRef.formatAsString(), null, comment);
}
 
Example #11
Source File: PersonExcelReader.java    From zerocell with Apache License 2.0 4 votes vote down vote up
@Override
public void cell(String cellReference, final String formattedValue,
    final XSSFComment xssfComment) {
  if (java.util.Objects.isNull(cur)) return;
  // Gracefully handle missing CellRef here in a similar way as XSSFCell does
  if(cellReference == null) {
    cellReference = new CellAddress(currentRow, currentCol).formatAsString();
  }
  int column = new CellReference(cellReference).getCol();
  currentCol = column;
  if (COL_0 == column) {
    assertColumnName("ID", formattedValue);
    //Handle any exceptions thrown by the converter - this stops execution of the whole process;
    try {
      cur.setId(new com.creditdatamw.zerocell.example.IdPrefixingConverter().convert(formattedValue, "ID", currentRow));
    } catch(Exception e) {
      throw new ZeroCellException("com.creditdatamw.zerocell.example.IdPrefixingConverter threw an exception while trying to convert value " + formattedValue, e);
    }
    return;
  }
  if (COL_1 == column) {
    assertColumnName("FIRST_NAME", formattedValue);
    cur.setFirstName(noop.convert(formattedValue, "FIRST_NAME", currentRow));
    return;
  }
  if (COL_2 == column) {
    assertColumnName("MIDDLE_NAME", formattedValue);
    cur.setMiddleName(noop.convert(formattedValue, "MIDDLE_NAME", currentRow));
    return;
  }
  if (COL_3 == column) {
    assertColumnName("LAST_NAME", formattedValue);
    cur.setLastName(noop.convert(formattedValue, "LAST_NAME", currentRow));
    return;
  }
  if (COL_4 == column) {
    assertColumnName("DATE_OF_BIRTH", formattedValue);
    cur.setDateOfBirth(toLocalDate.convert(formattedValue, "DATE_OF_BIRTH", currentRow));
    return;
  }
  if (COL_6 == column) {
    assertColumnName("DATE_REGISTERED", formattedValue);
    cur.setDateOfRegistration(toLocalDate.convert(formattedValue, "DATE_REGISTERED", currentRow));
    return;
  }
  if (COL_5 == column) {
    assertColumnName("FAV_NUMBER", formattedValue);
    cur.setFavouriteNumber(toInteger.convert(formattedValue, "FAV_NUMBER", currentRow));
    return;
  }
}
 
Example #12
Source File: ConvertExcelToCSVProcessor.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void cell(String cellReference, String formattedValue,
                 XSSFComment comment) {
    if(skipRow) {
        return;
    }

    // gracefully handle missing CellRef here in a similar way as XSSFCell does
    if(cellReference == null) {
        cellReference = new CellAddress(currentRow, currentCol).formatAsString();
    }

    // Did we miss any cells?
    int thisCol = (new CellReference(cellReference)).getCol();

    // Should we skip this

    //Use the first row of the file to decide on the area of data to export
    if(firstRow && firstCellOfRow){
        readConfig.setFirstRow(currentRow);
        readConfig.setFirstColumn(thisCol);
    }

    //if this cell falls outside our area, or has been explcitely marked as a skipped column, return and don't write it out.
    if(!firstRow && (thisCol < readConfig.getFirstColumn() || thisCol > readConfig.getLastColumn())){
        return;
    }

    if(readConfig.getColumnsToSkip().contains(thisCol)){
        skippedColumns++;
        return;
    }

    int missedCols = (thisCol - readConfig.getFirstColumn()) - (currentCol - readConfig.getFirstColumn()) - 1;
    if(firstCellOfRow){
        missedCols = (thisCol - readConfig.getFirstColumn());
    }

    missedCols -= skippedColumns;

    if (firstCellOfRow) {
        firstCellOfRow = false;
    }

    for (int i=0; i<missedCols; i++) {
        fieldValues.add(null);
    }
    currentCol = thisCol;

    fieldValues.add(formattedValue);

    rowHasValues = true;
    skippedColumns = 0;
}
 
Example #13
Source File: XSSFSheetXMLHandler.java    From myexcel with Apache License 2.0 2 votes vote down vote up
/**
 * A cell, with the given formatted value (may be null),
 * and possibly a comment (may be null), was encountered.
 * <p>
 * Sheets that have missing or empty cells may result in
 * sparse calls to <code>cell</code>. See the code in
 * <code>src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java</code>
 * for an example of how to handle this scenario.
 *
 * @param cellReference  cellReference
 * @param comment        comment
 * @param formattedValue formattedValue
 */
void cell(String cellReference, String formattedValue, XSSFComment comment);