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:ugh.fileformats.excel.Excelfile.java

License:Open Source License

/***************************************************************************
 * Read paginiation sequences from Excel sheet and creates physical
 * docstruct entities (one for each page) and creates Image-instances
 * //from w w  w.  j  av  a 2  s.c  o  m
 * @param inSheet
 *            single sheet of a whole excel file containing the pagination
 *            sequences
 * @param pathasstring
 * @return true, if everthing is okay; otherwise false
 * @throws MetadataTypeNotAllowedException
 **************************************************************************/
private boolean ReadPaginationSequences(HSSFSheet inSheet, String pathasstring)
        throws MetadataTypeNotAllowedException {

    DocStruct boundbook = this.mydoc.getPhysicalDocStruct();

    double oldPhysicalend = 0;
    // Positions of appropriate columns in the spreadsheet.
    int countedstartpageCol = 0;
    int countedendpageCol = 0;
    int uncountedstartpageCol = 0;
    int uncountedendpageCol = 0;
    int formatCol = 0;
    // Contains all pagination sequences; will be used later when reading
    // the hierarchy.
    this.allPaginations = new LinkedList<PaginationSequence>();

    // Get column's names and positions from the second row in the
    // spreadsheet.
    org.apache.poi.hssf.usermodel.HSSFRow secondRow = inSheet.getRow(1);
    int from = secondRow.getFirstCellNum();
    int to = secondRow.getLastCellNum();
    for (int i = from; i < to + 1; i++) {
        HSSFCell currentCell = secondRow.getCell((short) (i));

        if ((currentCell != null) && (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING)) {
            String currentValue = currentCell.getStringCellValue();
            if ((currentValue != null) && (currentValue.length() >= 7)
                    && (currentValue.substring(0, 7).equals("GSEIT_S"))) {
                countedstartpageCol = i;
                continue;
            }
            if ((currentValue != null) && (currentValue.length() >= 7)
                    && (currentValue.substring(0, 7).equals("GSEIT_E"))) {
                countedendpageCol = i;
                continue;
            }
            if ((currentValue != null) && (currentValue.length() >= 7)
                    && (currentValue.substring(0, 7).equals("USEIT_S"))) {
                uncountedstartpageCol = i;
                continue;
            }
            if ((currentValue != null) && (currentValue.length() >= 7)
                    && (currentValue.substring(0, 7).equals("USEIT_E"))) {
                uncountedendpageCol = i;
                continue;
            }
            if ((currentValue != null) && (currentValue.length() >= 7)
                    && (currentValue.substring(0, 7).equals("FRMT_S"))) {
                formatCol = i;
                continue;
            }
        }
    }

    // Now we can begin to read the pagination sequences; we'll start from
    // third row.
    for (int x = 2; x < inSheet.getPhysicalNumberOfRows(); x++) {
        // Each row is one pagination sequence.
        org.apache.poi.hssf.usermodel.HSSFRow currentRow = inSheet.getRow(x);
        // Get cell values.
        HSSFCell countedstartpagecell = currentRow.getCell((short) countedstartpageCol);
        HSSFCell countedendpagecell = currentRow.getCell((short) countedendpageCol);
        HSSFCell uncountedstartpagecell = currentRow.getCell((short) uncountedstartpageCol);
        HSSFCell uncountedendpagecell = currentRow.getCell((short) uncountedendpageCol);
        HSSFCell formatcell = currentRow.getCell((short) formatCol);
        // These variables are for one pagination sequence.
        double numpages = 0;
        double uncountedstartpage = 0;
        double uncountedendpage = 0;
        double countedstartpage = 0;
        double countedendpage = 0;
        String pageformat = null;

        // Check if we have to go out of loop, cause entries are empty.
        if ((countedstartpagecell == null) || (countedendpagecell == null)) {
            // Get out of loop; no value in start or endpage available; must
            // be the last one.
            break;
        }
        if ((countedstartpagecell.getCellType() == HSSFCell.CELL_TYPE_BLANK)
                || (countedendpagecell.getCellType() == HSSFCell.CELL_TYPE_BLANK)) {
            break;
        }

        // Get cell values.
        if ((countedstartpagecell != null)
                && (countedstartpagecell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)) {
            countedstartpage = countedstartpagecell.getNumericCellValue();
        } else {
            if (countedstartpagecell != null) {
                System.err.println(
                        "WARNING: value for counted page start in Pagination sequences is NOT numeric (" + x
                                + ")");
            } else {
                System.err.println("WARNING: value for counted page start has no value in Pagination Sequence ("
                        + x + ")");
            }
        }
        if ((countedendpagecell != null) && (countedendpagecell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)) {
            countedendpage = countedendpagecell.getNumericCellValue();
        } else {
            if (countedendpagecell != null) {
                System.err
                        .println("WARNING: value for counted page end in Pagination sequences is NOT numeric ("
                                + x + ")");
            } else {
                System.err.println("WARNING: counted endpage has no value... (" + x + ")");
            }
        }
        if ((uncountedstartpagecell != null)
                && (uncountedstartpagecell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)) {
            uncountedstartpage = uncountedstartpagecell.getNumericCellValue();
        } else {
            if ((uncountedstartpagecell != null)
                    && (uncountedstartpagecell.getCellType() != HSSFCell.CELL_TYPE_BLANK)) {
                System.err.println(
                        "WARNING: value for uncounted startpage in Pagination sequences is NOT numeric (" + x
                                + ")");
            }
        }
        if ((uncountedendpagecell != null)
                && (uncountedendpagecell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)) {
            uncountedendpage = uncountedendpagecell.getNumericCellValue();
        } else {
            if ((uncountedendpagecell != null)
                    && (uncountedendpagecell.getCellType() != HSSFCell.CELL_TYPE_BLANK)) {
                System.err
                        .println("WARNING: value for uncounted endpage in Pagination sequences is NOT numeric ("
                                + x + ")");
            }
        }
        // Seitenformat.
        if ((formatcell != null) && ((formatcell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
                || (formatcell.getCellType() == HSSFCell.CELL_TYPE_FORMULA))) {
            pageformat = "1";
        }
        if ((formatcell != null) && (formatcell.getCellType() == HSSFCell.CELL_TYPE_STRING)) {
            pageformat = formatcell.getStringCellValue();
            if (!pageformat.equalsIgnoreCase("R")) {
                System.err.println("WARNING: unknown page format - neither arabic nor roman... (" + x + ")");
            }
            pageformat = "R";
        }
        if ((formatcell != null) && (formatcell.getCellType() == HSSFCell.CELL_TYPE_BLANK)) {
            // Blank cell.
            pageformat = "1";
        }
        if (formatcell == null) {
            // Assume that it's an arabic number, when no value was set.
            pageformat = "1";
        }
        // Calculate physical start and endpages.
        if (((uncountedstartpage == 0) && (uncountedendpage != 0))
                || ((uncountedstartpage != 0) && (uncountedendpage == 0))) {
            System.err.println("WARNING: uncounted start or endpage is NOT set... (" + x + ")");
            continue;
        }
        if ((countedstartpage > countedendpage) || (uncountedstartpage > uncountedendpage)) {
            System.err.println("WARNING: startpage is larger then endpage (" + x + ")");
        }
        if (uncountedstartpage == 0) {
            numpages = countedendpage - countedstartpage;
        } else {
            numpages = uncountedendpage - uncountedstartpage;
        }

        double physicalstart = oldPhysicalend + 1;
        double physicalend = physicalstart + numpages;
        oldPhysicalend = physicalend;

        // Create pagination sequence.
        PaginationSequence ps = new PaginationSequence(this.myPreferences);
        if ((countedstartpage == countedendpage) && (uncountedstartpage != 0) && (uncountedendpage != 0)) {
            // A sequence of uncounted pages.
            ps.logcountedstart = 0;
            ps.logcountedend = 0;
            ps.lognotcountedstart = (int) uncountedstartpage;
            ps.lognotcountedend = (int) uncountedendpage;
            ps.pageformatnumber = pageformat;
        }
        if (countedstartpage != countedendpage) {
            // A sequence of counted pages.
            if (uncountedstartpage != uncountedendpage) {
                System.err.println(
                        "WARNING: counted page sequence can be may be an uncounted sequence... (" + x + ")");
            }
            ps.logcountedstart = (int) countedstartpage;
            ps.logcountedend = (int) countedendpage;
            ps.pageformatnumber = pageformat;
        }
        ps.physicalstart = (int) physicalstart;
        ps.physicalend = (int) physicalend;
        // Convert pagination sequence to physical strucutre; get a list of
        // physical structures
        LinkedList<?> pages = ps.ConvertToPhysicalStructure(this.mydoc);
        this.allPaginations.add(ps);

        // Add number of pagination sequence necessary to calculate physical
        // page number in Gliederung.
        MetadataType seqno = new MetadataType();
        seqno.setName("_PaginationNo");
        for (int u = 0; u < pages.size(); u++) {
            DocStruct page = (DocStruct) pages.get(u);
            Metadata md = new Metadata(seqno);
            md.setValue(Integer.toString(x - 1));
            try {
                page.addMetadata(md);
                // Add the single page to the uppermost physical structure
                // (bound book).
                boundbook.addChild(page);
            } catch (TypeNotAllowedAsChildException tnaace) {
                System.err.println("ERROR: ReadPaginationSequences: Can't add pages to BoundBook");
                return false;
            } catch (MetadataTypeNotAllowedException mtnae) {
                System.err.println(
                        "ERROR: ReadPaginationSequences: Can't read metadata; metadata type not allowed!");
                System.err.println("       " + md.getType().getName() + " can't be added for DocStruct "
                        + page.getType().getName());
                return false;
            }
        }
    }

    // Create top physical document structure (BoundBook)
    if ((boundbook.getAllChildren() == null) || (boundbook.getAllChildren().size() == 0)) {
        System.out.println("DEBUG: rdffile.ReadPagSequence: No pages available...");
        return false;
    }

    // Add page information to parent logical structure.
    DocStruct parent = this.mydoc.getLogicalDocStruct();
    if (parent == null) {
        System.out.println(
                "ERROR: Excelfile.ReadPaginationSequences: Can't find any parent element on topmost level");
        return false;
    }

    DocStructType parentType = parent.getType();
    if (parentType.getAnchorClass() != null) {
        // It's an anchor (e.g. a periodical...) so we cannot add any
        // children to this but we must get the next structure entity
        // (child) - e.g. the volume.
        List<DocStruct> children = parent.getAllChildren();
        if (children == null) {
            System.out.println("ERROR: ReadPaginationSequences: Parent is anchor but has no child");
            return false;
        }
        // Get first child as new parent.
        parent = children.get(0);
    }
    List<DocStruct> allpages = boundbook.getAllChildren();
    for (int i = 0; i < allpages.size(); i++) {
        // Get single node.
        DocStruct currentPage = allpages.get(i);
        parent.addReferenceTo(currentPage, "logical_physical");
        currentPage.addReferenceFrom(parent, "physical_physical");
    }
    // Add internal metadata: start and endpage.
    MetadataType endpagemd = this.myPreferences.getMetadataTypeByName("_pagephysend");
    Metadata mdnew = new Metadata(endpagemd);
    mdnew.setValue(Integer.toString(allpages.size()));

    try {
        // Add physical page number.
        parent.addMetadata(mdnew);
    } catch (MetadataTypeNotAllowedException mtnae) {
        System.err.println("ERROR: ReadPaginationSequences: Can't add metadata");
        System.err.println(
                "       " + mdnew.getType().getName() + " can't be added to " + parent.getType().getName());
        return false;
    }
    MetadataType startpagemd = this.myPreferences.getMetadataTypeByName("_pagephysstart");
    mdnew = new Metadata(startpagemd);
    // Physical page number begins always with 1.
    mdnew.setValue("1");

    try {
        // Add physical page number.
        parent.addMetadata(mdnew);
    } catch (MetadataTypeNotAllowedException mtnae) {
        System.err.println("ERROR: ReadPaginationSequences: Can't add metadata");
        System.err.println(
                "       " + mdnew.getType().getName() + " can't be added to " + parent.getType().getName());
        return false;
    }

    // Create imageset; every page one image.
    if (this.myImageset == null) {
        this.myImageset = new ugh.dl.FileSet();
    }

    // NOT FINISHED
    //
    // Create File objects for images.
    for (int i = 0; i < allpages.size(); i++) {
        DocStruct currentPage = allpages.get(i);

        // Create new Image object and add it to myImageSet.
        ugh.dl.ContentFile newimage = new ugh.dl.ContentFile();
        String filename = "";

        ugh.dl.MetadataType MDT2 = this.myPreferences.getMetadataTypeByName("physPageNumber");
        List<? extends Metadata> physpagelist = currentPage.getAllMetadataByType(MDT2);
        int physpage = 0;
        for (Metadata md : physpagelist) {
            try {
                physpage = Integer.parseInt(md.getValue());
            } catch (Exception e) {
                System.err.println("ERROR: physical page number seems to be a non integer value!!");
                return false;
            }
        }

        if (physpage < 100000) {
            filename = "000" + physpage + ".tif";
        }
        if (physpage < 10000) {
            filename = "0000" + physpage + ".tif";
        }
        if (physpage < 1000) {
            filename = "00000" + physpage + ".tif";
        }
        if (physpage < 100) {
            filename = "000000" + physpage + ".tif";
        }
        if (physpage < 10) {
            filename = "0000000" + physpage + ".tif";
        }
        newimage.setLocation(pathasstring + "/" + filename);
        newimage.setMimeType("image/tiff");
        // Add the file to the imageset.
        this.myImageset.addFile(newimage);
        // Add contentFile to page.
        currentPage.addContentFile(newimage);
    }
    this.mydoc.setPhysicalDocStruct(boundbook);
    this.mydoc.setFileSet(this.myImageset);

    return true;
}

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

License:Open Source License

/***************************************************************************
 * Reads the logical structure of a work from spreadsheet table "Gliederung"
 * The content is attached to the "maindocstruct", which must already be
 * available e.g. be reading the Bibliography first
 * // w w  w .  j a v  a2s .co  m
 * @param inSheet
 *            of the table "Gliederung"
 * @return true, everything is okay; otherwise false
 * @throws TypeNotAllowedForParentException
 * @throws MetadataTypeNotAllowedException
 * 
 **************************************************************************/
private boolean ReadGliederung(HSSFSheet inSheet)
        throws TypeNotAllowedForParentException, MetadataTypeNotAllowedException {
    int structtypeCol = -1; // position of structure type column
    int sequenceCol = -1;
    int levelCol = -1;
    int countedstartpageCol = -1;
    int uncountedstartpageCol = -1;
    int ueberlappungCol = -1;

    // Contains the column number for a metadata type.
    LinkedList<String> metadataColumn = new LinkedList<String>();
    // Contains the name of a metadata type (as String).
    LinkedList<String> metadataType = new LinkedList<String>();

    // Try to get the column-positions according to their names.
    org.apache.poi.hssf.usermodel.HSSFRow secondRow = inSheet.getRow(1);
    int from = secondRow.getFirstCellNum();
    int to = secondRow.getLastCellNum();
    for (int i = from; i < to + 1; i++) {
        HSSFCell currentCell = secondRow.getCell((short) (i));

        if ((currentCell != null) && (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING)) {
            String currentValue = currentCell.getStringCellValue();

            // Checking for length of string and comapre only the first x
            // chars is necessary, because OpenOffice's excel files may
            // contain whitespaces after the cell-content. I'm not sure,
            // wether only whitespaces or also other characters may occur...
            if ((currentValue != null) && (currentValue.length() >= 8)
                    && (currentValue.substring(0, 8).equals("STRCT_EL"))) {
                structtypeCol = i;
                continue;
            }
            if ((currentValue != null) && (currentValue.length() >= 5)
                    && (currentValue.substring(0, 5).equals("LEVEL"))) {
                levelCol = i;
                continue;
            }
            if ((currentValue != null) && (currentValue.length() >= 3)
                    && (currentValue.substring(0, 3).equals("SEQ"))) {
                sequenceCol = i;
                continue;
            }
            if ((currentValue != null) && (currentValue.length() >= 7)
                    && (currentValue.substring(0, 7).equals("GSEIT_S"))) {
                countedstartpageCol = i;
                continue;
            }
            if ((currentValue != null) && (currentValue.length() >= 7)
                    && (currentValue.substring(0, 7).equals("USEIT_S"))) {
                uncountedstartpageCol = i;
                continue;
            }
            if ((currentValue != null) && (currentValue.length() >= 12)
                    && (currentValue.substring(0, 12).equals("Ueberlappung"))) {
                ueberlappungCol = i;
                continue;
            }

            // Check metadata columns; these are configurable using the
            // language excel:Gliederung.
            if (currentValue != null) {
                currentValue.trim();
                MetadataType columnmdt = getMDTypeByName(currentValue, "ExcelGliederung");
                if (columnmdt != null) {
                    // We found a column which has a metadatatype.
                    metadataType.add(currentValue);
                    metadataColumn.add(Integer.toString(i));
                }
            }
        }
    }

    // Now we can begin to read the contents.
    //
    // Read DocStructs.
    //
    DocStruct alllevels[] = { null, null, null, null, null, null, null, null, null, null, null, null };
    int oldhierarchy = 0;
    // Each row is one pagination sequence.
    for (int x = 2; x < inSheet.getPhysicalNumberOfRows(); x++) {
        HSSFCell levelcell = null;
        HSSFCell structtypecell = null;
        HSSFCell sequencecell = null;
        HSSFCell countedstartpagecell = null;
        HSSFCell uncountedstartpagecell = null;
        HSSFCell ueberlappungcell = null;

        org.apache.poi.hssf.usermodel.HSSFRow currentRow = inSheet.getRow(x);
        if (structtypeCol > -1) {
            structtypecell = currentRow.getCell((short) structtypeCol);
        } else {
            System.err.println("ERROR: Can't find column 'STRCT_EL'");
            return false;
        }
        if (sequenceCol > -1) {
            sequencecell = currentRow.getCell((short) sequenceCol);
        } else {
            System.err.println("ERROR: Can't find column 'SEQ'");
            return false;
        }
        if (countedstartpageCol > -1) {
            countedstartpagecell = currentRow.getCell((short) countedstartpageCol);
        } else {
            System.err.println("ERROR: Can't find column 'GSEIT_S'");
            return false;
        }
        if (uncountedstartpageCol > -1) {
            uncountedstartpagecell = currentRow.getCell((short) uncountedstartpageCol);
        } else {
            System.err.println("ERROR: Can't find column 'USEIT_S'");
            return false;
        }

        if (levelCol > -1) {
            levelcell = currentRow.getCell((short) levelCol);
        }
        if (ueberlappungCol > -1) {
            ueberlappungcell = currentRow.getCell((short) ueberlappungCol);
        }

        // Get cells for metadata and store the cells in a LinkedList.
        LinkedList<HSSFCell> metadataCells = new LinkedList<HSSFCell>();
        for (int u = 0; u < metadataColumn.size(); u++) {
            int column = Integer.parseInt(metadataColumn.get(u));
            HSSFCell metadatacell = currentRow.getCell((short) column);
            metadataCells.add(metadatacell);
        }

        // Read values and create DocStruct object.
        int hierarchy = 0;
        String type = null;
        String overlapping = null;
        int sequence = 0;
        int countedpage = 0;
        int uncountedpage = 0;

        // Get hierarchy.
        //
        // Level-column is available.
        if ((levelCol > -1) && (levelcell != null)) {
            if (levelcell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                hierarchy = (int) levelcell.getNumericCellValue();
            } else {
                if (levelcell.getCellType() != HSSFCell.CELL_TYPE_BLANK) {
                    System.err.println("ERROR: Value of hierachy is NOT numeric (1) - line " + x);
                }
                continue;
            }
            // Level information is stored in type value.
        } else {
            if ((structtypecell != null) && (structtypecell.getCellType() == HSSFCell.CELL_TYPE_STRING)) {
                type = structtypecell.getStringCellValue();
                // Needed to read OpenOffice excel files...
                type = TrimString(type);
            } else {
                if ((structtypecell != null) && (structtypecell.getCellType() != HSSFCell.CELL_TYPE_BLANK)) {
                    System.err.println(
                            "ERROR: Unknown cell type for structure entity type (not a string) - line " + x);
                }
                continue;
            }
            // Separate level information from structtype.
            for (int z = 0; z < type.length(); z++) {
                // Position of space.
                int spacepos = type.indexOf(" ");
                String hierarchystring = type.substring(0, spacepos);
                hierarchy = Integer.parseInt(hierarchystring);
                type = type.substring(spacepos + 1);
            }
        }
        // Get type, but only if we don't have a type already.
        if ((type == null) && (structtypecell != null)) {
            if (structtypecell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                type = structtypecell.getStringCellValue();
                // Needed to read OpenOffice excel files...
                type = TrimString(type);
            } else {
                System.err.println("ERROR: Wrong value for structure cell - line " + x);
                continue;
            }
        }
        // Get start sequence.
        if ((sequencecell != null) && (sequencecell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)) {
            sequence = (int) sequencecell.getNumericCellValue();
        } else {
            System.err.println("ERROR: Can't find pagination sequence for start page - line " + x);
            continue;
        }
        // Get counted start page.
        if ((countedstartpagecell != null)
                && (countedstartpagecell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)) {
            countedpage = (int) countedstartpagecell.getNumericCellValue();
        } else {
            System.err.println(
                    "ERROR: Can't find cell for counted startpage of cell value is not numeric - line " + x);
            continue;
        }
        // Get uncounted start page.
        if ((uncountedstartpagecell != null)
                && (uncountedstartpagecell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)) {
            uncountedpage = (int) uncountedstartpagecell.getNumericCellValue();
        } else {
            if ((uncountedstartpagecell != null)
                    && (uncountedstartpagecell.getCellType() != HSSFCell.CELL_TYPE_BLANK)) {
                System.err
                        .println("WARNING (Gliederung): invalid value in uncountedstartpage cell - line " + x);
            }
        }

        // Get overlapping.
        if ((ueberlappungcell != null) && (ueberlappungcell.getCellType() != HSSFCell.CELL_TYPE_BLANK)) {
            if (ueberlappungcell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                overlapping = ueberlappungcell.getStringCellValue();
                // Needed to read OpenOffice excel files...
                overlapping = TrimString(overlapping);
            }
            if (ueberlappungcell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                overlapping = Double.toString(ueberlappungcell.getNumericCellValue());
                // Needed to read OpenOffice excel files...
                overlapping = TrimString(overlapping);
            }
        }

        // Create DocStruct instance.
        //
        DocStruct newStruct = null;
        if (type != null) {
            DocStructType structType = getDSTypeByName(type, "ExcelGliederung");
            if (structType == null) {
                System.err.println(
                        "ERROR: Excelfile.ReadGliederung: Can't find DocStruct for type=" + type + "<");
                return false;
            }
            newStruct = this.mydoc.createDocStruct(structType);
        }

        // Add metadata as title, author, identifier etc... metadata is
        // configurable using the language excel:Gliederung.
        for (int u = 0; u < metadataColumn.size(); u++) {
            // Gt cell.
            HSSFCell metadatacell = metadataCells.get(u);
            String mdvalue = null;
            if ((metadatacell != null) && (metadatacell.getCellType() != HSSFCell.CELL_TYPE_BLANK)) {
                if (metadatacell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                    mdvalue = metadatacell.getStringCellValue();
                }
                if (metadatacell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                    mdvalue = Double.toString(metadatacell.getNumericCellValue());
                    // Needed to read OpenOffice excel files...
                    mdvalue = TrimString(mdvalue);
                }
            } else {
                // It's a blank cell - so continue with next metadata
                // column.
                continue;
            }
            if (mdvalue != null) {
                String metadatatypeString = metadataType.get(u);
                MetadataType mdtype = getMDTypeByName(metadatatypeString, "ExcelGliederung");
                if (metadatatypeString.equals("Autoren")) {
                    List<Metadata> allvalues = ReadAuthors(mdvalue);
                    for (int j = 0; j < allvalues.size(); j++) {
                        // Create new Metadata instance.
                        Metadata partOfMdvalue = allvalues.get(j);
                        // Excel spreadsheet cell as native object.
                        partOfMdvalue.setNativeObject(metadatacell);
                        partOfMdvalue.wasUpdated(false);
                        try {
                            // Addit to new DocStruct instance.
                            if (!newStruct.addMetadata(partOfMdvalue)) {
                                System.err.println("ERROR: Can't add metadata to new document structure - line "
                                        + x + ".");
                            }
                        } catch (MetadataTypeNotAllowedException mtnaae) {
                            System.err.println("ERROR: ReadGliederung: can't add metadata - line" + x);
                            System.err.println("       " + partOfMdvalue.getType().getName()
                                    + " can't be added to " + newStruct.getType().getName());
                        }
                        // Add it to new DocStruct.
                        partOfMdvalue.setDocStruct(newStruct);
                    }
                } else {
                    // Ccreate new Metadatainstance.
                    Metadata md = new Metadata(mdtype);
                    md.setValue(mdvalue);
                    // Set the Excel-spreadsheet cell as native object.
                    md.setNativeObject(metadatacell);
                    md.wasUpdated(false);

                    try {
                        // Add it to new DocStruct instance.
                        if (!newStruct.addMetadata(md)) {
                            System.err.println(
                                    "ERROR: Can't add metadata to new document structure - line " + x + ".");
                        }
                    } catch (MetadataTypeNotAllowedException mtnae) {
                        System.err.println("ERROR: ReadPaginationSequences: Can't add metadata - line " + x);
                        System.err.println("       " + md.getType().getName() + " can't be added to "
                                + newStruct.getType().getName());
                    }
                    md.setDocStruct(newStruct);
                }
            }
        }

        // Add physical page numbers.
        PaginationSequence currentSeq = null;
        int physnumber = 0;
        for (int u = 0; u < this.allPaginations.size(); u++) {
            if (u == (sequence - 1)) {
                currentSeq = this.allPaginations.get(u);
                if (uncountedpage > 0) {
                    physnumber = CalculatePhysicalNumber(currentSeq, uncountedpage);
                }
                if ((countedpage > 0) && (uncountedpage <= 0)) {
                    physnumber = CalculatePhysicalNumber(currentSeq, countedpage);
                }
                MetadataType pagenumberPhys = this.myPreferences.getMetadataTypeByName("_pagephysstart");
                Metadata mdnew = new Metadata(pagenumberPhys);
                mdnew.setValue(Integer.toString(physnumber));
                try {
                    // Add physical page number.
                    newStruct.addMetadata(mdnew);
                } catch (MetadataTypeNotAllowedException mtnaae) {
                    System.err.println("ERROR: ReadGliederung: can't add metadata ");
                    System.err.println("       " + mdnew.getType().getName() + " can't be added to "
                            + newStruct.getType().getName());
                    return false;
                }
                // Add overlapping information.
                MetadataType overlappingType = this.myPreferences.getMetadataTypeByName("_overlapping");
                mdnew = new Metadata(overlappingType);
                mdnew.setValue(overlapping);
                try {
                    // Add physical page number.
                    newStruct.addMetadata(mdnew);
                } catch (MetadataTypeNotAllowedException mtnaae) {
                    System.err.println("ERROR: ReadGliederung: can't add metadata ");
                    System.err.println("       " + mdnew.getType().getName() + " can't be added to "
                            + newStruct.getType().getName());
                    return false;
                }
                continue;
            }
        }

        // Get pagaination sequence, calculate number, add DocStruct to
        // tree.
        if (hierarchy < 0) {
            System.out.println("ERROR: Invalid hierarchy level (1)");
            return false;
        }
        if ((oldhierarchy < hierarchy) && ((hierarchy - oldhierarchy) > 1)) {
            // There is a jump in hiearchy level from 1 to 3 or so; this is
            // an error.
            System.out.println("ERROR: Invalid hierarchy level (2)");
            return false;
        }

        if (hierarchy == 1) {
            // Add it to the topmost element.
            DocStruct parent = this.mydoc.getLogicalDocStruct();
            if (parent == null) {
                System.out.println("ERROR: Can't find any parent element on topmost level");
                return false;
            }

            DocStructType parentType = parent.getType();
            if (parentType.getAnchorClass() != null) {
                // It's an anchor (e.g. a periodical...) so we cannot add
                // any children to this but we must get the next structure
                // entity (child) - e.g. the volume.
                List<DocStruct> children = parent.getAllChildren();
                if (children == null) {
                    System.out.println("ERROR: Parent is anchor but has no child");
                    return false;
                }
                // Get first child as new parent.
                parent = children.get(0);
            }

            // Aadd this new DocStruct object to the parent.
            try {
                if (!parent.addChild(newStruct)) {
                    System.err.println("ERROR: Can't read Gliederung; can't add child");
                    System.err.println("       " + newStruct.getType().getName() + " can't be added to "
                            + parent.getType().getName());
                    return false;
                }
            } catch (TypeNotAllowedAsChildException tnaace) {
                // Type is not allowed to be added; wrong configuration.
                System.err.println("ERROR: Can't read Gliederung; can't add child");
                System.err.println("       " + newStruct.getType().getName() + " can't be added to "
                        + parent.getType().getName());
                return false;
            }

            // Store this newStruct as the current DocStruct instance for
            // hierarchy level 1.
            alllevels[1] = newStruct;
        } else {
            DocStruct oldStruct = alllevels[hierarchy - 1];
            if (oldStruct == null) {
                System.err.println("ERROR: ReadGliederung");
                System.err.println("       Can't find any structure entity on level" + (hierarchy - 1));
                return false;
            }
            try {
                oldStruct.addChild(newStruct);
            } catch (TypeNotAllowedAsChildException tnaace) {
                System.err.println("ERROR: ReadGliederung: Can't add child");
                System.err.println("       " + newStruct.getType().getName() + " can't be added to "
                        + oldStruct.getType().getName());
                return false;
            }
            alllevels[hierarchy] = newStruct;
        }
        // Store the old value, so we can compare it with the new one in the
        // next row.
        oldhierarchy = hierarchy;

        // Add new DocStruct instance to LinkedList and add all other
        // information.
        this.allDocStruct.add(newStruct);
        this.allStructRow.add(String.valueOf(x));
        this.allStructSheets.add("Gliederung");

    }

    // Calculate physical endpages and add references for pages (DocStruct
    // instances).
    DocStruct parent = this.mydoc.getLogicalDocStruct();
    if (parent == null) {
        System.out.println("ERROR: Can't find any parent element on topmost level");
        return false;
    }

    DocStructType parentType = parent.getType();
    if (parentType.getAnchorClass() != null) {
        // It's an anchor (e.g. a periodical...) so we cannot add any
        // children to this but we must get the next structure entity
        // (child) - e.g. the volume.
        List<DocStruct> children = parent.getAllChildren();
        if (children == null) {
            System.out.println("ERROR: Parent is anchor but has no child");
            return false;
        }
        // Get first child as new parent.
        parent = children.get(0);
    }

    // Calculates the endpages of the children - NOT of the parent we know
    // the parent pages already from the Pagination Sequences.
    CalculateEndPage(parent);

    return true;
}

From source file:uk.ac.ebi.mnb.parser.ExcelXLSHelper.java

License:Open Source License

public String[][] getSheetData(HSSFSheet sheet) {

    int maxRow = sheet.getLastRowNum() + 1;
    int colNumber = INTIAL_COLUMN_NUMBER;

    String[][] data = new String[maxRow][colNumber];

    List<String> block_xy1 = new ArrayList<String>();
    List<String> block_xy2 = new ArrayList<String>();

    int prevLastFilledColumn = -1;
    int emptyRows = 0;
    LOGGER.info("getting sheet data");

    for (int i = 0; i < maxRow; i++) {

        HSSFRow row = sheet.getRow(i);
        int lastFilledColumn = -1;

        if (row != null) {

            // convert to 1D array
            if (row.getLastCellNum() > 0) {
                String[] rowData = new String[row.getLastCellNum()];
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    // check for empty rows
                    HSSFCell cell = row.getCell(j);
                    rowData[j] = getCellString(cell).trim();
                    if (rowData[j].isEmpty() == Boolean.FALSE) {
                        lastFilledColumn = j;
                    }/*from  w  ww.java  2s  .  c o m*/
                }

                // resize if needed
                if (lastFilledColumn >= colNumber) {
                    LOGGER.info("Sending resize row:" + i + " col:" + lastFilledColumn + " max:" + colNumber);
                    data = resize(data, lastFilledColumn);
                    colNumber = data[0].length;
                }

                // and copy the new data
                if (lastFilledColumn >= 0) {
                    System.arraycopy(rowData, 0, data[i], 0, lastFilledColumn + 1);
                }

            }

            for (int j = row.getLastCellNum(); j < colNumber; j++) {
                if (j != -1) {
                    data[i][j] = "";
                }
            }

            if (lastFilledColumn == -1) {
                if (prevLastFilledColumn != -1) {
                    // empty row
                    block_xy2.add((i - 1) + "," + prevLastFilledColumn);
                }
            } else {
                if (block_xy2.size() == block_xy1.size()) {
                    block_xy1.add(i + "," + 0);
                }
            }

            prevLastFilledColumn = lastFilledColumn;

        }

        emptyRows = lastFilledColumn <= 0 ? emptyRows + 1 : 0;

        if (emptyRows > 5) {
            return Arrays.copyOf(data, i - 5);
        }
    }

    for (int i = 0; i < block_xy2.size(); i++) {
        //System.out.println(block_xy1.get(i) + " to " + block_xy2.get(i));
    }

    return data;
}

