Example usage for org.apache.poi.xssf.usermodel XSSFCell getCellStyle

List of usage examples for org.apache.poi.xssf.usermodel XSSFCell getCellStyle

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFCell getCellStyle.

Prototype

@Override
public XSSFCellStyle getCellStyle() 

Source Link

Document

Return the cell's style.

Usage

From source file:achmad.rifai.admin.ui.Saver.java

private void title(int i, XSSFRow r1, String s) {
    org.apache.poi.xssf.usermodel.XSSFCell c = r1.createCell(i);
    c.setCellType(CellType.STRING);/*from www. j  a  v  a  2s.c  o  m*/
    c.setCellValue(s);
    org.apache.poi.xssf.usermodel.XSSFCellStyle cs = c.getCellStyle();
    cs.setFillBackgroundColor(new org.apache.poi.xssf.usermodel.XSSFColor(Color.BLACK));
    cs.setFillForegroundColor(new org.apache.poi.xssf.usermodel.XSSFColor(Color.YELLOW));
    cs.setAlignment(HorizontalAlignment.CENTER);
    cs.setVerticalAlignment(VerticalAlignment.CENTER);
    cs.setBorderBottom(BorderStyle.DASHED);
    cs.setBorderTop(BorderStyle.DASHED);
    cs.setBorderLeft(BorderStyle.DASHED);
    cs.setBorderRight(BorderStyle.DASHED);
}

From source file:achmad.rifai.admin.ui.Saver.java

private void konten(int i, XSSFRow r, String s) {
    org.apache.poi.xssf.usermodel.XSSFCell c = r.createCell(i);
    c.setCellType(CellType.STRING);//from  w  ww  .ja  v  a 2s.  c o  m
    c.setCellValue(s);
    org.apache.poi.xssf.usermodel.XSSFCellStyle cs = c.getCellStyle();
    cs.setFillBackgroundColor(new org.apache.poi.xssf.usermodel.XSSFColor(Color.WHITE));
    cs.setFillForegroundColor(new org.apache.poi.xssf.usermodel.XSSFColor(Color.BLACK));
    cs.setAlignment(HorizontalAlignment.JUSTIFY);
    cs.setVerticalAlignment(VerticalAlignment.TOP);
    cs.setBorderBottom(BorderStyle.DASHED);
    cs.setBorderTop(BorderStyle.DASHED);
    cs.setBorderLeft(BorderStyle.DASHED);
    cs.setBorderRight(BorderStyle.DASHED);
}

From source file:achmad.rifai.admin.ui.Saver.java

private void konten1(int i, XSSFRow r, String s) {
    org.apache.poi.xssf.usermodel.XSSFCell c = r.createCell(i);
    c.setCellType(CellType.STRING);/*from   ww w .j ava 2s .co m*/
    c.setCellValue(s);
    org.apache.poi.xssf.usermodel.XSSFCellStyle cs = c.getCellStyle();
    cs.setFillBackgroundColor(new org.apache.poi.xssf.usermodel.XSSFColor(Color.WHITE));
    cs.setFillForegroundColor(new org.apache.poi.xssf.usermodel.XSSFColor(Color.BLACK));
    cs.setAlignment(HorizontalAlignment.CENTER);
    cs.setVerticalAlignment(VerticalAlignment.CENTER);
    cs.setBorderBottom(BorderStyle.DASHED);
    cs.setBorderTop(BorderStyle.DASHED);
    cs.setBorderLeft(BorderStyle.DASHED);
    cs.setBorderRight(BorderStyle.DASHED);
}

From source file:achmad.rifai.admin.ui.Saver.java

private void title2(int i, XSSFRow r1, String s, XSSFSheet sh) {
    sh.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(0, 1, i, i));
    org.apache.poi.xssf.usermodel.XSSFCell c = r1.createCell(i);
    c.setCellType(CellType.STRING);//  w w  w.  j  a v  a  2s. co  m
    c.setCellValue(s);
    org.apache.poi.xssf.usermodel.XSSFCellStyle cs = c.getCellStyle();
    cs.setFillBackgroundColor(new org.apache.poi.xssf.usermodel.XSSFColor(Color.BLACK));
    cs.setFillForegroundColor(new org.apache.poi.xssf.usermodel.XSSFColor(Color.YELLOW));
    cs.setAlignment(HorizontalAlignment.CENTER);
    cs.setVerticalAlignment(VerticalAlignment.CENTER);
    cs.setBorderBottom(BorderStyle.DASHED);
    cs.setBorderTop(BorderStyle.DASHED);
    cs.setBorderLeft(BorderStyle.DASHED);
    cs.setBorderRight(BorderStyle.DASHED);
}

