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

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

Introduction

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

Prototype

short getFirstCellNum();

Source Link

Document

Get the number of the first cell contained in this row.

Usage

From source file:eu.esdihumboldt.hale.app.bgis.ade.common.AbstractAnalyseTable.java

License:Open Source License

/**
 * Analyzes the table header./*from  w  ww .  ja  va2 s . c  o m*/
 * 
 * @param sheet the table sheet
 */
protected void analyseHeader(Sheet sheet) {
    Row header = sheet.getRow(0);

    // identify columns
    for (int i = header.getFirstCellNum(); i < header.getLastCellNum(); i++) {
        Cell cell = header.getCell(i);
        String text = extractText(cell);

        headerCell(i, text);
    }
}

From source file:examples.toHTML.ToHtml.java

License:Apache License

private void printSheetContent(Sheet sheet) {
    printColumnHeads();//  www .  j  a va  2s.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++) {
            // &nbsp;
            String content = " ";
            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;

                    content = replaceUmlaut(content);

                    if (content.equals(""))
                        // &nbsp;
                        content = " ";
                }
            }
            out.format("    <td class=\"%s %s\">%s</td>%n", styleName(style), attrs, content);
        }
        out.format("  </tr>%n");
    }
    out.format("</tbody>%n");
}

From source file:guru.qas.martini.report.DefaultState.java

License:Apache License

public void updateLongestExecutions() {
    if (!longestExecutionCells.isEmpty()) {
        for (Cell cell : longestExecutionCells) {
            CellStyle original = cell.getCellStyle();
            Sheet sheet = cell.getSheet();
            Workbook workbook = sheet.getWorkbook();
            CellStyle newStyle = workbook.createCellStyle();
            newStyle.cloneStyleFrom(original);
            int originalFontIndex = original.getFontIndexAsInt();
            Font originalFont = workbook.getFontAt(originalFontIndex);

            Font font = workbook.createFont();
            font.setBold(true);//w  w w .ja  v  a2  s.c  o m
            font.setColor(IndexedColors.DARK_RED.getIndex());
            font.setFontHeight((short) Math.round(originalFont.getFontHeight() * 1.5));
            newStyle.setFont(font);
            cell.setCellStyle(newStyle);

            Row row = cell.getRow();
            short firstCellNum = row.getFirstCellNum();
            short lastCellNum = row.getLastCellNum();

            for (int i = firstCellNum; i < lastCellNum; i++) {
                Cell rowCell = row.getCell(i);
                original = rowCell.getCellStyle();
                CellStyle borderStyle = workbook.createCellStyle();
                borderStyle.cloneStyleFrom(original);
                borderStyle.setBorderTop(BorderStyle.MEDIUM);
                borderStyle.setBorderBottom(BorderStyle.MEDIUM);

                if (i == cell.getColumnIndex()) {
                    borderStyle.setBorderLeft(BorderStyle.MEDIUM);
                    borderStyle.setBorderRight(BorderStyle.MEDIUM);
                } else if (i == firstCellNum) {
                    borderStyle.setBorderLeft(BorderStyle.MEDIUM);
                } else if (i == lastCellNum - 1) {
                    borderStyle.setBorderRight(BorderStyle.MEDIUM);
                }
                rowCell.setCellStyle(borderStyle);
            }
        }
    }
}

From source file:guru.qas.martini.report.DefaultState.java

License:Apache License

protected void colorRow(short color, Row row) {
    short firstCellNum = row.getFirstCellNum();
    short lastCellNum = row.getLastCellNum();
    for (int i = firstCellNum; i <= lastCellNum; i++) {
        Cell cell = row.getCell(i);//w  ww  .j a  v  a 2  s.  c  o m
        if (null != cell) {
            CellStyle cellStyle = cell.getCellStyle();
            Workbook workbook = cell.getSheet().getWorkbook();
            CellStyle clone = workbook.createCellStyle();

            clone.cloneStyleFrom(cellStyle);
            clone.setFillForegroundColor(color);
            clone.setFillPattern(FillPatternType.SOLID_FOREGROUND);

            BorderStyle borderStyle = cellStyle.getBorderLeftEnum();
            clone.setBorderLeft(BorderStyle.NONE == borderStyle ? BorderStyle.THIN : borderStyle);
            short borderColor = cellStyle.getLeftBorderColor();
            clone.setLeftBorderColor(0 == borderColor ? IndexedColors.BLACK.getIndex() : borderColor);

            borderStyle = cellStyle.getBorderRightEnum();
            clone.setBorderRight(BorderStyle.NONE == borderStyle ? BorderStyle.THIN : borderStyle);
            borderColor = cellStyle.getRightBorderColor();
            clone.setRightBorderColor(0 == borderColor ? IndexedColors.BLACK.getIndex() : borderColor);

            borderStyle = cellStyle.getBorderTopEnum();
            clone.setBorderTop(BorderStyle.NONE == borderStyle ? BorderStyle.THIN : borderStyle);
            borderColor = cellStyle.getTopBorderColor();
            clone.setTopBorderColor(0 == borderColor ? IndexedColors.BLACK.getIndex() : borderColor);

            borderStyle = cellStyle.getBorderBottomEnum();
            clone.setBorderBottom(BorderStyle.NONE == borderStyle ? BorderStyle.THIN : borderStyle);
            borderColor = cellStyle.getBottomBorderColor();
            clone.setBottomBorderColor(borderColor);
            cell.setCellStyle(clone);
        }
    }
}

