com.welemski.wrench.poi.util.SpreadsheetCell Maven / Gradle / Ivy
The newest version!
/*
* The MIT License
*
* Copyright 2016 Lemuel Raganas.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.welemski.wrench.poi.util;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Optional;
import java.util.function.Function;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
/**
*
* @author Lemuel Raganas
*/
public class SpreadsheetCell {
private Cell cell;
private boolean evaluate;
public SpreadsheetCell(Cell cell){
this.cell = cell;
}
public Cell getCell(){
return this.cell;
}
public String getStringValue(String decimalFormat, String value){
if(this.cell == null){
return value;
}
switch(this.cell.getCellType()){
case Cell.CELL_TYPE_BLANK :
return value;
case Cell.CELL_TYPE_BOOLEAN :
Boolean b = cell.getBooleanCellValue();
return b.toString();
case Cell.CELL_TYPE_ERROR :
return value;
case Cell.CELL_TYPE_FORMULA :
if(this.evaluate){
String richString = cell.getRichStringCellValue().getString();
if(richString == null){
return value;
}
return richString;
}
return cell.getCellFormula();
case Cell.CELL_TYPE_NUMERIC :
if(DateUtil.isCellDateFormatted(cell)){
if(cell.getDateCellValue() == null){
return value;
}
return cell.getDateCellValue().toString();
}
Double doubleValue = cell.getNumericCellValue();
if(decimalFormat != null){
double numval = cell.getNumericCellValue();
DecimalFormat dcFormat = new DecimalFormat(decimalFormat);
String dcs = dcFormat.format(numval);
return dcs;
}
return doubleValue.toString();
default:
return cell.getStringCellValue();
}
}
public String getStringValue(){
return getStringValue("###",null);
}
public Double getNumericValue(){
return getNumericValue(null);
}
public Double getNumericValue(Double value){
if(this.cell == null){
return value;
}
switch(this.cell.getCellType()){
case Cell.CELL_TYPE_BLANK :
return value;
case Cell.CELL_TYPE_BOOLEAN :
if(cell.getBooleanCellValue()){
return 1.0;
} else {
return 0.0;
}
case Cell.CELL_TYPE_ERROR :
return value;
case Cell.CELL_TYPE_FORMULA :
if(evaluate){
FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
return evaluator.evaluate(cell).getNumberValue();
}
return cell.getNumericCellValue();
case Cell.CELL_TYPE_STRING :
try{
return Double.parseDouble(cell.getStringCellValue());
} catch (NumberFormatException ex){
return value;
}
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)){
return ((Long)cell.getDateCellValue().getTime()).doubleValue();
}
return cell.getNumericCellValue();
default:
return value;
}
}
public Date getDateValue(Date value){
if(this.cell == null){
return value;
}
if(this.cell.getCellType() == Cell.CELL_TYPE_NUMERIC && DateUtil.isCellDateFormatted(cell)){
return cell.getDateCellValue();
}
return value;
}
public Date getDateValue(){
return getDateValue(null);
}
/**
* Custom extraction method.
* Use this method if you want to extract data from a Cell using your own routines.
*
* @param value
* @param extractor
* @return Optional
*/
public Optional