Example usage for org.apache.poi.ss.usermodel CellStyle getDataFormat

List of usage examples for org.apache.poi.ss.usermodel CellStyle getDataFormat

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel CellStyle getDataFormat.

Prototype

short getDataFormat();

Source Link

Document

get the index of the data format.

Usage

From source file:cn.afterturn.easypoi.excel.export.base.BaseExportService.java

License:Apache License

/**
 * Cell/*from   ww w.j  av a 2s . c o m*/
 */
public void createStringCell(Row row, int index, String text, CellStyle style, ExcelExportEntity entity) {
    Cell cell = row.createCell(index);
    if (style != null && style.getDataFormat() > 0 && style.getDataFormat() < 12) {
        cell.setCellValue(Double.parseDouble(text));
        cell.setCellType(CellType.NUMERIC);
    } else {
        RichTextString rtext;
        if (type.equals(ExcelType.HSSF)) {
            rtext = new HSSFRichTextString(text);
        } else {
            rtext = new XSSFRichTextString(text);
        }
        cell.setCellValue(rtext);
    }
    if (style != null) {
        cell.setCellStyle(style);
    }
    addStatisticsData(index, text, entity);
}

From source file:cn.bzvs.excel.export.base.ExcelExportBase.java

License:Apache License

/**
 * Cell// www. j a  va 2 s  . co m
 *
 * @param row
 * @param index
 * @param text
 * @param style
 * @param entity
 */
public void createStringCell(Row row, int index, String text, CellStyle style, ExcelExportEntity entity) {
    Cell cell = row.createCell(index);
    if (style != null && style.getDataFormat() > 0 && style.getDataFormat() < 12) {
        cell.setCellValue(Double.parseDouble(text));
        cell.setCellType(Cell.CELL_TYPE_NUMERIC);
    } else {
        RichTextString Rtext;
        if (type.equals(ExcelType.HSSF)) {
            Rtext = new HSSFRichTextString(text);
        } else {
            Rtext = new XSSFRichTextString(text);
        }
        cell.setCellValue(Rtext);
    }
    if (style != null) {
        cell.setCellStyle(style);
    }
    addStatisticsData(index, text, entity);
}

From source file:com.adobe.acs.commons.data.Variant.java

License:Apache License