From source file:uk.ac.ebi.mnb.parser.ExcelXLSHelper.java

License:Open Source License

@Override
public String[][] getTableHead(Integer sheetIndex, Integer rowCount) {

    HSSFSheet sheet = workbook.getSheetAt(sheetIndex);
    Integer colCount = 0;//from ww  w  . ja  va 2s.c o m

    List[] data = new List[rowCount];

    for (Integer i = 0; i < rowCount; i++) {
        HSSFRow row = sheet.getRow(i);
        data[i] = new ArrayList();
        for (Integer j = 0; j < row.getLastCellNum(); j++) {
            HSSFCell cell = row.getCell(j);
            if (cell != null && !cell.toString().trim().isEmpty()) {
                data[i].add(cell.toString());
                colCount = j > colCount ? j : colCount;
            } else {
                data[i].add("");
            }
        }
    }

    String[][] fixedData = new String[rowCount][colCount];
    for (int i = 0; i < rowCount; i++) {
        // make sure we have the correct length
        while (data[i].size() < colCount) {
            data[i].add("");
        }
        fixedData[i] = (String[]) data[i].subList(0, colCount).toArray(new String[0]);
    }

    return fixedData;
}

From source file:uk.ac.manchester.cs.owl.semspreadsheets.model.hssf.impl.SheetHSSFImpl.java

