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:poi.hssf.view.SVTableModel.java

License:Apache License

public SVTableModel(HSSFSheet st) {
    this.st = st;
    Iterator i = st.rowIterator();

    while (i.hasNext()) {
        HSSFRow row = (HSSFRow) i.next();
        if (maxcol < (row.getLastCellNum() + 1)) {
            this.maxcol = row.getLastCellNum();
        }/*w ww  .  j  a v a  2 s .co  m*/
    }
}

From source file:POI.Sheet.java

/**
 * ??//from  w  ww.j a  va2s.c  om
 *
 * @return 
 * @see HSSFSheet
 */
public int getColumnSize() {
    HSSFRow row = sheet.getRow(this.getFirstRowNum());
    return row.getLastCellNum() - row.getFirstCellNum();
}

From source file:ro.nextreports.engine.exporter.util.XlsUtil.java

License:Apache License

/**
 * Copy a sheet to another sheet at a specific (row, column) position
 * //from   w w w.ja v a 2  s  .  c  o m
 * @param parentSheet the sheet to copy into
 * @param parentSheetRow the row inside parentSheet where we start to copy
 * @param parentSheetColumn the column inside parentSheet where we start to copy
 * @param sheet the sheet that is copied
 * @param copyStyle true to copy the style
 * @return column number
 */
public static int copyToSheet(HSSFSheet parentSheet, int parentSheetRow, int parentSheetColumn, HSSFSheet sheet,
        boolean copyStyle) {
    int maxColumnNum = 0;
    Map<Integer, HSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, HSSFCellStyle>() : null;
    for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
        HSSFRow srcRow = sheet.getRow(i);
        HSSFRow destRow;
        // subreport is not the first cell in a row
        if ((parentSheetColumn > 0) && (i == sheet.getFirstRowNum())) {
            destRow = parentSheet.getRow(parentSheetRow);
        } else {
            destRow = parentSheet.getRow(parentSheetRow + i);
            if (destRow == null) {
                destRow = parentSheet.createRow(parentSheetRow + i);
            }
        }
        if (srcRow != null) {
            XlsUtil.copyRow(sheet, parentSheet, parentSheetRow, parentSheetColumn, srcRow, destRow, styleMap);
            if (srcRow.getLastCellNum() > maxColumnNum) {
                maxColumnNum = srcRow.getLastCellNum();
            }
        }
    }
    for (int i = 0; i <= maxColumnNum; i++) {
        parentSheet.setColumnWidth(i, sheet.getColumnWidth(i));
    }
    return maxColumnNum;
}

From source file:ro.nextreports.engine.exporter.util.XlsUtil.java

License:Apache License

/**
 * Copy a row from a sheet to another sheet
 * /*  w  w w. j  a  v a2  s .  co m*/
 * 
 * @param srcSheet the sheet to copy
 * @param destSheet the sheet to copy into
 * @param parentSheetRow the row inside destSheet where we start to copy
 * @param parentSheetColumn the column inside destSheet where we start to copy
 * @param srcRow the row to copy
 * @param destRow the row to create
 * @param styleMap style map
 *       
 */
public static void copyRow(HSSFSheet srcSheet, HSSFSheet destSheet, int parentSheetRow, int parentSheetColumn,
        HSSFRow srcRow, HSSFRow destRow, Map<Integer, HSSFCellStyle> styleMap) {
    // manage a list of merged zone in order to not insert two times a
    // merged zone
    Set<CellRangeAddressWrapper> mergedRegions = new TreeSet<CellRangeAddressWrapper>();
    destRow.setHeight(srcRow.getHeight());
    // pour chaque row
    for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
        HSSFCell oldCell = srcRow.getCell(j); // ancienne cell         
        if (oldCell != null) {
            HSSFCell newCell = destRow.createCell(parentSheetColumn + j);
            copyCell(oldCell, newCell, styleMap);

            CellRangeAddress mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(),
                    (short) oldCell.getColumnIndex());

            if (mergedRegion != null) {

                CellRangeAddress newMergedRegion = new CellRangeAddress(
                        parentSheetRow + mergedRegion.getFirstRow(), parentSheetRow + mergedRegion.getLastRow(),
                        parentSheetColumn + mergedRegion.getFirstColumn(),
                        parentSheetColumn + mergedRegion.getLastColumn());

                CellRangeAddressWrapper wrapper = new CellRangeAddressWrapper(newMergedRegion);
                if (isNewMergedRegion(wrapper, mergedRegions)) {
                    mergedRegions.add(wrapper);
                    destSheet.addMergedRegion(wrapper.range);
                }
            }
        }
    }

}

From source file:senselogic.excelbundle.ExcelImporter.java

License:Apache License

/**
 * Returns the first index of the specified value on the row or -1 if there
 * is no such value./*www . j  a  va  2 s  . com*/
 */
private int getIndexOf(HSSFRow r, String value) {
    short lastCell = r.getLastCellNum();
    for (int i = 1; i <= lastCell; i++) {
        String rowValue = getString(r.getCell(i));
        if (value.equals(rowValue))
            return i;
    }
    return -1;
}

From source file:test.ExcelUtil.java