From source file:com.appdynamics.jrbronet.projectplan.ExcelManager.java

private static void copyRow(XSSFWorkbook workbook, XSSFSheet sourceWorksheet, int sourceRowNum,
        XSSFSheet destinationWorksheet, int destinationRowNum) {
    // Get the source / new row
    XSSFRow newRow = destinationWorksheet.getRow(destinationRowNum);
    XSSFRow sourceRow = sourceWorksheet.getRow(sourceRowNum);

    // If the row exist in destination, push down all rows by 1 else create a new row

    if (newRow != null) {
        destinationWorksheet.shiftRows(destinationRowNum, destinationWorksheet.getLastRowNum(), 1);
    } else {/*from   w  ww . j  av a 2  s . c  o  m*/
        newRow = destinationWorksheet.createRow(destinationRowNum);
    }

    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        XSSFCell oldCell = sourceRow.getCell(i);
        XSSFCell newCell = newRow.createCell(i);

        // If the old cell is null jump to next cell
        if (oldCell == null) {
            newCell = null;
            continue;
        }

        // Copy style from old cell and apply to new cell
        XSSFCellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        ;
        newCell.setCellStyle(newCellStyle);

        // If there is a cell comment, copy
        if (oldCell.getCellComment() != null) {
            newCell.setCellComment(oldCell.getCellComment());
        }

        // If there is a cell hyperlink, copy
        if (oldCell.getHyperlink() != null) {
            newCell.setHyperlink(oldCell.getHyperlink());
        }

        // Set the cell data type
        newCell.setCellType(oldCell.getCellType());

        //newCell.setCellValue(oldCell.getRawValue());
        //newCell.setCellType(oldCell.getCellType());                        

        // Set the cell data value
        switch (oldCell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_ERROR:
            newCell.setCellErrorValue(oldCell.getErrorCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            newCell.setCellFormula(oldCell.getCellFormula());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }

    }

    // If there are are any merged regions in the source row, copy to new row
    /*
    for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
    CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
    if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
        CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
                (newRow.getRowNum() +
                        (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow()
                                )),
                cellRangeAddress.getFirstColumn(),
                cellRangeAddress.getLastColumn());
        worksheet.addMergedRegion(newCellRangeAddress);
    }            
    }
    */
}

From source file:com.cn.util.ExcelImport.java

/**
* ?2007excel/*from w ww. j a v  a2  s.c o m*/
* 
* @param file
* @return
*/
private static List<List<Object>> read2007Excel(InputStream inputStream) throws IOException {
    List<List<Object>> dataList = new ArrayList<>();
    XSSFWorkbook xwb = new XSSFWorkbook(inputStream);
    XSSFSheet sheet = xwb.getSheetAt(0);
    XSSFRow row = null;
    XSSFCell cell = null;
    Object val = null;
    DecimalFormat df = new DecimalFormat("0");// ?
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// ?

    for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
        row = sheet.getRow(i);
        if (row == null) {
            continue;
        }
        List<Object> objList = new ArrayList<>();
        for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
            cell = row.getCell(j);
            if (cell == null) {
                val = null;
                objList.add(val);
                continue;
            }
            switch (cell.getCellType()) {
            case XSSFCell.CELL_TYPE_STRING:
                val = cell.getStringCellValue();
                break;
            case XSSFCell.CELL_TYPE_NUMERIC:
                if ("@".equals(cell.getCellStyle().getDataFormatString())) {
                    val = df.format(cell.getNumericCellValue());
                } else if ("General".equals(cell.getCellStyle().getDataFormatString())) {
                    val = df.format(cell.getNumericCellValue());
                } else {
                    val = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
                }
                break;
            case XSSFCell.CELL_TYPE_BOOLEAN:
                val = cell.getBooleanCellValue();
                break;
            case XSSFCell.CELL_TYPE_BLANK:
                val = "";
                break;
            default:
                val = cell.toString();
                break;
            }
            objList.add(val);
        }
        dataList.add(objList);
    }
    return dataList;
}

From source file:com.excel.javafx.frames.MainFrame.java