License:BSD License

public List<Cell> getCellsWithContent() {
    List<Cell> cells = new ArrayList<Cell>();
    int firstRow = sheet.getFirstRowNum();
    int lastRow = sheet.getLastRowNum();
    for (int rowIndex = firstRow; rowIndex <= lastRow; rowIndex++) {
        HSSFRow row = sheet.getRow(rowIndex);
        if (row != null) {
            int firstCell = row.getFirstCellNum();
            int lastCell = row.getLastCellNum();
            for (int cellIndex = firstCell; cellIndex <= lastCell; cellIndex++) {
                HSSFCell cell = row.getCell(cellIndex);
                boolean skip = cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK
                        || (cell.getCellType() == HSSFCell.CELL_TYPE_STRING
                                && cell.getStringCellValue().isEmpty());
                if (!skip) {
                    cells.add(new CellHSSFImpl(hssfWorkbook, cell));
                }/*from w ww. ja  v a  2 s. co  m*/
            }
        }
    }
    return cells;
}

From source file:utilesBD.servidoresDatos.JServerServidorDatosExcel.java

/**
 * Metodo que se encarga de leer los datos de un archivo en formato excel
 * @return Fila que contiene la informacion del archivo
 *///from   ww w .  j a  v  a  2  s  .c o  m