License:Apache License

/**
 * //  w  w  w .  j av  a  2 s. c o m
 * , excel
 * @param row
 * @return
 */
public static boolean isEmptyRow(HSSFRow row) {
    boolean result = true;
    if (row == null) {
        result = true;
    } else {
        for (short i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
            HSSFCell cell = row.getCell(i);
            result &= isEmptyCell(cell);
            if (!result) {
                break;
            }
        }
    }
    return result;
}

From source file:test.gov.nih.nci.cacoresdk.PropertyGenerator.java

License:BSD License

@SuppressWarnings("deprecation")
public void generateConfigurationFiles(String confFileName, String outputDir) throws Exception {
    InputStream configFile = new FileInputStream(confFileName);
    HSSFWorkbook wb = new HSSFWorkbook(configFile);
    HSSFSheet sheet = wb.getSheetAt(0);//from   w  w w  .jav a2  s . c o m
    int rowNum = sheet.getLastRowNum();
    HSSFRow row = sheet.getRow(0);
    int colNum = row.getLastCellNum();

    ArrayList<String> names = new ArrayList<String>();
    for (int i = 0; i < colNum; i++) {
        HSSFCell currentCell = row.getCell(i);
        if (currentCell == null) {
            names.add("");
        } else {
            currentCell.setCellType(currentCell.CELL_TYPE_STRING);
            names.add(currentCell.toString());
        }
    }
    /*
     * First Line includes names for all different configurations   (Requirement 1)
     * Second column is the default value  (Requirement 2)
     * From 3 - colNum are names of different configurations. (Requirement 3) 
     */

    int currentRowNum = 1;

    /*
     * Loop through xls file to generate properties files
     */

    while (currentRowNum <= rowNum && countRow(sheet.getRow(currentRowNum++)) == 1) {
        ArrayList<String> propertiesNames = new ArrayList<String>();

        String filename = getFirstString(sheet.getRow(currentRowNum - 1));

        int rowCount = 0;
        int headerRow = currentRowNum - 1;
        while (currentRowNum <= rowNum && countRow(sheet.getRow(currentRowNum++)) != 1) {
            rowCount++;
        }

        ArrayList<Properties> properties = new ArrayList<Properties>();
        properties.add(new Properties());
        for (int i = 1; i < colNum; i++) {
            Properties prop = new Properties();
            properties.add(prop);
        }

        currentRowNum = headerRow + 1;
        for (int i = currentRowNum; i < currentRowNum + rowCount; i++) {
            HSSFRow currentRow = sheet.getRow(i);
            if (currentRow == null)
                continue;
            if (currentRow.getCell(0) == null)
                continue;
            String propertyName = currentRow.getCell(0).getStringCellValue();
            propertiesNames.add(propertyName);
            for (int j = 1; j < colNum; j++) {
                Properties currentP = properties.get(j);
                HSSFCell currentCell = currentRow.getCell(j);
                if (currentCell == null) {
                    currentP.put(propertyName, "");
                } else {
                    currentCell.setCellType(currentCell.CELL_TYPE_STRING);
                    currentP.put(propertyName, currentCell.toString());
                }
            }
        }
        currentRowNum = currentRowNum + rowCount;

        /*
         * Merge default properties with other properties
         */
        for (int j = 2; j < colNum; j++) {
            Properties defaultP = properties.get(1);
            Properties currentP = properties.get(j);
            Set<Object> propertyNames = defaultP.keySet();
            for (Object propertyKey : propertyNames) {
                String key = (String) propertyKey;
                if (currentP.getProperty(key).equals(""))
                    currentP.setProperty(key, defaultP.getProperty(key));
            }

        }

        for (int j = 1; j < colNum; j++) {
            File directory = new File(outputDir + "/" + names.get(j));
            directory.mkdirs();
            File f = new File(directory, filename);
            Properties currentP = properties.get(j);

            saveProperties(propertiesNames, currentP, f);
        }
    }
}

From source file:test.gov.nih.nci.cacoresdk.PropertyGenerator.java

License:BSD License

private int countRow(HSSFRow row) {
    int nonEmptyCount = 0;
    if (row == null)
        return 0;
    for (int i = 0; i < row.getLastCellNum(); i++) {
        HSSFCell currentCell = row.getCell(i);
        if (currentCell != null) {
            currentCell.setCellType(currentCell.CELL_TYPE_STRING);
            if (!(currentCell.toString().equals("")))
                nonEmptyCount++;// w ww .  j  av  a 2s. c o m
        }
    }
    return nonEmptyCount;
}

From source file:test.gov.nih.nci.cacoresdk.PropertyGenerator.java

License:BSD License

private String getFirstString(HSSFRow row) {
    for (int i = 0; i < row.getLastCellNum(); i++) {
        HSSFCell currentCell = row.getCell(i);
        if (currentCell != null) {
            currentCell.setCellType(currentCell.CELL_TYPE_STRING);
            if (!(currentCell.toString().equals("")))
                return currentCell.toString();
        }/*  ww  w  .  j  a  v  a  2s. c  o  m*/
    }
    return "";
}

From source file:ugh.fileformats.excel.Excelfile.java

