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(short fontIndex) 

Source Link

Document

Applies the specified font to the entire string.

Usage

From source file:org.apache.ofbiz.pricat.AbstractPricatParser.java

License:Apache License

public void writeCommentsToFile(XSSFWorkbook workbook, XSSFSheet sheet) {
    report.println();// w  w  w  . j  av  a2s.  c o  m
    report.print(UtilProperties.getMessage(resource, "WriteCommentsBackToExcel", locale),
            InterfaceReport.FORMAT_NOTE);
    FileOutputStream fos = null;
    XSSFCreationHelper factory = workbook.getCreationHelper();
    XSSFFont boldFont = workbook.createFont();
    boldFont.setFontName("Arial");
    boldFont.setBold(true);
    boldFont.setCharSet(134);
    boldFont.setFontHeightInPoints((short) 9);
    XSSFFont plainFont = workbook.createFont();
    plainFont.setFontName("Arial");
    plainFont.setCharSet(134);
    plainFont.setFontHeightInPoints((short) 9);

    XSSFSheet errorSheet = null;
    if (errorMessages.keySet().size() > 0) {
        String errorSheetName = UtilDateTime.nowDateString("yyyy-MM-dd HHmm") + " Errors";
        errorSheetName = WorkbookUtil.createSafeSheetName(errorSheetName);
        errorSheet = workbook.createSheet(errorSheetName);
        workbook.setSheetOrder(errorSheetName, 0);
        workbook.setActiveSheet(workbook.getSheetIndex(errorSheetName));
        XSSFDrawing drawingPatriarch = errorSheet.getDrawingPatriarch();
        if (drawingPatriarch == null) {
            drawingPatriarch = errorSheet.createDrawingPatriarch();
        }
        for (int i = 0; i <= getHeaderRowNo(); i++) {
            XSSFRow newRow = errorSheet.createRow(i);
            XSSFRow row = sheet.getRow(i);
            newRow.setHeight(row.getHeight());
            copyRow(row, newRow, factory, drawingPatriarch);
        }

        // copy merged regions
        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress mergedRegion = sheet.getMergedRegion(i);
            if (mergedRegion.getFirstRow() < getHeaderRowNo()) {
                errorSheet.addMergedRegion(mergedRegion);
            }
        }

        // copy images
        List<XSSFPictureData> pics = workbook.getAllPictures();
        List<XSSFShape> shapes = sheet.getDrawingPatriarch().getShapes();
        for (int i = 0; i < shapes.size(); i++) {
            XSSFShape shape = shapes.get(i);
            XSSFAnchor anchor = shape.getAnchor();
            if (shape instanceof XSSFPicture && anchor instanceof XSSFClientAnchor) {
                XSSFPicture pic = (XSSFPicture) shape;
                XSSFClientAnchor clientAnchor = (XSSFClientAnchor) anchor;
                if (clientAnchor.getRow1() < getHeaderRowNo()) {
                    for (int j = 0; j < pics.size(); j++) {
                        XSSFPictureData picture = pics.get(j);
                        if (picture.getPackagePart().getPartName()
                                .equals(pic.getPictureData().getPackagePart().getPartName())) {
                            drawingPatriarch.createPicture(clientAnchor, j);
                        }
                    }
                }
            }
        }
    }

    try {
        // set comments in the original sheet
        XSSFDrawing patriarch = sheet.getDrawingPatriarch();
        for (CellReference cell : errorMessages.keySet()) {
            if (cell != null && errorMessages.get(cell) != null) {
                XSSFComment comment = sheet.getCellComment(new CellAddress(cell.getRow(), cell.getCol()));
                boolean isNewComment = false;
                if (comment == null) {
                    XSSFClientAnchor anchor = factory.createClientAnchor();
                    anchor.setDx1(100);
                    anchor.setDx2(100);
                    anchor.setDy1(100);
                    anchor.setDy2(100);
                    anchor.setCol1(cell.getCol());
                    anchor.setCol2(cell.getCol() + 4);
                    anchor.setRow1(cell.getRow());
                    anchor.setRow2(cell.getRow() + 4);
                    anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);

                    comment = patriarch.createCellComment(anchor);
                    isNewComment = true;
                }
                XSSFRichTextString rts = factory.createRichTextString("OFBiz PriCat:\n");
                rts.applyFont(boldFont);
                rts.append(errorMessages.get(cell), plainFont);
                comment.setString(rts);
                comment.setAuthor("Apache OFBiz PriCat");
                if (isNewComment) {
                    sheet.getRow(cell.getRow()).getCell(cell.getCol()).setCellComment(comment);
                    OFBizPricatUtil.formatCommentShape(sheet, cell);
                }
            }
        }

        // set comments in the new error sheet
        XSSFDrawing errorPatriarch = errorSheet.getDrawingPatriarch();
        int newRowNum = getHeaderRowNo() + 1;
        Map<Integer, Integer> rowMapping = new HashMap<Integer, Integer>();
        for (CellReference cell : errorMessages.keySet()) {
            if (cell != null && errorMessages.get(cell) != null) {
                XSSFRow row = sheet.getRow(cell.getRow());
                Integer rowNum = Integer.valueOf(row.getRowNum());
                int errorRow = newRowNum;
                if (rowMapping.containsKey(rowNum)) {
                    errorRow = rowMapping.get(rowNum).intValue();
                } else {
                    XSSFRow newRow = errorSheet.getRow(errorRow);
                    if (newRow == null) {
                        newRow = errorSheet.createRow(errorRow);
                    }
                    rowMapping.put(rowNum, Integer.valueOf(errorRow));
                    newRow.setHeight(row.getHeight());
                    copyRow(row, newRow, factory, errorPatriarch);
                    newRowNum++;
                }
            }
        }

        // write to file
        if (sequenceNum > 0L) {
            File commentedExcel = FileUtil.getFile(tempFilesFolder + userLoginId + "/" + sequenceNum + ".xlsx");
            fos = new FileOutputStream(commentedExcel);
            workbook.write(fos);
        } else {
            fos = new FileOutputStream(pricatFile);
            workbook.write(fos);
        }
        fos.flush();
        fos.close();
        workbook.close();
    } catch (FileNotFoundException e) {
        report.println(e);
        Debug.logError(e, module);
    } catch (IOException e) {
        report.println(e);
        Debug.logError(e, module);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                Debug.logError(e, module);
            }
        }
        if (workbook != null) {
            try {
                workbook.close();
            } catch (IOException e) {
                Debug.logError(e, module);
            }
        }
    }
    report.println(UtilProperties.getMessage(resource, "ok", locale), InterfaceReport.FORMAT_OK);
    report.println();
}

