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

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

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFRichTextString XSSFRichTextString.

Prototype

@Internal
public XSSFRichTextString(CTRst st) 

Source Link

Document

Create a rich text string from the supplied XML bean

Usage

From source file:org.primefaces.extensions.showcase.util.ExcelCustomExporter.java

License:Apache License

protected void addColumnFacets(DataTable table, Sheet sheet, ColumnType columnType) {

    int sheetRowIndex = sheet.getLastRowNum() + 1;
    Row rowHeader = sheet.createRow(sheetRowIndex);

    for (UIColumn col : table.getColumns()) {
        if (!col.isRendered()) {
            continue;
        }/*from  w  w w . jav  a  2  s . co  m*/

        if (col instanceof DynamicColumn) {
            ((DynamicColumn) col).applyModel();
        }

        if (col.isExportable()) {
            // Adding RowIndex for custom Export
            UIComponent component = (UIComponent) col;
            if (component.getId().equalsIgnoreCase("subject")) {

                Cell cell = rowHeader.createCell(0);
                String value = "Index";
                cell.setCellValue(new XSSFRichTextString(value));
            }
            // Adding RowIndex for custom Export
            addColumnValue(rowHeader, col.getFacet(columnType.facet()), "facet");
        }
    }

}

From source file:org.primefaces.extensions.showcase.util.ExcelCustomExporter.java

License:Apache License

protected void addColumnValue(Row row, List<UIComponent> components, String type) {
    int cellIndex = row.getLastCellNum() == -1 ? 0 : row.getLastCellNum();
    Cell cell = row.createCell(cellIndex);
    StringBuilder builder = new StringBuilder();
    FacesContext context = FacesContext.getCurrentInstance();

    for (UIComponent component : components) {
        if (component.isRendered()) {
            String value = exportValue(context, component);

            if (value != null) {
                builder.append(value);/*w  w  w.j  a v  a2  s .c  o  m*/
            }
        }
    }

    cell.setCellValue(new XSSFRichTextString(builder.toString()));

    if (type.equalsIgnoreCase("facet")) {
        // addColumnAlignments(components,facetStyle);
        cell.setCellStyle(facetStyle);
    } else {
        CellStyle cellStyle = this.cellStyle;
        for (UIComponent component : components) {
            cellStyle = addColumnAlignments(component, cellStyle);
            cell.setCellStyle(cellStyle);
        }
    }

}

From source file:org.talend.dataprep.schema.xls.streaming.StreamingSheetReader.java

License:Open Source License

/**
 * Tries to format the contents of the last contents appropriately based on the type of cell and the discovered
 * numeric format.//from ww w  . j  a v  a2s.  co m
 *
 * @return
 */
String formattedContents() {
    switch (currentCell.getType()) {
    case "s": // string stored in shared table
        int idx = Integer.parseInt(lastContents);
        return new XSSFRichTextString(sst.getEntryAt(idx)).toString();
    case "inlineStr": // inline string (not in sst)
        return new XSSFRichTextString(lastContents).toString();
    case "str": //
        return lastContents;
    case "e": // error type
        return StringUtils.EMPTY;// "ERROR:  " + lastContents;
    case "n": // numeric type
        if (currentCell.getNumericFormat() != null && lastContents.length() > 0) {
            return dataFormatter.formatRawCellContents(Double.parseDouble(lastContents),
                    currentCell.getNumericFormatIndex(), currentCell.getNumericFormat());
        } else {
            return lastContents;
        }
    default:
        return lastContents;
    }
}

From source file:org.talend.dataprep.schema.xls.streaming.StreamingSheetReader.java

License:Open Source License

/**
 * Returns the contents of the cell, with no formatting applied
 *
 * @return//  ww w.  java2  s  .  c o  m
 */
String unformattedContents() {
    switch (currentCell.getType()) {
    case "s": // string stored in shared table
        int idx = Integer.parseInt(lastContents);
        return new XSSFRichTextString(sst.getEntryAt(idx)).toString();
    case "inlineStr": // inline string (not in sst)
        return new XSSFRichTextString(lastContents).toString();
    default:
        return lastContents;
    }
}

From source file:packtest.AligningCells.java

License:Apache License

/**
 * Creates a cell and aligns it a certain way.
 *
 * @param wb     the workbook//  w  w w.j av  a2  s  .  c  om
 * @param row    the row to create the cell in
 * @param column the column number to create the cell in
 * @param halign the horizontal alignment for the cell.
 */
