Example usage for org.apache.poi.hssf.usermodel HSSFRichTextString applyFont

List of usage examples for org.apache.poi.hssf.usermodel HSSFRichTextString applyFont

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFRichTextString applyFont.

Prototype

public void applyFont(int startIndex, int endIndex, Font font) 

Source Link

Document

Applies a font to the specified characters of a string.

Usage

From source file:com.siteview.ecc.report.xls.JRXlsExporter.java

License:Open Source License

protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont,
        Locale locale) {/* www .  j  a  v a2 s .co  m*/
    String text = styledText.getText();
    HSSFRichTextString richTextStr = new HSSFRichTextString(text);
    int runLimit = 0;
    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        Map attributes = iterator.getAttributes();
        JRFont runFont = attributes.isEmpty() ? defaultFont : new JRBaseFont(attributes);
        short runForecolor = attributes.get(TextAttribute.FOREGROUND) != null
                ? getNearestColor((Color) attributes.get(TextAttribute.FOREGROUND)).getIndex()
                : forecolor;
        HSSFFont font = getLoadedFont(runFont, runForecolor, attributes, locale);
        richTextStr.applyFont(iterator.getIndex(), runLimit, font);
        iterator.setIndex(runLimit);
    }
    return richTextStr;
}

From source file:devcup.search4u.xlsview.ConvertResultsToXLS.java

private HSSFRichTextString labeledStringToRichTextString(String labeledString, HSSFFont partFont) {
    //create List, delete labels
    List<ObjectPair<Integer, Integer>> boarderPairs = new ArrayList<ObjectPair<Integer, Integer>>();

    Integer firstPosition = labeledString.indexOf("<B>");
    Integer secondPosition;//w  w w  . j  a va 2 s  . c om
    while (firstPosition != -1) {
        labeledString = labeledString.replaceFirst("<B>", "");
        secondPosition = labeledString.indexOf("</B>");
        labeledString = labeledString.replaceFirst("</B>", "");
        boarderPairs.add(new ObjectPair<Integer, Integer>(firstPosition, secondPosition));

        firstPosition = labeledString.indexOf("<B>");
    }

    //create richTextString, apply font
    HSSFRichTextString richTextString = new HSSFRichTextString(labeledString);

    for (ObjectPair<Integer, Integer> boarderpair : boarderPairs) {
        richTextString.applyFont(boarderpair.getFirst(), boarderpair.getSecond(), partFont);
    }

    return richTextString;
}

From source file:net.sf.jasperreports.engine.export.JRXlsExporter.java

License:Open Source License

protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont,
        Locale locale) {/*from   w ww .j  a  v  a  2  s. c  o m*/
    String text = styledText.getText();
    HSSFRichTextString richTextStr = new HSSFRichTextString(text);
    int runLimit = 0;
    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        Map<Attribute, Object> attributes = iterator.getAttributes();
        JRFont runFont = attributes.isEmpty() ? defaultFont : new JRBaseFont(attributes);
        short runForecolor = attributes.get(TextAttribute.FOREGROUND) != null
                ? getWorkbookColor((Color) attributes.get(TextAttribute.FOREGROUND)).getIndex()
                : forecolor;
        HSSFFont font = getLoadedFont(runFont, runForecolor, attributes, locale);
        richTextStr.applyFont(iterator.getIndex(), runLimit, font);
        iterator.setIndex(runLimit);
    }
    return richTextStr;
}

From source file:org.unitime.timetable.export.XLSPrinter.java

License:Apache License