public JFilaDatosDefecto leeLineaExcel(HSSFRow hssfRow) {
    JFilaDatosDefecto loLinea = new JFilaDatosDefecto();
    //Me barro todos los elementos de una fila
    for (int i = hssfRow.getFirstCellNum(); i < hssfRow.getLastCellNum(); i++) {
        HSSFCell hssfCell = hssfRow.getCell(i);
        if (hssfCell != null) {
            switch (hssfCell.getCellType()) {
            case HSSFCell.CELL_TYPE_BOOLEAN:
                loLinea.addCampo(String.valueOf(hssfCell.getBooleanCellValue()));
                break;
            case HSSFCell.CELL_TYPE_FORMULA:
                try {
                    loLinea.addCampo(hssfCell.getStringCellValue());
                } catch (Exception e) {
                    try {
                        loLinea.addCampo(String.valueOf(hssfCell.getNumericCellValue()));
                    } catch (Exception e1) {
                        try {
                            loLinea.addCampo(new JDateEdu(hssfCell.getDateCellValue()).toString()
                                    .replace("31/12/1899 ", ""));
                        } catch (Exception e2) {
                            try {
                                loLinea.addCampo(String.valueOf(hssfCell.getBooleanCellValue()));
                            } catch (Exception e3) {
                                loLinea.addCampo("");
                            }
                        }
                    }
                }
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {
                    loLinea.addCampo(
                            new JDateEdu(hssfCell.getDateCellValue()).toString().replace("31/12/1899 ", ""));
                } else {
                    double ldValor = hssfCell.getNumericCellValue();
                    loLinea.addCampo(
                            JFormat.msFormatearDouble(ldValor, "############.#########").replace(',', '.'));
                }
                break;
            case HSSFCell.CELL_TYPE_STRING:
                loLinea.addCampo(hssfCell.toString());
                break;
            default:
                loLinea.addCampo("");
            }
        } else {
            loLinea.addCampo("");
        }
    }
    return loLinea;
}