private void setValue(Cell cell) {
    int cellType = cell.getCellType();
    if (cellType == Cell.CELL_TYPE_FORMULA) {
        cellType = cell.getCachedFormulaResultType();
    }//from  w  w  w .  j  av a  2s .  c om
    switch (cellType) {
    case Cell.CELL_TYPE_BOOLEAN:
        setValue(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        double number = cell.getNumericCellValue();
        if (Math.floor(number) == number) {
            setValue((long) number);
        } else {
            setValue(number);
        }
        if (DateUtil.isCellDateFormatted(cell)) {
            setValue(cell.getDateCellValue());
        }
        DataFormatter dataFormatter = new DataFormatter();
        if (cellType == Cell.CELL_TYPE_FORMULA) {
            setValue(dataFormatter.formatCellValue(cell));
        } else {
            CellStyle cellStyle = cell.getCellStyle();
            setValue(dataFormatter.formatRawCellContents(cell.getNumericCellValue(), cellStyle.getDataFormat(),
                    cellStyle.getDataFormatString()));
        }
        break;
    case Cell.CELL_TYPE_STRING:
        setValue(cell.getStringCellValue().trim());
        break;
    case Cell.CELL_TYPE_BLANK:
    default:
        clear();
        break;
    }
}

From source file:com.canoo.webtest.plugins.exceltest.ExcelVerifyCellStyle.java

License:Open Source License

public void doExecute() throws Exception {
    final String[] border = separateSides(getBorder());
    final String[] borderColor = separateSides(getBorderColor());
    for (int i = 0; i < SIDES.length; i++) {
        checkFormat(SIDES[i] + "Border", border[i], getCellBorder(i));
        checkFormat(SIDES[i] + "BorderColor", ExcelColorUtils.lookupStandardColorName(borderColor[i]),
                getCellBorderColor(i));/*  w w  w  . j  av a2 s  .c o  m*/
    }
    final Cell excelCell = getExcelCell();
    checkFormat("type", getType(),
            ExcelCellUtils.getCellType(excelCell == null ? Cell.CELL_TYPE_BLANK : excelCell.getCellType()));
    if (excelCell == null) {
        if (cellNotRequired()) {
            return;
        } else {
            throw new StepExecutionException("Can't find cell for " + getCellReferenceStr(), this);
        }
    }
    final CellStyle cellStyle = excelCell.getCellStyle();
    checkFormat("format", getFormat(),
            getExcelWorkbook().createDataFormat().getFormat(cellStyle.getDataFormat()));
    checkFormat("align", getAlign(), ExcelCellUtils.getAlignmentString(cellStyle.getAlignment()));
    checkFormat("valign", getValign(),
            ExcelCellUtils.getVerticalAlignmentString(cellStyle.getVerticalAlignment()));
    checkFormat("wrap", getWrap(), String.valueOf(cellStyle.getWrapText()));
    checkFormat("locked", getLocked(), String.valueOf(cellStyle.getLocked()));
    checkFormat("fontName", getFontName(), getFont(cellStyle).getFontName());
    checkFormat("fontSize", getFontSize(), String.valueOf(getFont(cellStyle).getFontHeightInPoints()));
    checkFormat("fontStyle", sortElements(getFontStyle()), getFontStyle(getFont(cellStyle)));
    checkFormat("fillColor", ExcelColorUtils.lookupStandardColorName(getFillColor()),
            ExcelColorUtils.getColorName(this, cellStyle.getFillForegroundColorColor()));
    checkFormat("fillBackgroundColor", ExcelColorUtils.lookupStandardColorName(getFillBackgroundColor()),
            ExcelColorUtils.getColorName(this, cellStyle.getFillBackgroundColorColor()));
    checkFormat("textColor", ExcelColorUtils.lookupStandardColorName(getTextColor()),
            ExcelColorUtils.getColorName(this, getFont(cellStyle).getColor()));
    checkFormat("fillPattern", getFillPattern(), ExcelCellUtils.getFillPattern(cellStyle.getFillPattern()));
}

From source file:com.frameworkset.platform.sanylog.util.POIExcelUtil.java

License:Open Source License

/**
 * ?Excel?MapList?Excel??Java.//from  ww w  .  ja  v  a 2s  .  co m
 * 
 * @param file
 * @return
 * @throws IOException
 */
public static List<Map<String, Object>> parseHSSFMapList(MultipartFile file) throws IOException {//
    POIFSFileSystem poiFs = new POIFSFileSystem(file.getInputStream());
    HSSFWorkbook wb = new HSSFWorkbook(poiFs);

    HSSFSheet sheet = wb.getSheetAt(0);

    int rowNum = sheet.getLastRowNum();
    HSSFRow titleRow = sheet.getRow(0);

    int colNum = titleRow.getLastCellNum();

    //?17
    List<String> titleList = new ArrayList<String>();
    for (int i = 0; i < colNum; i++) {
        String title = titleRow.getCell(i).getStringCellValue();
        titleList.add(trimTitle(title));
    }

    List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
    for (int i = 1; i <= rowNum; i++) {
        HSSFRow row = sheet.getRow(i);
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        for (int j = 0; j < colNum; j++) {
            HSSFCell cell = row.getCell(j);
            if (cell != null) {
                switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_NUMERIC:
                    double d = cell.getNumericCellValue();
                    CellStyle style = cell.getCellStyle();
                    //?
                    if (HSSFDateUtil.isCellDateFormatted(cell) || (style != null
                            && (style.getDataFormat() == 57 || style.getDataFormat() == 58))) {
                        map.put(titleList.get(j), HSSFDateUtil.getJavaDate(d));
                    } else {
                        map.put(titleList.get(j), d);
                    }
                    break;

                default:
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    map.put(titleList.get(j), row.getCell(j).getStringCellValue());
                    break;
                }
            } else {
                map.put(titleList.get(j), null);
            }
        }
        mapList.add(map);
    }

    return mapList;
}

From source file:com.github.camaral.sheeco.type.adapter.SpreadsheetBooleanAdapterTest.java

License:Apache License

