Example usage for org.apache.poi.ss.usermodel Row getLastCellNum

List of usage examples for org.apache.poi.ss.usermodel Row getLastCellNum

Introduction

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

Prototype

short getLastCellNum();

Source Link

Document

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

Usage

From source file:com.github.jaydsolanki.excelio.ExcelIO.java

private List<List<String>> readSheet(Sheet sheet) {
    List<List<String>> data = new ArrayList<>();
    for (int i = 0; i < sheet.getLastRowNum() + 1; i++) {
        Row row = sheet.getRow(i);
        List<String> rowList = new ArrayList<String>();
        for (int j = 0; j < row.getLastCellNum(); j++) {
            rowList.add(row.getCell(j) + "");
        }/*from   w  w  w .  j a  va2s.c  om*/
        data.add(rowList);
    }
    return data;
}

From source file:com.github.jferard.spreadsheetwrapper.xls.poi.XlsPoiWriter.java

License:Open Source License

private int getCellCountUnsafe(final int r) {
    final Row row = this.sheet.getRow(r);
    final short count;
    if (row == null)
        count = 0;//  www .ja  va2  s  .c o m
    else
        count = row.getLastCellNum(); // 1-based

    return count;
}

From source file:com.github.wnameless.workbookaccessor.WorkbookReader.java

License:Apache License

private RubyArray<String> rowToRubyArray(final Row row, boolean isCSV) {
    int colNum;/*  w w w  .ja va  2  s .  c om*/
    if (hasHeader)
        colNum = sheet.rowIterator().next().getLastCellNum();
    else
        colNum = row.getLastCellNum();

    return range(0, colNum - 1).map(new TransformBlock<Integer, Cell>() {

        @Override
        public Cell yield(Integer item) {
            return row.getCell(item);
        }

    }).map(cell2Str(isCSV));
}

From source file:com.googlecode.testcase.annotation.handle.toexcel.strategy.ToHtmlWithExcel.java

License:Apache License

private void printSheetContent(Sheet sheet) {
    printColumnHeads(sheet);/*from ww  w .  j  a va  2  s . c  o  m*/

    out.format("<tbody>%n");
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();
        out.format("  <tr>%n");
        out.format("    <td class=%s>%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1);
        for (int i = firstColumn; i < endColumn; i++) {
            String content = "&nbsp;";
            String attrs = "";
            CellStyle style = null;
            if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    style = cell.getCellStyle();
                    attrs = tagStyle(cell, style);
                    //Set the value that is rendered for the cell
                    //also applies the format
                    CellFormat cf = CellFormat.getInstance(style.getDataFormatString());
                    CellFormatResult result = cf.apply(cell);
                    content = result.text;
                    if (content.equals(""))
                        content = "&nbsp;";
                }
            }
            out.format("    <td class=%s %s>%s</td>%n", styleName(style), attrs, content);
        }
        out.format("  </tr>%n");
    }
    out.format("</tbody>%n");
}

From source file:com.hack23.cia.service.external.esv.impl.EsvApiImpl.java

License:Apache License

/**
 * Adds the ministry per year to map./*from w w w .  j  a v  a  2  s  . c om*/
 *
 * @param name
 *            the name
 * @param map
 *            the map
 * @param mySheet
 *            the my sheet
 */
private static void addMinistryPerYearToMap(final String name,
        final Map<Integer, List<GovernmentBodyAnnualSummary>> map, final HSSFSheet mySheet) {
    if (mySheet.getSheetName().chars().allMatch(Character::isDigit)) {

        final int year = Integer.valueOf(mySheet.getSheetName());

        final List<GovernmentBodyAnnualSummary> yearList = new ArrayList<>();
        final Iterator<Row> rowIterator = mySheet.iterator();

        rowIterator.next();

        while (rowIterator.hasNext()) {
            final Row row = rowIterator.next();
            final short maxColIx = row.getLastCellNum();

            if (maxColIx == 10) {
                final GovernmentBodyAnnualSummary governmentBodyAnnualSummary = new GovernmentBodyAnnualSummary(
                        year, row.getCell(0).toString(), getInteger(row.getCell(1).toString()),
                        row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString(),
                        row.getCell(5).toString(), getInteger(row.getCell(6).toString()),
                        getInteger(row.getCell(7).toString()), row.getCell(8).toString(),
                        row.getCell(9).toString());
                row.getCell(9).toString();

                if (name == null || name.equalsIgnoreCase(governmentBodyAnnualSummary.getMinistry())) {
                    yearList.add(governmentBodyAnnualSummary);
                }

            }

        }
        map.put(year, yearList);
    }
}