From source file:org.cgiar.ccafs.ap.summaries.projects.xlsx.BaseXLS.java

License:Open Source License

/**
 * This method writes the title box into the given sheet.
 * //www.j a va 2 s  . c  o  m
 * @param sheet is the sheet where you want to write the title box.
 * @param text is the title of the report.
 */
public void writeTitleBox(Sheet sheet, String text) {

    XSSFDrawing draw = (XSSFDrawing) sheet.createDrawingPatriarch();
    XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 1, 1, 1, 1, 3, 6);
    anchor.setAnchorType(2);
    XSSFTextBox textbox = draw.createTextbox(anchor);

    textbox.setFillColor(TEXTBOX_BACKGROUND_COLOR_RGB.getRed(), TEXTBOX_BACKGROUND_COLOR_RGB.getGreen(),
            TEXTBOX_BACKGROUND_COLOR_RGB.getBlue());
    textbox.setVerticalAlignment(VerticalAlignment.CENTER);

    XSSFRichTextString stringX = new XSSFRichTextString();
    Font font = workbook.createFont();
    font.setFontHeightInPoints((short) 20);
    font.setFontName("Tahoma");
    font.setColor(TEXTBOX_FONT_COLOR_INDEX);
    stringX.append(text);

    stringX.applyFont(font);
    textbox.setText(stringX);
}

From source file:se.inera.intyg.rehabstod.service.export.xlsx.XlsxExportServiceImpl.java

License:Open Source License

private XSSFRichTextString diagnosKapitelFormat(String diagnosKapitelId) {

    StringBuilder buf = new StringBuilder();

    buf.append(diagnosKapitelId).append(StringUtils.isNotEmpty(diagnosKapitelId) ? ": " : "");
    // Add the description text for the code
    buf.append(diagnosKapitelService.getDiagnosKapitel(diagnosKapitelId).getName());

    XSSFRichTextString richTextString = new XSSFRichTextString();
    richTextString.setString(buf.toString());
    richTextString.applyFont(defaultFont12);

    richTextString.applyFont(0, diagnosKapitelId.length(), boldFont12);
    return richTextString;
}

From source file:se.inera.intyg.rehabstod.service.export.xlsx.XlsxExportServiceImpl.java

License:Open Source License

private XSSFRichTextString buildPersonnummerRichText(Patient patient) {
    String value = patient.getId();
    XSSFRichTextString richTextString = new XSSFRichTextString();
    richTextString.setString(value);//  www  .j a  va 2 s. com
    richTextString.applyFont(defaultFont11);

    return richTextString;
}

From source file:se.inera.intyg.rehabstod.service.export.xlsx.XlsxExportServiceImpl.java

License:Open Source License

private XSSFRichTextString buildGraderRichText(SjukfallEnhet sf) {

    if (sf.getGrader() == null || sf.getGrader().isEmpty()) {
        return new XSSFRichTextString();
    }/* w  w  w  .j a  v a 2 s .  co  m*/

    StringBuilder buf = new StringBuilder();
    Pair aktivIndicies = null;
    boolean first = true;
    for (Integer grad : sf.getGrader()) {

        if (!first) {
            buf.append(UNICODE_RIGHT_ARROW_SYMBOL).append(" ");
        }

        int currentIndex = buf.length();
        // Store indicies for the aktiv grad so we can make its text bold later.
        if (grad == sf.getAktivGrad()) {
            aktivIndicies = new Pair(currentIndex, currentIndex + ("" + grad + "%").length());
        }
        buf.append("").append(grad).append("% ");

        first = false;
    }
    buf.setLength(buf.length() - 1);

    XSSFRichTextString richTextString = new XSSFRichTextString();
    richTextString.setString(buf.toString());
    richTextString.applyFont(defaultFont11);

    // Uses stored indicies to make the correct part of the rich string bold.
    if (aktivIndicies != null) {
        richTextString.applyFont(aktivIndicies.getI1(), aktivIndicies.getI2(), boldFont11);
    }
    return richTextString;
}