public static boolean compareTwoCells(XSSFCell cell1, XSSFCell cell2) {
    if ((cell1 == null) && (cell2 == null)) {
        return true;
    } else if ((cell1 == null) || (cell2 == null)) {
        return false;
    }//w w  w.j  ava  2s  . c o  m

    boolean equalCells = false;
    int type1 = cell1.getCellType();
    int type2 = cell2.getCellType();
    if (type1 == type2) {
        if (cell1.getCellStyle().equals(cell2.getCellStyle())) {
            // Compare cells based on its type
            switch (cell1.getCellType()) {
            case HSSFCell.CELL_TYPE_FORMULA:
                if (cell1.getCellFormula().equals(cell2.getCellFormula())) {
                    equalCells = true;
                }
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                if (cell1.getNumericCellValue() == cell2.getNumericCellValue()) {
                    equalCells = true;
                }
                break;
            case HSSFCell.CELL_TYPE_STRING:
                if (cell1.getStringCellValue().equals(cell2.getStringCellValue())) {
                    equalCells = true;
                }
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                if (cell2.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    equalCells = true;
                }
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                if (cell1.getBooleanCellValue() == cell2.getBooleanCellValue()) {
                    equalCells = true;
                }
                break;
            case HSSFCell.CELL_TYPE_ERROR:
                if (cell1.getErrorCellValue() == cell2.getErrorCellValue()) {
                    equalCells = true;
                }
                break;
            default:
                if (cell1.getStringCellValue().equals(cell2.getStringCellValue())) {
                    equalCells = true;
                }
                break;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
    return equalCells;
}

From source file:com.yanglb.utilitys.codegen.core.reader.BaseReader.java

License:Apache License

/**
 * ?Cell???/*from   ww w . ja  v a2 s.c o  m*/
 * @param cell
 * @return
 */
public String getCellStringValue(XSSFCell cell) {
    String result = null;
    int type = cell.getCellType();
    if (type == Cell.CELL_TYPE_FORMULA)
        type = cell.getCachedFormulaResultType();
    if (type == Cell.CELL_TYPE_BLANK)
        return null;
    if (type == Cell.CELL_TYPE_ERROR) {
        return "#VALUE!";
    }

    switch (type) {
    case Cell.CELL_TYPE_BOOLEAN:
        result = String.valueOf(cell.getBooleanCellValue());
        break;

    case Cell.CELL_TYPE_STRING:
        result = cell.getStringCellValue();
        break;

    case Cell.CELL_TYPE_NUMERIC: {
        if (cell.getCellStyle().getDataFormat() == DataFormatType.FORMAT_DATE) {
            Date date = cell.getDateCellValue();
            String format = "yyyy/MM/dd";//cell.getCellStyle().getDataFormatString();
            if (cell.getCellStyle().getDataFormatString().contains(":")) {
                // 
                format = "yyyy/MM/dd HH:mm:ss";
            }
            SimpleDateFormat df = null;
            try {
                df = new SimpleDateFormat(format);
            } catch (IllegalArgumentException e) {
                //df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
            }
            result = df.format(date);
        } else {
            // ?? 0
            Number number = cell.getNumericCellValue();
            result = number.toString();
            if (result.indexOf('.') != -1) {
                result = result.replaceAll("[0]*$", "");
            }
            if (result.endsWith(".")) {
                result = result.substring(0, result.length() - 1);
            }
        }
    }
        break;
    }
    return result;
}

From source file:de.bund.bfr.knime.openkrise.db.imports.custom.bfrnewformat.TraceGenerator.java

License:Open Source License

private XSSFRow copyRow(XSSFWorkbook workbook, XSSFSheet worksheet, int sourceRowNum, int destinationRowNum) {
    XSSFRow sourceRow = worksheet.getRow(sourceRowNum);
    worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1, true, false);
    XSSFRow newRow = worksheet.createRow(destinationRowNum);

    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        XSSFCell oldCell = sourceRow.getCell(i);
        XSSFCell newCell = newRow.createCell(i);

        // If the old cell is null jump to next cell
        if (oldCell == null) {
            newCell = null;/*from   www.j av a 2s . co m*/
            continue;
        }

        // Copy style from old cell and apply to new cell
        XSSFCellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());

        newCell.setCellStyle(newCellStyle);

        // Set the cell data type
        newCell.setCellType(oldCell.getCellType());

    }

    // If there are are any merged regions in the source row, copy to new row
    for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
        CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
        if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
            CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
                    (newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())),
                    cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn());
            worksheet.addMergedRegion(newCellRangeAddress);
        }
    }

    newRow.setHeight(sourceRow.getHeight());

    return newRow;
}

From source file:de.escnet.ExcelTable.java

License:Open Source License

private void styleAlignment(Map<String, String> styles, XSSFCell cell) {
    XSSFCellStyle cellStyle = cell.getCellStyle();
    if (cellStyle != null) {
        short alignment = cellStyle.getAlignment();
        if (alignment == XSSFCellStyle.ALIGN_RIGHT) {
            styles.put("text-align", "right");
            return;
        }//from   ww  w.  j  a  v a  2  s  . c o m

        if (alignment == XSSFCellStyle.ALIGN_CENTER) {
            styles.put("text-align", "center");
            return;
        }
    }

    styles.put("text-align", "left");
}