Search in sources :

Example 26 with HSSFRichTextString

use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.

the class HSSFReadWrite method testCreateSampleSheet.

/**
	 * given a filename this outputs a sample sheet with just a set of
	 * rows/cells.
	 */
private static void testCreateSampleSheet(String outputFilename) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    try {
        HSSFSheet s = wb.createSheet();
        HSSFCellStyle cs = wb.createCellStyle();
        HSSFCellStyle cs2 = wb.createCellStyle();
        HSSFCellStyle cs3 = wb.createCellStyle();
        HSSFFont f = wb.createFont();
        HSSFFont f2 = wb.createFont();
        f.setFontHeightInPoints((short) 12);
        f.setColor((short) 0xA);
        f.setBold(true);
        f2.setFontHeightInPoints((short) 10);
        f2.setColor((short) 0xf);
        f2.setBold(true);
        cs.setFont(f);
        cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
        cs2.setBorderBottom(BorderStyle.THIN);
        cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cs2.setFillForegroundColor((short) 0xA);
        cs2.setFont(f2);
        wb.setSheetName(0, "HSSF Test");
        int rownum;
        for (rownum = 0; rownum < 300; rownum++) {
            HSSFRow r = s.createRow(rownum);
            if ((rownum % 2) == 0) {
                r.setHeight((short) 0x249);
            }
            for (int cellnum = 0; cellnum < 50; cellnum += 2) {
                HSSFCell c = r.createCell(cellnum);
                c.setCellValue(rownum * 10000 + cellnum + (((double) rownum / 1000) + ((double) cellnum / 10000)));
                if ((rownum % 2) == 0) {
                    c.setCellStyle(cs);
                }
                c = r.createCell(cellnum + 1);
                c.setCellValue(new HSSFRichTextString("TEST"));
                // 50 characters divided by 1/20th of a point
                s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05));
                if ((rownum % 2) == 0) {
                    c.setCellStyle(cs2);
                }
            }
        }
        // draw a thick black border on the row at the bottom using BLANKS
        rownum++;
        rownum++;
        HSSFRow r = s.createRow(rownum);
        cs3.setBorderBottom(BorderStyle.THICK);
        for (int cellnum = 0; cellnum < 50; cellnum++) {
            HSSFCell c = r.createCell(cellnum);
            c.setCellStyle(cs3);
        }
        s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));
        s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110));
        // end draw thick black border
        // create a sheet, set its title then delete it
        wb.createSheet();
        wb.setSheetName(1, "DeletedSheet");
        wb.removeSheetAt(1);
        // end deleted sheet
        FileOutputStream out = new FileOutputStream(outputFilename);
        try {
            wb.write(out);
        } finally {
            out.close();
        }
    } finally {
        wb.close();
    }
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) FileOutputStream(java.io.FileOutputStream) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 27 with HSSFRichTextString

use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.

the class InCellLists method listInCell.

/**
     * Inserts a list of plain items - that is items that are neither
     * numbered or bulleted - into a single cell.
     *
     * @param workbook A reference to the HSSFWorkbook that 'contains' the
     *                 cell.
     * @param listItems An ArrayList whose elements encapsulate the text for
     *                  the list's items.
     * @param cell An instance of the HSSFCell class that encapsulates a
     *             reference to the spreadsheet cell into which the list
     *             will be written.
     */