From source file:vone.HSSFReadWrite.java

License:Apache License

/**
 * Method main//from w  w  w .j a v a  2s .c o  m
 *
 * Given 1 argument takes that as the filename, inputs it and dumps the cell
 * values/types out to sys.out.<br/>
 *
 * given 2 arguments where the second argument is the word "write" and the
 * first is the filename - writes out a sample (test) spreadsheet see
 * {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br/>
 *
 * given 2 arguments where the first is an input filename and the second an
 * output filename (not write), attempts to fully read in the spreadsheet
 * and fully write it out.<br/>
 *
 * given 3 arguments where the first is an input filename and the second an
 * output filename (not write) and the third is "modify1", attempts to read
 * in the spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col
 * 3 to "MODIFIED CELL" then writes it out. Hence this is "modify test 1".
 * If you take the output from the write test, you'll have a valid scenario.
 */
@SuppressWarnings("deprecation")
public static void main(String[] args) {
    if (args.length < 1) {
        System.err.println("At least one argument expected");
        return;
    }

    String fileName = args[0];
    try {
        if (args.length < 2) {

            HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);

            System.out.println("Data dump:\n");

            for (int k = 0; k < wb.getNumberOfSheets(); k++) {
                HSSFSheet sheet = wb.getSheetAt(k);
                int rows = sheet.getPhysicalNumberOfRows();
                System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows + " row(s).");
                for (int r = 0; r < rows; r++) {
                    HSSFRow row = sheet.getRow(r);
                    if (row == null) {
                        continue;
                    }

                    // int cells = row.getPhysicalNumberOfCells();
                    int cells = row.getLastCellNum();
                    System.out.println("\nROW " + row.getRowNum() + " has " + cells + " cell(s).");
                    for (int c = 0; c < cells; c++) {
                        HSSFCell cell = row.getCell(c);
                        String value = null;
                        if (cell == null) {
                            System.out.println("CELL col=" + c + " VALUE= Empty");
                            continue;
                        }

                        switch (cell.getCellTypeEnum()) {

                        case FORMULA:
                            value = "FORMULA value=" + cell.getCellFormula();
                            break;

                        case NUMERIC:
                            value = "NUMERIC value=" + cell.getNumericCellValue();
                            break;

                        case STRING:
                            value = "STRING value=" + cell.getStringCellValue();
                            break;

                        default:
                        }
                        System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value);
                    }
                }
            }
            wb.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ypcnv.views.impl.FileXLS.java