@Test(expectedExceptions = InvalidCellValueException.class)
public void testInvalidNumeric() {
    CellStyle style = mock(CellStyle.class);
    when(style.getDataFormat()).thenReturn((short) 0x0e);

    when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_NUMERIC);
    when(cell.getDateCellValue()).thenReturn(new Date(111111));
    when(cell.getCellStyle()).thenReturn(style);

    sut.fromSpreadsheet(cell);/*from w w  w .j a  va 2  s.c o m*/
}

From source file:com.github.camaral.sheeco.type.adapter.SpreadsheetDateAdapterTest.java

License:Apache License

@Test
public void testNumericTypeDate() {

    // given/*from w  w  w  .  j a  v  a  2 s  .c  om*/
    Date expected = new Date(111111);

    CellStyle style = mock(CellStyle.class);
    when(style.getDataFormat()).thenReturn((short) 0x0e);

    when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_NUMERIC);
    when(cell.getDateCellValue()).thenReturn(expected);
    when(cell.getCellStyle()).thenReturn(style);

    // when
    Date value = sut.fromSpreadsheet(cell);

    // then
    Assert.assertEquals(value, expected);
}

From source file:com.ncc.excel.test.ExcelUtil.java

License:Apache License

/** 
 * ????? //from   w w w  .  ja  v a  2 s. c  o  m
 *  
 * @param fromStyle 
 * @param toStyle 
 */
public static void copyCellStyle(CellStyle fromStyle, CellStyle toStyle) {
    toStyle.setAlignment(fromStyle.getAlignment());
    //   
    toStyle.setBorderBottom(fromStyle.getBorderBottom());
    toStyle.setBorderLeft(fromStyle.getBorderLeft());
    toStyle.setBorderRight(fromStyle.getBorderRight());
    toStyle.setBorderTop(fromStyle.getBorderTop());
    toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
    toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
    toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
    toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());

    // ?  
    toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
    toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());

    // ??  
    toStyle.setDataFormat(fromStyle.getDataFormat());
    toStyle.setFillPattern(fromStyle.getFillPattern());
    // toStyle.setFont(fromStyle.getFont(null));  
    toStyle.setHidden(fromStyle.getHidden());
    toStyle.setIndention(fromStyle.getIndention());//   
    toStyle.setLocked(fromStyle.getLocked());
    toStyle.setRotation(fromStyle.getRotation());//   
    toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());
    toStyle.setWrapText(fromStyle.getWrapText());

}

From source file:com.qihang.winter.poi.excel.export.base.ExcelExportBase.java

License:Apache License

/**
 * Cell/*from   ww  w .  j  ava2 s.com*/
 * 
 * @param row
 * @param index
 * @param text
 * @param style
 * @param entity
 */
public void createStringCell(Row row, int index, String text, CellStyle style,
        com.qihang.winter.poi.excel.entity.params.ExcelExportEntity entity) {
    Cell cell = row.createCell(index);
    if (style != null && style.getDataFormat() > 0 && style.getDataFormat() < 12) {
        cell.setCellValue(Double.parseDouble(text));
        cell.setCellType(Cell.CELL_TYPE_NUMERIC);
    } else {
        RichTextString Rtext;
        if (type.equals(com.qihang.winter.poi.excel.entity.enmus.ExcelType.HSSF)) {
            Rtext = new HSSFRichTextString(text);
        } else {
            Rtext = new XSSFRichTextString(text);
        }
        cell.setCellValue(Rtext);
    }
    if (style != null) {
        cell.setCellStyle(style);
    }
    addStatisticsData(index, text, entity);
}

From source file:com.vaadin.addon.spreadsheet.CellValueManager.java

License:Open Source License

private String getCachedFormulaCellValue(Cell formulaCell) {
    String result = null;//w  ww.j  a  v a2 s . c o m
    switch (formulaCell.getCachedFormulaResultType()) {
    case Cell.CELL_TYPE_STRING:
        result = formulaCell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        result = String.valueOf(formulaCell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        result = ErrorEval.getText(formulaCell.getErrorCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        CellStyle style = formulaCell.getCellStyle();
        result = formatter.formatRawCellContents(formulaCell.getNumericCellValue(), style.getDataFormat(),
                style.getDataFormatString());
        break;
    }
    return result;
}