From source file:Import.SheetFrameController.java

@Override
public void handle(Event event) {

    if (event.getSource() == listSheet) {

        if (/*listSheet.getSelectionModel().getSelectedIndex() > -1*/ listSheet.getSelectionModel()
                .getSelectedItem() != null) {
            // clear de la liste des columns
            listColumn.getSelectionModel().clearSelection();
            // update de la liste des colonnes
            // rcupration du nom du sheet slectionn
            String sheetName = (String) listSheet.getSelectionModel().getSelectedItem();
            // rcupration du sheet
            Sheet sheet = book.getSheet(sheetName);
            // rcupration des colonnes du sheet
            int top = sheet.getFirstRowNum();
            Row row = sheet.getRow(top);
            // rcupration du nombre de cellule dans la row
            short first = row.getFirstCellNum();
            short last = row.getLastCellNum();
            // on parse la premiure row entre le first et le last
            // cration du arraylist
            ArrayList al = new ArrayList();
            al.clear();/* ww  w .  ja  v a 2s  .  c o  m*/

            // boolean exeption
            boolean catchException = false;

            for (int i = first; i < last; i++) {
                Cell cell = row.getCell(i);
                // on rcupre le nom de la cellule
                try {
                    if (cell.getCellType() == CellType.STRING.getCode()) {

                        String value = cell.getStringCellValue();
                        // on ajoute la valeur dans le arraylist
                        al.add(value);
                    }
                } catch (java.lang.NullPointerException nle) {
                    catchException = true;
                }
            }

            if (catchException)
                this.alertException(
                        "Un probleme est survenu dans la lecture d'une ou plusieurs cellules du fichier");

            // on transverse le arraylist dans le observable list
            ObservableList<String> ol = FXCollections.observableArrayList(al);
            // on attache le ol dans le listColumn
            listColumn.setItems(ol);

        }

    }

    if (event.getSource() == listColumn) {
        if (listColumn.getSelectionModel().getSelectedIndex() > -1) {
            // un item est slectionn dans la liste, on enable le bouton suivant
            bSuiv.setDisable(false);
        } else
            bSuiv.setDisable(true);
    }
}

From source file:invoiceapplication.CopyRowOriginal.java

private static boolean checkIfRowIsEmpty(Row row) {
    if (row == null)
        return true;
    if (row.getLastCellNum() <= 0)
        return true;
    boolean isEmptyRow = true;
    for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
        Cell cell = row.getCell(cellNum);
        if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
            isEmptyRow = false;/*from  ww  w.jav a 2  s. c o m*/
        }
    }
    return isEmptyRow;
}

From source file:io.konig.spreadsheet.WorkbookProcessorImpl.java

License:Apache License

private void assignColumnIndexes(WorkbookSheet s, List<SheetColumn> undeclaredColumns) {
    logger.debug("assignColumnIndexes({})", s.getSheet().getSheetName());
    undeclaredColumns.clear();/* www . java2s. c om*/
    Sheet sheet = s.getSheet();
    SheetProcessor p = s.getProcessor();

    for (SheetColumn c : p.getColumns()) {
        c.setIndex(-1);
    }

    int firstRow = sheet.getFirstRowNum();
    Row row = sheet.getRow(firstRow);

    int colSize = row.getLastCellNum() + 1;
    for (int i = row.getFirstCellNum(); i < colSize; i++) {

        Cell cell = row.getCell(i);
        if (cell != null) {

            String columnName = cellStringValue(cell);
            if (columnName != null) {
                SheetColumn column = p.findColumnByName(columnName);
                if (column != null) {
                    column.setIndex(i);
                    logger.debug("assignColumnIndexes - {} index = {}", column, i);

                } else {
                    SheetColumn c = new SheetColumn(columnName);
                    c.setIndex(i);
                    undeclaredColumns.add(c);
                }
            }
        }
    }

}

From source file:io.konig.spreadsheet.WorkbookProcessorImpl.java

License:Apache License