License:Open Source License

/***************************************************************************
 * @param inStruct/*from w  w  w.  ja  va2 s.c  o  m*/
 * @param reverse
 * @param rowCounter
 * @return
 **************************************************************************/
private boolean UpdateAllMetadata(DocStruct inStruct, boolean reverse, int rowCounter) {
    // First, check in which row we have to store information use
    // information from allDocStruct and allStructRow which had been filled
    // while reading the excel spredsheet.
    String currentsheet = null;
    int currentrow = 0;
    // We have to put it into comment, because allDocStruct.size is always
    // different from allStructRow.size, because maindocstruct and anchor
    // are also in the allDocStruct.
    for (int j = 0; j < this.allDocStruct.size(); j++) {
        DocStruct singledoc = this.allDocStruct.get(j);
        if (singledoc.equals(inStruct)) {
            // It is the correct docstruct, so get row and sheet.
            currentrow = Integer.parseInt(this.allStructRow.get(j));
            currentsheet = this.allStructSheets.get(j);
            break;
        }
    }
    if (currentrow == 0) {
        // We haven't found the inStruct in the structure maybe inStruct was
        // added after reading the excel sheet.
        return false;
    }
    if (currentsheet == null) {
        System.err.println("DEBUG: unknown sheet in Excel file");
        return false;
    }
    if (currentsheet.equals("Bibliographie")) {
        System.out.println("updating in Sheet \"Bibliographie\"!");
    }
    if (currentsheet.equals("Gliederung")) {
        List<Metadata> allMD = inStruct.getAllMetadata();
        // Count the row in the excel spreadsheet.
        rowCounter++;
        for (int i = 0; i < allMD.size(); i++) {
            Metadata md = allMD.get(i);
            MetadataType mdt = md.getType();
            if (mdt == null) {
                return false;
            }
            if (md.getType().getName().startsWith("_")) {
                // It's internal metadata, so we have to get out of loop, we
                // do not have to store internal metadata in excel sheet.
                continue;
            }
            if (md.wasUpdated()) {
                // Metadata field was updated; we have to update the
                // spreadsheet cell.
                Object mdnatobj = md.getNativeObject();
                if (mdnatobj == null) {
                    // No object is available.
                    if (md.getValue() != null) {
                        // We have no cell, but a metadata value; so we have
                        // to find the cell (column) and add it to the cell.
                        HSSFSheet inSheet = this.excelworkbook.getSheet(currentsheet);
                        org.apache.poi.hssf.usermodel.HSSFRow secondRow = inSheet.getRow(1);
                        int currentcolumn = 0;
                        int from = secondRow.getFirstCellNum();
                        int to = secondRow.getLastCellNum();
                        for (int k = from; k < to + 1; k++) {
                            HSSFCell currentCell = secondRow.getCell((short) (k));
                            String currentValue = null;
                            if ((currentCell != null)
                                    && (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING)) {
                                currentValue = currentCell.getStringCellValue();
                            }
                            if (currentValue != null) {
                                currentValue.trim();
                                MetadataType columnmdt = getMDTypeByName(currentValue, "excelGliederung");
                                if ((columnmdt != null) && (columnmdt.getName().equals(mdt.getName()))) {
                                    // We found a column which has a
                                    // metadatatype.
                                    currentcolumn = k;
                                    // Get out of loop, we found the column
                                    // for metadata.
                                    break;
                                }
                            }
                        }
                        if (currentcolumn == 0) {
                            // Metadata column wasn't found.
                            System.err.println("DEBUG: column couldn' be found");
                            return false;
                        }
                        // currentrow and currentcolumn contains the cell
                        // coordinates.
                        org.apache.poi.hssf.usermodel.HSSFRow cellRow = inSheet.getRow(currentrow);
                        HSSFCell currentcell = cellRow.getCell((short) (currentcolumn));
                        if (currentcell == null) {
                            // Cell doesn't exists, so we create a new cell.
                            currentcell = cellRow.createCell((short) (currentcolumn));
                            System.err.println(
                                    "excel cell at " + currentrow + "/" + currentcolumn + " (r/c) is null");
                        }
                        // Update the value.
                        currentcell.setCellValue(md.getValue());
                        continue;
                    }
                    // No metadata value and no object.
                    continue;
                }
                if (mdnatobj.getClass().getName().equals("HSSFCell")) {
                    HSSFCell mdcell = (HSSFCell) mdnatobj;
                    if (md.getValue() == null) {
                        mdcell.setCellValue("");
                    } else {
                        mdcell.setCellValue(md.getValue());
                    }
                } else {
                    // Wrong native object; not an excel spreadsheet we
                    // should throw an exception here.
                    return false;
                }
            }
        }
    }
    if (reverse) {
        // All children.
        List<DocStruct> allChildren = inStruct.getAllChildren();
        if (allChildren == null) {
            // No children, so we can get out.
            return true;
        }
        for (int i = 0; i < allChildren.size(); i++) {
            DocStruct child = allChildren.get(i);
            if (!UpdateAllMetadata(child, true, rowCounter)) {
                return false;
            }
        }
    }

    return true;
}