public void listInCell(HSSFWorkbook workbook, ArrayList<String> listItems, HSSFCell cell) {
    StringBuilder buffer = new StringBuilder();
    HSSFCellStyle wrapStyle = workbook.createCellStyle();
    wrapStyle.setWrapText(true);
    for (String listItem : listItems) {
        buffer.append(listItem);
        buffer.append("\n");
    }
    // The StringBuffer's contents are the source for the contents
    // of the cell.
    cell.setCellValue(new HSSFRichTextString(buffer.toString().trim()));
    cell.setCellStyle(wrapStyle);
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Example 28 with HSSFRichTextString

use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.

the class AbstractExcelConverter method isTextEmpty.

protected boolean isTextEmpty(HSSFCell cell) {
    final String value;
    switch(cell.getCellTypeEnum()) {
        case STRING:
            // XXX: enrich
            value = cell.getRichStringCellValue().getString();
            break;
        case FORMULA:
            switch(cell.getCachedFormulaResultTypeEnum()) {
                case STRING:
                    HSSFRichTextString str = cell.getRichStringCellValue();
                    if (str == null || str.length() <= 0)
                        return false;
                    value = str.toString();
                    break;
                case NUMERIC:
                    HSSFCellStyle style = cell.getCellStyle();
                    double nval = cell.getNumericCellValue();
                    short df = style.getDataFormat();
                    String dfs = style.getDataFormatString();
                    value = _formatter.formatRawCellContents(nval, df, dfs);
                    break;
                case BOOLEAN:
                    value = String.valueOf(cell.getBooleanCellValue());
                    break;
                case ERROR:
                    value = ErrorEval.getText(cell.getErrorCellValue());
                    break;
                default:
                    value = ExcelToHtmlUtils.EMPTY;
                    break;
            }
            break;
        case BLANK:
            value = ExcelToHtmlUtils.EMPTY;
            break;
        case NUMERIC:
            value = _formatter.formatCellValue(cell);
            break;
        case BOOLEAN:
            value = String.valueOf(cell.getBooleanCellValue());
            break;
        case ERROR:
            value = ErrorEval.getText(cell.getErrorCellValue());
            break;
        default:
            return true;
    }
    return ExcelToHtmlUtils.isEmpty(value);
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Example 29 with HSSFRichTextString

use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.

the class TestXSSFComment method setString.

@Test
public void setString() {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sh = wb.createSheet();
    XSSFComment comment = sh.createDrawingPatriarch().createCellComment(new XSSFClientAnchor());
    //passing HSSFRichTextString is incorrect
    try {
        comment.setString(new HSSFRichTextString(TEST_RICHTEXTSTRING));
        fail("expected exception");
    } catch (IllegalArgumentException e) {
        assertEquals("Only XSSFRichTextString argument is supported", e.getMessage());
    }
    //simple string argument
    comment.setString(TEST_RICHTEXTSTRING);
    assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
    //if the text is already set, it should be overridden, not added twice!
    comment.setString(TEST_RICHTEXTSTRING);
    CTComment ctComment = comment.getCTComment();
    XmlObject[] obj = ctComment.selectPath("declare namespace w='" + NS_SPREADSHEETML + "' .//w:text");
    assertEquals(1, obj.length);
    assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
    //sequential call of comment.getString() should return the same XSSFRichTextString object
    assertSame(comment.getString(), comment.getString());
    XSSFRichTextString richText = new XSSFRichTextString(TEST_RICHTEXTSTRING);
    XSSFFont font1 = wb.createFont();
    font1.setFontName("Tahoma");
    font1.setFontHeight(8.5);
    font1.setItalic(true);
    font1.setColor(IndexedColors.BLUE_GREY.getIndex());
    richText.applyFont(0, 5, font1);
    //check the low-level stuff
    comment.setString(richText);
    obj = ctComment.selectPath("declare namespace w='" + NS_SPREADSHEETML + "' .//w:text");
    assertEquals(1, obj.length);
    assertSame(comment.getString(), richText);
    //check that the rich text is set in the comment
    CTRPrElt rPr = richText.getCTRst().getRArray(0).getRPr();
    assertEquals(true, rPr.getIArray(0).getVal());
    assertEquals(8.5, rPr.getSzArray(0).getVal(), 0);
    assertEquals(IndexedColors.BLUE_GREY.getIndex(), rPr.getColorArray(0).getIndexed());
    assertEquals("Tahoma", rPr.getRFontArray(0).getVal());
    assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
Also used : HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt) XmlObject(org.apache.xmlbeans.XmlObject) Test(org.junit.Test)

Example 30 with HSSFRichTextString

use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.

the class ExcelToHtmlConverter method processCell.

protected boolean processCell(HSSFCell cell, Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt) {
    final HSSFCellStyle cellStyle = cell.getCellStyle();
    String value;
    switch(cell.getCellTypeEnum()) {
        case STRING:
            // XXX: enrich
            value = cell.getRichStringCellValue().getString();
            break;
        case FORMULA:
            switch(cell.getCachedFormulaResultTypeEnum()) {
                case STRING:
                    HSSFRichTextString str = cell.getRichStringCellValue();
                    if (str != null && str.length() > 0) {
                        value = (str.toString());
                    } else {
                        value = ExcelToHtmlUtils.EMPTY;
                    }
                    break;
                case NUMERIC:
                    double nValue = cell.getNumericCellValue();
                    short df = cellStyle.getDataFormat();
                    String dfs = cellStyle.getDataFormatString();
                    value = _formatter.formatRawCellContents(nValue, df, dfs);
                    break;
                case BOOLEAN:
                    value = String.valueOf(cell.getBooleanCellValue());
                    break;
                case ERROR:
                    value = ErrorEval.getText(cell.getErrorCellValue());
                    break;
                default:
                    logger.log(POILogger.WARN, "Unexpected cell cachedFormulaResultType (" + cell.getCachedFormulaResultTypeEnum() + ")");
                    value = ExcelToHtmlUtils.EMPTY;
                    break;
            }
            break;
        case BLANK:
            value = ExcelToHtmlUtils.EMPTY;
            break;
        case NUMERIC:
            value = _formatter.formatCellValue(cell);
            break;
        case BOOLEAN:
            value = String.valueOf(cell.getBooleanCellValue());
            break;
        case ERROR:
            value = ErrorEval.getText(cell.getErrorCellValue());
            break;
        default:
            logger.log(POILogger.WARN, "Unexpected cell type (" + cell.getCellTypeEnum() + ")");
            return true;
    }
    final boolean noText = ExcelToHtmlUtils.isEmpty(value);
    final boolean wrapInDivs = !noText && isUseDivsToSpan() && !cellStyle.getWrapText();
    if (cellStyle.getIndex() != 0) {
        @SuppressWarnings("resource") HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook();
        String mainCssClass = getStyleClassName(workbook, cellStyle);
        if (wrapInDivs) {
            tableCellElement.setAttribute("class", mainCssClass + " " + cssClassContainerCell);
        } else {
            tableCellElement.setAttribute("class", mainCssClass);
        }
        if (noText) {
            /*
                 * if cell style is defined (like borders, etc.) but cell text
                 * is empty, add "&nbsp;" to output, so browser won't collapse
                 * and ignore cell
                 */
            value = " ";
        }
    }
    if (isOutputLeadingSpacesAsNonBreaking() && value.startsWith(" ")) {
        StringBuilder builder = new StringBuilder();
        for (int c = 0; c < value.length(); c++) {
            if (value.charAt(c) != ' ')
                break;
            builder.append(' ');
        }
        if (value.length() != builder.length())
            builder.append(value.substring(builder.length()));
        value = builder.toString();
    }
    Text text = htmlDocumentFacade.createText(value);
    if (wrapInDivs) {
        Element outerDiv = htmlDocumentFacade.createBlock();
        outerDiv.setAttribute("class", this.cssClassContainerDiv);
        Element innerDiv = htmlDocumentFacade.createBlock();
        StringBuilder innerDivStyle = new StringBuilder();
        innerDivStyle.append("position:absolute;min-width:");
        innerDivStyle.append(normalWidthPx);
        innerDivStyle.append("px;");
        if (maxSpannedWidthPx != Integer.MAX_VALUE) {
            innerDivStyle.append("max-width:");
            innerDivStyle.append(maxSpannedWidthPx);
            innerDivStyle.append("px;");
        }
        innerDivStyle.append("overflow:hidden;max-height:");
        innerDivStyle.append(normalHeightPt);
        innerDivStyle.append("pt;white-space:nowrap;");
        ExcelToHtmlUtils.appendAlign(innerDivStyle, cellStyle.getAlignment());
        htmlDocumentFacade.addStyleClass(outerDiv, cssClassPrefixDiv, innerDivStyle.toString());
        innerDiv.appendChild(text);
        outerDiv.appendChild(innerDiv);
        tableCellElement.appendChild(outerDiv);
    } else {
        tableCellElement.appendChild(text);
    }
    return ExcelToHtmlUtils.isEmpty(value) && (cellStyle.getIndex() == 0);
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Aggregations

HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)38 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)17 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)16 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)9 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)8 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)7 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)5 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)5 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)5 Test (org.junit.Test)5 HSSFSimpleShape (org.apache.poi.hssf.usermodel.HSSFSimpleShape)3 FileOutputStream (java.io.FileOutputStream)2 BigDecimal (java.math.BigDecimal)2 HSSFComment (org.apache.poi.hssf.usermodel.HSSFComment)2 Region (org.apache.poi.hssf.util.Region)2 Element (org.w3c.dom.Element)2 Text (org.w3c.dom.Text)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1