private int rank(Sheet sheet, SheetProcessor p) throws SpreadsheetException {

    int count = 0;

    int firstRow = sheet.getFirstRowNum();
    Row row = sheet.getRow(firstRow);

    int colSize = row.getLastCellNum() + 1;
    for (int i = row.getFirstCellNum(); i < colSize; i++) {

        Cell cell = row.getCell(i);/*w w w  .j av  a2  s. c  om*/
        if (cell != null) {
            String text = cellStringValue(cell);
            if (text != null) {
                SheetColumn column = p.findColumnByName(text);
                if (column != null) {
                    count++;
                }
            }
        }
    }

    return count;
}

From source file:jexcel4py.Jexcel4py.java

public void deliveryExcel(String strInExcelName) throws IOException {
    wbRead = readExcel(strInExcelName);// www  .  j a v  a  2s .  c o  m
    wbWrite = new HSSFWorkbook();
    Sheet wrSheet = wbWrite.createSheet();
    Sheet rdSheet = wbRead.getSheetAt(0);
    int rdRows = rdSheet.getLastRowNum();// ?
    for (int i = 0; i <= rdRows; i++) {
        Row wrRow = wrSheet.createRow(i);
        Row row = rdSheet.getRow(i);
        for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
            Cell rdCell = row.getCell(j);
            //                System.out.print(rdCell + "\t");

            Cell wrCell = wrRow.createCell(j);
            copyCell(rdCell, wrCell);

        }
        //            System.out.println();
    }

    //        File fileOut = new File(strOutExcelName);
    //        if(fileOut.exists()){
    //            fileOut.delete();
    //        }
    //        FileOutputStream fosFileOut = new FileOutputStream(strOutExcelName);
    //        wbWrite.write(fosFileOut);
    //        fosFileOut.close();
    //        wbRead.close();
    //        wbWrite.close();
}

From source file:massiveanalyserxryv2.ServiceTaskSearch.java

@Override
protected Task createTask() {
    Task task;/*from   w  w w  .  j ava 2  s .  co m*/
    task = new Task() {
        @Override
        protected Object call() throws Exception {
            // rcupration de la liste des mots cls
            // si il s'agit d'une base de donne slectionn dans la liste
            String path;
            if (modelDataSearch.getAbosoluthPathDb() == null) {
                path = System.getProperty("user.dir");
                path = path + "/db/";
                path = path + modelDataSearch.getNameDb();
            } else {
                // sinon on cre le path avec le chemin absolu (fichier import)
                path = modelDataSearch.getAbosoluthPathDb();
            }

            ArrayList<String> keyWords = new ArrayList<String>();
            keyWords.clear();
            // lecture
            for (String line : Files.readAllLines(Paths.get(path))) {
                keyWords.add(line);
            }

            // rcupration de la liste des contents du tableau excel
            Workbook book = WorkbookFactory.create(modelDataSearch.getFile());
            // rcupration du sheet
            Sheet sheet = book.getSheet(modelDataSearch.getNameSheet());
            // rcupration de la colonne
            int top = sheet.getFirstRowNum();
            int down = sheet.getLastRowNum();
            Row row = sheet.getRow(top);
            // on parse les column jusqu'a ce que le nom soit le meme que celui dans le modele
            short start = row.getFirstCellNum();
            short end = row.getLastCellNum();
            int indiceColumn = -1;
            for (short i = start; i <= end; i++) {
                if (row.getCell(i).getStringCellValue().equals(modelDataSearch.getNameColumn())) {
                    // on connait l'indice de column
                    indiceColumn = i;
                    break;
                }
            }

            // cration de la liste des contents
            ArrayList<DataContent> listContent = new ArrayList<DataContent>();
            listContent.clear();
            for (int j = top; j <= down; j++) {
                if (sheet.getRow(j) != null) {
                    if ((sheet.getRow(j).getCell(indiceColumn).getCellType() == CellType.STRING.getCode())) {
                        DataContent data = new DataContent(j,
                                sheet.getRow(j).getCell(indiceColumn).getStringCellValue());
                        listContent.add(data);
                    }
                }
            }

            // recherches
            for (DataContent content : listContent) {
                for (String key : keyWords) {
                    if (key.isEmpty())
                        continue;

                    int res = content.getContent().toLowerCase().indexOf(key.toLowerCase());
                    if (res != -1) {
                        DataResultat data = new DataResultat(content.getNumRow() + 1, content.getContent(),
                                key); // +1 car dans le fichie excel les row commence  1 et pas  0
                        ob.add(data);
                    }
                }
            }

            // Fermeture du workbook
            if (book != null)
                book.close();
            //tableauResultat.setItems(ob);
            return (Object) ob;

        }

    };

    return task;
}