Example usage for org.apache.poi.hssf.usermodel HSSFRow getLastCellNum

List of usage examples for org.apache.poi.hssf.usermodel HSSFRow getLastCellNum

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFRow getLastCellNum.

Prototype

@Override
public short getLastCellNum() 

Source Link

Document

Gets the index of the last cell contained in this row PLUS ONE.

Usage

From source file:org.beangle.commons.transfer.excel.ExcelItemWriter.java

License:Open Source License

/** {@inheritDoc} */
public void writeTitle(String titleName, Object data) {
    if (null != titleName) {
        sheet = workbook.createSheet(titleName);
    } else {//  w  w  w.  j a  v a 2s  .  c om
        sheet = workbook.createSheet();
    }
    title = data;
    index = 0;
    writeItem(data);
    HSSFRow titleRow = sheet.getRow(index);
    HSSFCellStyle titleStyle = getTitleStyle();
    for (int i = 0; i < titleRow.getLastCellNum(); i++) {
        titleRow.getCell(i).setCellStyle(titleStyle);
    }
    index++;
}

From source file:org.beangle.model.transfer.excel.ExcelItemReader.java

License:Open Source License

/**
 * ????/*  www  .j a  v  a2 s .c o  m*/
 * 
 * @param sheet
 * @param rowIndex
 * @return
 */
protected String[] readLine(HSSFSheet sheet, int rowIndex) {
    HSSFRow row = sheet.getRow(rowIndex);
    logger.debug("values count:{}", row.getLastCellNum());
    List<String> attrList = CollectUtils.newArrayList();
    for (int i = 0; i < row.getLastCellNum(); i++) {
        HSSFCell cell = row.getCell(i);
        if (null != cell) {
            String attr = cell.getRichStringCellValue().getString();
            if (StringUtils.isEmpty(attr)) {
                break;
            } else {
                attrList.add(attr.trim());
            }
        } else {
            break;
        }
    }
    String[] attrs = new String[attrList.size()];
    attrList.toArray(attrs);
    logger.debug("has attrs {}", attrs);
    return attrs;
}

From source file:org.beangle.model.transfer.excel.ExcelItemReader.java

License:Open Source License

public Object read() {
    HSSFSheet sheet = workbook.getSheetAt(sheetNum);
    if (indexInSheet > sheet.getLastRowNum()) {
        return null;
    }// w ww .ja v  a 2s.c om
    HSSFRow row = sheet.getRow(indexInSheet);
    indexInSheet++;
    // ,
    if (row == null) {
        return new Object[attrCount];
    } else {
        Object[] values = new Object[((attrCount != 0) ? attrCount : row.getLastCellNum())];
        for (int k = 0; k < values.length; k++) {
            values[k] = getCellValue(row.getCell(k));
        }
        return values;
    }
}

From source file:org.beangle.model.transfer.excel.ExcelItemWriter.java

License:Open Source License

public void writeTitle(String titleName, Object data) {
    if (null != titleName) {
        sheet = workbook.createSheet(titleName);
    } else {//from   w w w .jav  a2s.  co  m
        sheet = workbook.createSheet();
    }
    title = data;
    index = 0;
    writeItem(data);
    HSSFRow titleRow = sheet.getRow(index);
    HSSFCellStyle titleStyle = getTitleStyle();
    for (int i = 0; i < titleRow.getLastCellNum(); i++) {
        titleRow.getCell(i).setCellStyle(titleStyle);
    }
    index++;
}

From source file:org.devgateway.eudevfin.importing.metadata.streamprocessors.ExcelStreamProcessor.java

License:Open Source License

private void generateMetadataInfoList() {
    this.metadataInfoList = new ArrayList<String>();
    final HSSFRow row = this.sheet.getRow(METADATA_INFO_ROW_NUM);
    final short end = row.getLastCellNum();
    for (short i = 1; i < end; i++) {
        final HSSFCell cell = row.getCell((int) i);
        if (cell != null) {
            if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) {
                final String value = cell.getStringCellValue();
                this.metadataInfoList.add(value);
            } else {
                throw new InvalidDataException("Expecting mapper name in cell B1");
            }//from w  ww.java2  s  .com
        } else {
            break;
        }
    }

}

From source file:org.drools.scorecards.parser.xls.XLSScorecardParser.java

License:Apache License

public String peekValueAt(int row, int col) {
    if (currentWorksheet != null) {
        if (row >= 0 && row < currentWorksheet.getLastRowNum()) {
            HSSFRow hssfRow = currentWorksheet.getRow(row);
            if (hssfRow != null && col >= 0 && col < hssfRow.getLastCellNum()) {
                return hssfRow.getCell(col).getStringCellValue();
            }/*from   w  w  w  .  j  a v a  2 s. c  o m*/
        }
    }
    return null;
}

From source file:org.everit.jira.reporting.plugin.export.ExcelToCsvConverter.java

License:Apache License