License:Open Source License

/**
 * Check whether or not the given row's cells content have all the expected
 * names of data fields. <br>//from w  w w.  jav  a  2 s .c om
 * <br>
 * XLS workbooks generated by MS-Outlook while contacts export all ways have
 * fixed number of data fields.<br>
 * <br>
 * Obtain meta data concerning distribution of header names.
 * 
 * @param headerRow
 *            - row to be processed.
 * @throws FileViewException
 *             in case check is not passed.
 */
private void validateHeaderNames(HSSFRow headerRow) throws FileViewException {
    startColumnIdx = (int) headerRow.getFirstCellNum();
    /*
     * It's Apache POI feature - getLastCellNum gets the index of the last
     * cell contained in this row PLUS ONE.
     */
    stopColumnIdx = -1 + (int) headerRow.getLastCellNum();

    /* Whether quantity of fields is valid. */
    int foundQuantityOfDataFields = 1 + stopColumnIdx - startColumnIdx;
    if (foundQuantityOfDataFields != Contact2k3.getDataFieldsQuantity()
            || foundQuantityOfDataFields != FileXLSMeta.NUMBER_OF_DATA_FIELDS) {
        String message = String.format(FileXLSMeta.ERR_MESSAGE_WRONG_FIELD_QUANTITY,
                ((File) address).getAbsoluteFile(), FileXLSMeta.NUMBER_OF_DATA_FIELDS);
        LOG.error(message);
        throw new FileViewException(null, "Contact2k3Xls.checkHeaderNames()", message);
    }
    /* Whether data fields names are valid. */
    for (int idx = startColumnIdx; idx <= stopColumnIdx; idx++) {
        HSSFCell currentCell = headerRow.getCell(idx);
        String xlsFieldName = currentCell.getStringCellValue();
        String modelDataFieldName = null;

        modelDataFieldName = FileXLSNames.CONTAINER_FIELD_NAMING_MAP.get(xlsFieldName);

        if (modelDataFieldName == null) {
            ArrayList<String> foreignNamesSearchResultList = new ArrayList<String>();

            /*
             * Add one by one mappings of other localizations, or add
             * explicit locality flag support.
             */
            foreignNamesSearchResultList
                    .add(FileXLSNames.CONTAINER_FIELD_NAMING_MAP_RUS2ENGL.get(xlsFieldName));

            Iterator<String> namesWereFoundListIterator = foreignNamesSearchResultList.iterator();
            while (namesWereFoundListIterator.hasNext()) {
                String foundForeignName = namesWereFoundListIterator.next();
                if (foundForeignName != null) {
                    modelDataFieldName = FileXLSNames.CONTAINER_FIELD_NAMING_MAP.get(foundForeignName);
                }
            }
        }

        if (modelDataFieldName != null) {
            dataColumnsSequenceMap.put(modelDataFieldName, idx);
        } else {
            String message = String.format(FileXLSMeta.ERR_MESSAGE_WRONG_FIELD_NAME,
                    ((File) address).getAbsoluteFile(), xlsFieldName);
            throw new FileViewException(null, "Contact2k3Xls.checkHeaderNames()", message);
        }
    }
}