Example usage for org.apache.poi.xssf.usermodel XSSFRichTextString applyFont

List of usage examples for org.apache.poi.xssf.usermodel XSSFRichTextString applyFont

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFRichTextString 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:org.cgiar.ccafs.ap.summaries.projects.xlsx.BaseXLS.java

License:Open Source License

/**
 * This method writes string founded value into a specific cell
 * /*from   w  ww. j  av a 2 s.co  m*/
 * @param sheet is the sheet where you want to add information into.
 * @param value is the specific information to be written.
 * @param terms terms to compare the string.
 */
public void writeSearchString(Sheet sheet, String text, String[] terms) {
    this.prepareCell(sheet);
    StringTokenizer tokens;
    String token;
    int begin = 0;

    XSSFRichTextString richText = new XSSFRichTextString();

    if (text == null) {
        cell.setCellValue("");
    } else {
        //
        tokens = new StringTokenizer(text);
        while (tokens.hasMoreTokens()) {
            token = tokens.nextToken().toLowerCase();
            richText.append(token);
            begin = richText.length() - token.length();
            for (Point point : this.auxiliarRichText(token, terms)) {
                richText.applyFont(begin + point.x, begin + point.y + 1, this.richTextFont);
            }

            richText.append(" ");
        }

        if (text.toString().length() > 30) {
            sheet.setColumnWidth(columnCounter, 12000);
        }
        cell.setCellValue(richText);
    }

}

From source file:packtest.WorkingWithRichText.java

License:Apache License

public static void main(String[] args) throws Exception {

    XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
    try {//from   www  . j a  va 2s  .  c  o m
        XSSFSheet sheet = wb.createSheet();
        XSSFRow row = sheet.createRow((short) 2);

        XSSFCell cell = row.createCell(1);
        XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox");

        XSSFFont font1 = wb.createFont();
        font1.setBold(true);
        font1.setColor(new XSSFColor(new java.awt.Color(255, 0, 0)));
        rt.applyFont(0, 10, font1);

        XSSFFont font2 = wb.createFont();
        font2.setItalic(true);
        font2.setUnderline(XSSFFont.U_DOUBLE);
        font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0)));
        rt.applyFont(10, 19, font2);

        XSSFFont font3 = wb.createFont();
        font3.setColor(new XSSFColor(new java.awt.Color(0, 0, 255)));
        rt.append(" Jumped over the lazy dog", font3);

        cell.setCellValue(rt);

        // Write the output to a file
        OutputStream fileOut = new FileOutputStream(Utils.getPath("xssf-richtext.xlsx"));
        try {
            wb.write(fileOut);
        } finally {
            fileOut.close();
        }
    } finally {
        wb.close();
    }
}

From source file:poi.xssf.usermodel.examples.WorkingWithRichText.java

License:Apache License

public static void main(String[] args) throws Exception {

    XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();

    XSSFSheet sheet = wb.createSheet();// ww w. ja v  a  2s .  c om
    XSSFRow row = sheet.createRow((short) 2);

    XSSFCell cell = row.createCell(1);
    XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox");

    XSSFFont font1 = wb.createFont();
    font1.setBold(true);
    font1.setColor(new XSSFColor(new java.awt.Color(255, 0, 0)));
    rt.applyFont(0, 10, font1);

    XSSFFont font2 = wb.createFont();
    font2.setItalic(true);
    font2.setUnderline(XSSFFont.U_DOUBLE);
    font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0)));
    rt.applyFont(10, 19, font2);

    XSSFFont font3 = wb.createFont();
    font3.setColor(new XSSFColor(new java.awt.Color(0, 0, 255)));
    rt.append(" Jumped over the lazy dog", font3);

    cell.setCellValue(rt);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx");
    wb.write(fileOut);
    fileOut.close();
}