public void printLine(A... fields) {
    int cellIdx = 0;
    Row row = iSheet.createRow(iRowNum++);
    int nrLines = 1;
    for (int idx = 0; idx < fields.length; idx++) {
        if (iHiddenColumns.contains(idx))
            continue;
        Cell cell = row.createCell(cellIdx++);

        A f = fields[idx];/*from   w w  w  .  j a v  a  2s.c  o  m*/
        if (f == null || f.isEmpty() || (iCheckLast
                && f.equals(iLastLine == null || idx >= iLastLine.length ? null : iLastLine[idx]))) {
            f = new A();
            if (fields[idx] != null && fields[idx].has(F.NOSEPARATOR))
                f.set(F.NOSEPARATOR);
        }

        cell.setCellStyle(getStyle(f, iLastLine == null && !f.has(F.NOSEPARATOR), f.getPattern()));

        if (f.hasBufferedImage()) {
            try {
                addImageToSheet(cellIdx - 1, iRowNum - 1, (HSSFSheet) iSheet, f.getBufferedImage(),
                        EXPAND_ROW_AND_COLUMN);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (f.isNumber()) {
            cell.setCellValue(f.getNumber());
        } else if (f.isDate()) {
            cell.setCellValue(f.getDate());
        } else if (f.hasText()) {
            boolean number = sNumber.matcher(f.getText()).matches();
            if (number && f.has(F.RIGHT)) {
                try {
                    cell.setCellValue(Double.valueOf(f.getText()));
                } catch (NumberFormatException e) {
                    cell.setCellValue(f.getText());
                }
            } else {
                cell.setCellValue(f.getText());
                nrLines = Math.max(nrLines, f.getText().split("\n").length);
            }
        } else if (f.hasChunks()) {
            StringBuffer text = new StringBuffer();
            List<Object[]> font = new ArrayList<Object[]>();
            for (A g : f.getChunks()) {
                if (g.hasText()) {
                    if (text.length() > 0)
                        text.append(f.has(F.INLINE) ? " " : "\n");
                    font.add(new Object[] { text.length(),
                            getFont(g.has(F.BOLD), g.has(F.ITALIC), g.has(F.UNDERLINE), g.getColor())
                                    .getIndex() });
                    text.append(g.getText());
                }
                if (g.hasChunks()) {
                    for (A h : g.getChunks()) {
                        if (h.hasText()) {
                            if (text.length() > 0)
                                text.append(" ");
                            font.add(new Object[] { text.length(),
                                    getFont(h.has(F.BOLD), h.has(F.ITALIC), h.has(F.UNDERLINE), h.getColor())
                                            .getIndex() });
                            text.append(h.getText());
                        }
                    }
                }
            }
            nrLines = Math.max(nrLines, text.toString().split("\n").length);
            font.add(new Object[] { text.length(), (short) 0 });
            HSSFRichTextString value = new HSSFRichTextString(text.toString());
            for (int i = 0; i < font.size() - 1; i++)
                value.applyFont((Integer) font.get(i)[0], (Integer) font.get(1 + i)[0], (Short) font.get(i)[1]);
            cell.setCellValue(value);
        }
    }
    if (nrLines > 1)
        row.setHeightInPoints(
                Math.max(nrLines * iSheet.getDefaultRowHeightInPoints() + 1f, row.getHeightInPoints()));
    iLastLine = fields;
}

From source file:poi.hssf.usermodel.examples.OfficeDrawing.java

License:Apache License

private static void drawSheet4(HSSFSheet sheet4, HSSFWorkbook wb) {
    // Create the drawing patriarch.  This is the top level container for
    // all shapes. This will clear out any existing shapes for that sheet.
    HSSFPatriarch patriarch = sheet4.createDrawingPatriarch();

    // Create a couple of textboxes
    HSSFTextbox textbox1 = patriarch//from   w w w  .ja v a  2s  .  co  m
            .createTextbox(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 2, 2));
    textbox1.setString(new HSSFRichTextString("This is a test"));
    HSSFTextbox textbox2 = patriarch
            .createTextbox(new HSSFClientAnchor(0, 0, 900, 100, (short) 3, 3, (short) 3, 4));
    textbox2.setString(new HSSFRichTextString("Woo"));
    textbox2.setFillColor(200, 0, 0);
    textbox2.setLineStyle(HSSFSimpleShape.LINESTYLE_DOTGEL);

    // Create third one with some fancy font styling.
    HSSFTextbox textbox3 = patriarch
            .createTextbox(new HSSFClientAnchor(0, 0, 900, 100, (short) 4, 4, (short) 5, 4 + 1));
    HSSFFont font = wb.createFont();
    font.setItalic(true);
    font.setUnderline(HSSFFont.U_DOUBLE);
    HSSFRichTextString string = new HSSFRichTextString("Woo!!!");
    string.applyFont(2, 5, font);
    textbox3.setString(string);
    textbox3.setFillColor(0x08000030);
    textbox3.setLineStyle(HSSFSimpleShape.LINESTYLE_NONE); // no line around the textbox.
    textbox3.setNoFill(true); // make it transparent
}

From source file:ubic.pubmedgate.interactions.AirolaXMLReader.java

License:Apache License

public HSSFRichTextString getRichStringSentence(String pair, HSSFFont textFont) {
    String PMID = pairIDToPMID.get(pair);
    ConnectionsDocument doc = p2g.getByPMID(PMID);
    Annotation sentence = pairIDToSentence.get(pair);
    Connection connection = pairIDToConnection.get(pair);

    if (sentence == null) {
        log.info("Error null sentence");
        System.exit(1);//from   w ww  . j  a v a 2s.  c  o  m
    }

    HSSFRichTextString result = new HSSFRichTextString(doc.getAnnotationText(sentence));

    long start = sentence.getStartNode().getOffset();

    Annotation region1 = connection.getPartnerA();
    long entity1Start = region1.getStartNode().getOffset() - start;
    long entity1End = region1.getEndNode().getOffset() - start;
    if (entity1Start < 0) {
        log.warn("Entity start less than zero:" + doc.getPMID() + "\n" + doc.getAnnotationText(sentence));
        entity1Start = 0;
    }

    Annotation region2 = connection.getPartnerB();
    long entity2Start = region2.getStartNode().getOffset() - start;
    long entity2End = region2.getEndNode().getOffset() - start;
    if (entity2Start < 0) {
        log.warn("Entity start less than zero:" + doc.getPMID() + "\n" + doc.getAnnotationText(sentence));
        entity2Start = 0;
    }

    result.applyFont((int) entity1Start, (int) entity1End, textFont);
    result.applyFont((int) entity2Start, (int) entity2End, textFont);

    return result;
}