public String getText()

in poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java [273:393]


    public String getText() {
        StringBuilder text = new StringBuilder();

        // We don't care about the difference between
        //  null (missing) and blank cells
        _wb.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);

        // Process each sheet in turn
        for(int i=0;i<_wb.getNumberOfSheets();i++) {
            HSSFSheet sheet = _wb.getSheetAt(i);
            if(sheet == null) { continue; }

            if(_includeSheetNames) {
                String name = _wb.getSheetName(i);
                if(name != null) {
                    text.append(name);
                    text.append("\n");
                }
            }

            // Header text, if there is any
            if(_includeHeadersFooters) {
                text.append(_extractHeaderFooter(sheet.getHeader()));
            }

            int firstRow = sheet.getFirstRowNum();
            int lastRow = sheet.getLastRowNum();
            for(int j=firstRow;j<=lastRow;j++) {
                HSSFRow row = sheet.getRow(j);
                if(row == null) { continue; }

                // Check each cell in turn
                int firstCell = row.getFirstCellNum();
                int lastCell = row.getLastCellNum();
                if(_includeBlankCells) {
                    firstCell = 0;
                }

                for(int k=firstCell;k<lastCell;k++) {
                    HSSFCell cell = row.getCell(k);
                    boolean outputContents = true;

                    if(cell == null) {
                        // Only output if requested
                        outputContents = _includeBlankCells;
                    } else {
                        switch(cell.getCellType()) {
                            case STRING:
                                text.append(cell.getRichStringCellValue().getString());
                                break;
                            case NUMERIC:
                                text.append(_formatter.formatCellValue(cell));
                                break;
                            case BOOLEAN:
                                text.append(cell.getBooleanCellValue());
                                break;
                            case ERROR:
                                text.append(ErrorEval.getText(cell.getErrorCellValue()));
                                break;
                            case FORMULA:
                                if(!_shouldEvaluateFormulas) {
                                    text.append(cell.getCellFormula());
                                } else {
                                    switch(cell.getCachedFormulaResultType()) {
                                        case STRING:
                                            HSSFRichTextString str = cell.getRichStringCellValue();
                                            if(str != null && str.length() > 0) {
                                                text.append(str);
                                            }
                                            break;
                                        case NUMERIC:
                                            HSSFCellStyle style = cell.getCellStyle();
                                            double nVal = cell.getNumericCellValue();
                                            short df = style.getDataFormat();
                                            String dfs = style.getDataFormatString();
                                            text.append(_formatter.formatRawCellContents(nVal, df, dfs));
                                            break;
                                        case BOOLEAN:
                                            text.append(cell.getBooleanCellValue());
                                            break;
                                        case ERROR:
                                            text.append(ErrorEval.getText(cell.getErrorCellValue()));
                                            break;
                                        default:
                                            throw new IllegalStateException("Unexpected cell cached formula result type: " + cell.getCachedFormulaResultType());

                                    }
                                }
                                break;
                            default:
                                throw new IllegalStateException("Unexpected cell type (" + cell.getCellType() + ")");
                        }

                        // Output the comment, if requested and exists
                        HSSFComment comment = cell.getCellComment();
                        if(_includeCellComments && comment != null) {
                            // Replace any newlines with spaces, otherwise it
                            //  breaks the output
                            String commentText = comment.getString().getString().replace('\n', ' ');
                            text.append(" Comment by ").append(comment.getAuthor()).append(": ").append(commentText);
                        }
                    }

                    // Output a tab if we're not on the last cell
                    if(outputContents && k < (lastCell-1)) {
                        text.append("\t");
                    }
                }

                // Finish off the row
                text.append("\n");
            }

            // Finally Footer text, if there is any
            if(_includeHeadersFooters) {
                text.append(_extractHeaderFooter(sheet.getFooter()));
            }
        }

        return text.toString();
    }