From source file:com.hack23.cia.service.external.esv.impl.EsvApiImpl.java

License:Apache License

/**
 * Adds the data for year to map.//from   www. j  ava2s . c o m
 *
 * @param name
 *            the name
 * @param map
 *            the map
 * @param mySheet
 *            the my sheet
 */
private static void addDataForYearToMap(String name, final Map<Integer, GovernmentBodyAnnualSummary> map,
        final HSSFSheet mySheet) {
    if (mySheet.getSheetName().chars().allMatch(Character::isDigit)) {

        final int year = Integer.valueOf(mySheet.getSheetName());

        final Iterator<Row> rowIterator = mySheet.iterator();

        rowIterator.next();

        while (rowIterator.hasNext()) {
            final Row row = rowIterator.next();
            final short maxColIx = row.getLastCellNum();

            if (maxColIx == 10) {
                final GovernmentBodyAnnualSummary governmentBodyAnnualSummary = new GovernmentBodyAnnualSummary(
                        year, row.getCell(0).toString(), getInteger(row.getCell(1).toString()),
                        row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString(),
                        row.getCell(5).toString(), getInteger(row.getCell(6).toString()),
                        getInteger(row.getCell(7).toString()), row.getCell(8).toString(),
                        row.getCell(9).toString());
                row.getCell(9).toString();

                if (name == null || name.equalsIgnoreCase(governmentBodyAnnualSummary.getName())) {
                    map.put(year, governmentBodyAnnualSummary);
                }
            }
        }
    }
}

From source file:com.heimaide.server.common.utils.excel.ImportExcel.java

License:Open Source License

/**
 * //  w  w  w.  j  a  va2  s .  co m
 */
public static void main(String[] args) throws Throwable {

    ImportExcel ei = new ImportExcel("target/a.xlsx", 1);
    File file = new File("");

    for (Row row : ei.getSheet()) {
        for (int i = 0; i < row.getLastCellNum(); i++) {
            Cell cell = row.getCell(i);
            CellStyle style = cell.getCellStyle();
            System.out.println(style);
        }
    }

    for (int i = ei.getDataRowNum(); i < ei.getLastDataRowNum(); i++) {
        Row row = ei.getRow(i);
        for (int j = 0; j < ei.getLastCellNum(); j++) {
            Object val = ei.getCellValue(row, j);
            System.out.print(val + ", ");
        }
        System.out.print("\n");
    }

}

From source file:com.hp.idc.resm.util.ExcelUtil.java

License:Open Source License

/**
 * ,//w  ww. java2 s . c om
 * 
 * @param fileName
 *            excel, getModelExcel
 * @return 
 * @throws FileNotFoundException 
 */