private static void createCell(XSSFWorkbook wb, XSSFRow row, short column, short halign, short valign) {
    XSSFCell cell = row.createCell(column);
    cell.setCellValue(new XSSFRichTextString("Align It"));
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(halign);
    cellStyle.setVerticalAlignment(valign);
    cell.setCellStyle(cellStyle);
}

From source file:packtest.AligningCells.java

License:Apache License

/**
 * Center a text over multiple columns using ALIGN_CENTER_SELECTION
 *
 * @param wb the workbook/*from   ww  w . ja v a2s  . co  m*/
 * @param row the row to create the cell in
 * @param start_column  the column number to create the cell in and where the selection starts
 * @param end_column    the column number where the selection ends
 * @param valign the horizontal alignment for the cell.
 *
 * @author Cristian Petrula, Romania
 */
private static void centerAcrossSelection(XSSFWorkbook wb, XSSFRow row, short start_column, short end_column,
        short valign) {

    // Create cell style with ALIGN_CENTER_SELECTION
    XSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER_SELECTION);
    cellStyle.setVerticalAlignment(valign);

    // Create cells over the selected area
    for (int i = start_column; i <= end_column; i++) {
        XSSFCell cell = row.createCell(i);
        cell.setCellStyle(cellStyle);
    }

    // Set value to the first cell
    XSSFCell cell = row.getCell(start_column);
    cell.setCellValue(new XSSFRichTextString("Align It"));

    // Make the selection
    CTRowImpl ctRow = (CTRowImpl) row.getCTRow();

    // Add object with format start_coll:end_coll. For example 1:3 will span from
    // cell 1 to cell 3, where the column index starts with 0
    //
    // You can add multiple spans for one row
    Object span = start_column + ":" + end_column;

    List<Object> spanList = new ArrayList<Object>();
    spanList.add(span);

    //add spns to the row
    ctRow.setSpans(spanList);
}

From source file:packtest.FillsAndColors.java

License:Apache License

public static void main(String[] args) throws Exception {
    Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
    Sheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow((short) 1);

    // Aqua background
    CellStyle style = wb.createCellStyle();
    style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
    style.setFillPattern(CellStyle.BIG_SPOTS);
    Cell cell = row.createCell((short) 1);
    cell.setCellValue(new XSSFRichTextString("X"));
    cell.setCellStyle(style);//from w w w  .  ja  v  a2  s .co m

    // Orange "foreground", foreground being the fill foreground not the font color.
    style = wb.createCellStyle();
    style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    cell = row.createCell((short) 2);
    cell.setCellValue(new XSSFRichTextString("X"));
    cell.setCellStyle(style);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream(Utils.getPath("fill_colors.xlsx"));
    wb.write(fileOut);
    fileOut.close();

}

From source file:packtest.MergingCells.java

License:Apache License

public static void main(String[] args) throws Exception {
    Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
    Sheet sheet = wb.createSheet("new sheet");

    Row row = sheet.createRow((short) 1);
    Cell cell = row.createCell((short) 1);
    cell.setCellValue(new XSSFRichTextString("This is a test of merging"));

    sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx");
    wb.write(fileOut);/*from w  w  w .  ja  v  a2  s .  c  o m*/
    fileOut.close();
}

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   w ww.j a  v a2s  .  co  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.AligningCells.java

License:Apache License

/**
 * Center a text over multiple columns using ALIGN_CENTER_SELECTION
 *
 * @param wb the workbook/* w  w w  .  j a v a2s . co m*/
 * @param row the row to create the cell in
 * @param start_column  the column number to create the cell in and where the selection starts
 * @param end_column    the column number where the selection ends
 * @param valign the horizontal alignment for the cell.
 *
 * @author Cristian Petrula, Romania
 */
private static void centerAcrossSelection(XSSFWorkbook wb, XSSFRow row, short start_column, short end_column,
        short valign) {

    // Create cell style with ALIGN_CENTER_SELECTION
    XSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER_SELECTION);
    cellStyle.setVerticalAlignment(valign);

    // Create cells over the selected area
    for (int i = start_column; i <= end_column; i++) {
        XSSFCell cell = row.createCell(i);
        cell.setCellStyle(cellStyle);
    }

    // Set value to the first cell
    XSSFCell cell = row.getCell(start_column);
    cell.setCellValue(new XSSFRichTextString("Align It"));

    // Make the selection
    CTRowImpl ctRow = (CTRowImpl) row.getCTRow();
    List spanList = new ArrayList();

    // Add object with format start_coll:end_coll. For example 1:3 will span from
    // cell 1 to cell 3, where the column index starts with 0
    //
    // You can add multiple spans for one row
    Object span = start_column + ":" + end_column;
    spanList.add(span);

    //add spns to the row
    ctRow.setSpans(spanList);
}