private void rowToCSV(final HSSFRow row) {
    HSSFCell cell = null;//  ww  w. j a v a2 s  .c  o  m
    int lastCellNum = 0;
    ArrayList<String> csvLine = new ArrayList<>();
    if (row != null) {
        lastCellNum = row.getLastCellNum();
        for (int i = 0; i <= lastCellNum; i++) {
            cell = row.getCell(i);
            if (cell == null) {
                csvLine.add("");
            } else {
                if (cell.getCellType() != Cell.CELL_TYPE_FORMULA) {
                    csvLine.add(formatter.formatCellValue(cell));
                } else {
                    csvLine.add(formatter.formatCellValue(cell, evaluator));
                }
            }
        }
        if (lastCellNum > maxRowWidth) {
            maxRowWidth = lastCellNum;
        }
    }
    csvData.add(csvLine);
}

From source file:org.exoplatform.addon.pulse.service.ws.RestActivitiesStatistic.java

License:Open Source License

private String buildCsvContent(HSSFWorkbook workbook) {
    HSSFSheet sheet = workbook.getSheetAt(0);
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i <= sheet.getLastRowNum(); i++) {
        HSSFRow row = sheet.getRow(i);
        for (int j = 0; j < row.getLastCellNum(); j++) {
            HSSFCell cell = row.getCell(j);
            int cellType = cell.getCellType();
            if (cellType == HSSFCell.CELL_TYPE_STRING) {
                buffer.append(cell.getStringCellValue());
            } else if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
                buffer.append(new DecimalFormat("#").format(cell.getNumericCellValue()));
            }/*from w  w w . j a v a2  s .  c  o  m*/
            if (j < row.getLastCellNum() - 1) {
                buffer.append(',');
            }
        }
        buffer.append('\n');
    }
    return buffer.toString();
}

From source file:org.exoplatform.services.document.impl.MSExcelDocumentReader.java

License:Open Source License

/**
 * Returns only a text from .xls file content.
 * //from ww w .j ava2s  . c  o m
 * @param is an input stream with .xls file content.
 * @return The string only with text from file content.
 */
public String getContentAsText(InputStream is) throws IOException, DocumentReadException {
    if (is == null) {
        throw new IllegalArgumentException("InputStream is null.");
    }

    final StringBuilder builder = new StringBuilder("");

    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

    try {
        if (is.available() == 0) {
            return "";
        }

        HSSFWorkbook wb;
        try {
            wb = new HSSFWorkbook(is);
        } catch (IOException e) {
            throw new DocumentReadException("Can't open spreadsheet.", e);
        }
        for (int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
            HSSFSheet sheet = wb.getSheetAt(sheetNum);
            if (sheet != null) {
                for (int rowNum = sheet.getFirstRowNum(); rowNum <= sheet.getLastRowNum(); rowNum++) {
                    HSSFRow row = sheet.getRow(rowNum);

                    if (row != null) {
                        int lastcell = row.getLastCellNum();
                        for (int k = 0; k < lastcell; k++) {
                            final HSSFCell cell = row.getCell((short) k);
                            if (cell != null) {
                                switch (cell.getCellType()) {
                                case HSSFCell.CELL_TYPE_NUMERIC: {
                                    double d = cell.getNumericCellValue();
                                    if (isCellDateFormatted(cell)) {
                                        Date date = HSSFDateUtil.getJavaDate(d);
                                        String cellText = dateFormat.format(date);
                                        builder.append(cellText).append(" ");
                                    } else {
                                        builder.append(d).append(" ");
                                    }
                                    break;
                                }
                                case HSSFCell.CELL_TYPE_FORMULA:
                                    SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
                                        public Void run() {
                                            builder.append(cell.getCellFormula().toString()).append(" ");
                                            return null;
                                        }
                                    });
                                    break;
                                case HSSFCell.CELL_TYPE_BOOLEAN:
                                    SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
                                        public Void run() {
                                            builder.append(cell.getBooleanCellValue()).append(" ");
                                            return null;
                                        }
                                    });
                                    break;
                                case HSSFCell.CELL_TYPE_ERROR:
                                    SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
                                        public Void run() {
                                            builder.append(cell.getErrorCellValue()).append(" ");
                                            return null;
                                        }
                                    });
                                    break;
                                case HSSFCell.CELL_TYPE_STRING:
                                    SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
                                        public Void run() {
                                            builder.append(cell.getStringCellValue().toString()).append(" ");
                                            return null;
                                        }
                                    });
                                    break;
                                default:
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("An exception occurred: " + e.getMessage());
                }
            }
        }
    }
    return builder.toString();
}

From source file:org.gageot.excel.core.ColumnMapRowMapper.java

License:Apache License

@Override
public Map<String, T> mapRow(HSSFRow row, int rowNum) throws IOException {
    Map<String, T> map = createColumnMap(row.getLastCellNum());

    short lastColumnNum = row.getLastCellNum();
    for (short columnNum = 0; columnNum < lastColumnNum; columnNum++) {
        if (columnNum < keys.length) { // TODO : write test
            map.put(keys[columnNum],//from w  w  w  . j  a va  2 s. c om
                    cellMapper.mapCell(row.getCell(columnNum, Row.RETURN_BLANK_AS_NULL), rowNum, columnNum));
        }
    }

    return map;
}