public Map<String, String> readModelExcel(File file, String modelId) {
    if (modelId == null)
        return null;
    Map<String, String> m = new HashMap<String, String>();
    try {
        InputStream in = new FileInputStream(file);
        Workbook wb;
        try {
            wb = new HSSFWorkbook(in);
        } catch (IllegalArgumentException e) {
            wb = new XSSFWorkbook(in);
        }
        Sheet sheet = wb.getSheetAt(0);
        int total = sheet.getLastRowNum();
        Row row0 = sheet.getRow(0);
        String[] head = new String[row0.getLastCellNum()];
        for (int j = 0; j < row0.getLastCellNum(); j++) {
            String[] str = row0.getCell(j).getStringCellValue().split("/");
            if (str.length == 2) {
                head[j] = str[1];
            } else {
                head[j] = "";
            }
            System.out.println(head[j]);
        }
        Row row = null;
        Cell cell = null;
        for (int i = 1; i < total; i++) {
            m.clear();
            row = sheet.getRow(i);
            for (int j = 0; j < row.getLastCellNum(); j++) {
                cell = row.getCell(j);
                m.put(head[j], cell.getStringCellValue());
                System.out.println(head[j] + "--" + cell.getStringCellValue());
            }
            // ServiceManager.getResourceUpdateService().addResource(modelId,
            // m, 1);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        file.delete();
    }
    return m;
}

From source file:com.hurry.excel.html.Excel2Html.java

License:Apache License

private void printSheetContent(Sheet sheet) {
    printColumnHeads();//www  .j  a  va2  s .c  o  m

    out.format("<tbody>%n");
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();

        out.format("  <tr>%n");
        out.format("    <td class=%s>%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1);
        for (int i = firstColumn; i < endColumn; i++) {
            String content = "&nbsp;";
            String attrs = "";
            CellStyle style = null;
            if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    style = cell.getCellStyle();
                    attrs = tagStyle(cell, style);
                    // Set the value that is rendered for the cell
                    // also applies the format
                    CellFormat cf = CellFormat.getInstance(style.getDataFormatString());
                    CellFormatResult result = cf.apply(cell);
                    content = result.text;
                    if (content.equals(""))
                        content = "&nbsp;";
                }
            }
            out.format("    <td class=%s %s>%s</td>%n", styleName(style), attrs, content);
        }
        out.format("  </tr>%n");
    }
    out.format("</tbody>%n");
}

From source file:com.ibm.db2j.GExcel.java

License:Open Source License

/**
 * Initialize the attributes ://from w w w.  j  a va  2 s.  c  o  m
 * 
 * - inputStream
 * - workbook
 * - evaluator
 * - sheet
 * - firstRowIsMetaData
 * 
 * - firstColumnIndex
 * - firstRowIndex
 * - lastColumnIndex
 * - lastRowIndex
 * 
 * @param fileName
 * @param spreadsheetName
 * @param firstCellRange
 * @param lastCellRange
 * @param interpretFirstLineAsMetaData
 * @throws SQLException
 */
public void initialize(String fileName, String spreadsheetName, String firstCellRange, String lastCellRange,
        boolean interpretFirstLineAsMetaData) throws SQLException {
    try {
        inputStream = new FileInputStream(fileName);
        workbook = WorkbookFactory.create(inputStream);
        evaluator = workbook.getCreationHelper().createFormulaEvaluator();
        sheet = findSpreadsheet(workbook, spreadsheetName);
        firstRowIsMetaData = interpretFirstLineAsMetaData;

        if (firstCellRange != null && lastCellRange != null) {
            CellReference firstCell = new CellReference(firstCellRange);

            // Deduce last row number if it was not specified
            if (lastCellRange.matches("[a-zA-Z]+")) {
                lastCellRange += (sheet.getLastRowNum() + 1); //Note: getLastRowNum is 0-based
                stopScanOnFirstEmptyRow = true;
                logger.logInfo("Deduced last row in Excel table: " + lastCellRange
                        + " - but scans will end on first empty row");
            }

            CellReference lastCell = new CellReference(lastCellRange);

            firstColumnIndex = firstCell.getCol();
            firstRowIndex = firstCell.getRow(); // + (firstRowIsMetaData?1:0);
            lastColumnIndex = lastCell.getCol();
            lastRowIndex = lastCell.getRow();
        } else {
            Row firstRow = locateFirstRow(sheet);

            if (firstRow == null) {
                throw new SQLException("Empty spreadsheet !");
            }

            firstRowIndex = firstRow.getRowNum(); // + (firstRowIsMetaData?1:0);
            lastRowIndex = sheet.getLastRowNum();
            firstColumnIndex = firstRow.getFirstCellNum(); //Note: getFirstCellNum is 0-based
            lastColumnIndex = firstRow.getLastCellNum() - 1; //Note: getLastCellNum is 1-based
        }

        //System.out.println("sheet: " + sheet.getSheetName() + ", firstcolindex: " + firstColumnIndex + ", lastcolindex: " + lastColumnIndex + ", firstrowindex: " + firstRowIndex + ", lastrowindex: " + lastRowIndex);
    } catch (Exception e) {
        throw new SQLException(e.getMessage());
    }
}