Example usage for org.apache.poi.xssf.usermodel XSSFClientAnchor setAnchorType

List of usage examples for org.apache.poi.xssf.usermodel XSSFClientAnchor setAnchorType

Introduction

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

Prototype

@Override
public void setAnchorType(AnchorType anchorType) 

Source Link

Document

Sets the anchor type

Usage

From source file:nc.noumea.mairie.appock.util.StockSpreadsheetExporter.java

License:Open Source License

private static void addImage(XSSFWorkbook workbook, XSSFSheet worksheet, File file, int rowNum)
        throws IOException {
    //add picture data to this workbook.
    InputStream is = new FileInputStream(file);
    byte[] bytes = IOUtils.toByteArray(is);
    int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
    is.close();//from   w w  w  . j  a  v a  2s  .  com

    XSSFDrawing drawing = worksheet.createDrawingPatriarch();

    //add a picture shape
    XSSFClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
    anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE);
    //set top-left corner of the picture,
    //subsequent call of Picture#resize() will operate relative to it
    anchor.setCol1(0);
    anchor.setRow1(rowNum);

    Picture pict = drawing.createPicture(anchor, pictureIdx);
    //auto-size picture relative to its top-left corner
    pict.resize();

    //get the picture width
    int pictWidthPx = pict.getImageDimension().width;
    int pictHeightPt = pict.getImageDimension().height;

    //get the cell width
    float cellWidthPx = worksheet.getColumnWidthInPixels(0);
    float cellHeightPx = ConvertImageUnits.heightUnits2Pixel(worksheet.getRow(rowNum).getHeight());

    //calculate the center position
    int centerPosPx = Math.round(cellWidthPx / 2f - (float) pictWidthPx / 2f);
    int centerPosPy = Math.round(cellHeightPx / 2f - (float) pictHeightPt / 2f + 10);

    //set the new upper left anchor position
    anchor.setCol1(0);
    //set the remaining pixels up to the center position as Dx in unit EMU
    anchor.setDx1(centerPosPx * Units.EMU_PER_PIXEL);
    anchor.setDy1(centerPosPy * Units.EMU_PER_PIXEL);

    //resize the pictutre to original size again
    //this will determine the new bottom rigth anchor position
    pict.resize();

}

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

License:Apache License

private void copyRow(XSSFRow sourceRow, XSSFRow targetRow, XSSFCreationHelper factory, XSSFDrawing patriarch) {
    for (int j = 0; j < sourceRow.getPhysicalNumberOfCells(); j++) {
        XSSFCell cell = sourceRow.getCell(j);
        if (cell != null) {
            XSSFCell newCell = targetRow.createCell(j);
            int cellType = cell.getCellType();
            newCell.setCellType(cellType);
            switch (cellType) {
            case XSSFCell.CELL_TYPE_BOOLEAN:
                newCell.setCellValue(cell.getBooleanCellValue());
                break;
            case XSSFCell.CELL_TYPE_ERROR:
                newCell.setCellErrorValue(cell.getErrorCellValue());
                break;
            case XSSFCell.CELL_TYPE_FORMULA:
                newCell.setCellFormula(cell.getCellFormula());
                break;
            case XSSFCell.CELL_TYPE_NUMERIC:
                newCell.setCellValue(cell.getNumericCellValue());
                break;
            case XSSFCell.CELL_TYPE_STRING:
                newCell.setCellValue(cell.getRichStringCellValue());
                break;
            default:
                newCell.setCellValue(formatter.formatCellValue(cell));
            }/*from   ww  w  . ja v  a  2 s  .  co  m*/
            if (cell.getCellComment() != null) {
                XSSFClientAnchor anchor = factory.createClientAnchor();
                anchor.setDx1(100);
                anchor.setDx2(100);
                anchor.setDy1(100);
                anchor.setDy2(100);
                anchor.setCol1(newCell.getColumnIndex());
                anchor.setCol2(newCell.getColumnIndex() + 4);
                anchor.setRow1(newCell.getRowIndex());
                anchor.setRow2(newCell.getRowIndex() + 4);
                anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);

                XSSFComment comment = patriarch.createCellComment(anchor);
                comment.setString(cell.getCellComment().getString());
                newCell.setCellComment(comment);
            }
            newCell.setCellStyle(cell.getCellStyle());
            newCell.getSheet().setColumnWidth(newCell.getColumnIndex(),
                    cell.getSheet().getColumnWidth(cell.getColumnIndex()));
        }
    }
}

From source file:org.azkfw.document.tools.DirectoryTreeDocument.java

License:Apache License

private void onFindFile(final DirectoryParserEvent event) {
    DirectoryParserFileInfo info = event.getInfo();
    String prefix = info.getPrefix();
    File file = info.getFile();/*from w  w  w. j av a  2s  .co  m*/

    XSSFRow row = sheet.createRow(offsetRow + countFile);
    int col = 0;
    for (int i = 0; i < prefix.length(); i++) {
        Character c = prefix.charAt(i);

        XSSFCell cell = row.createCell(offsetCol + col);
        cell.setCellValue(c.toString());
        col++;
    }

    XSSFDrawing patriarch = sheet.createDrawingPatriarch();
    XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 18, 18, (short) (offsetCol + col),
            (offsetRow + countFile), (short) (offsetCol + col + 1), (offsetRow + countFile + 1));
    anchor.setAnchorType(XSSFClientAnchor.MOVE_DONT_RESIZE);
    if (file.isFile()) {
        patriarch.createPicture(anchor,
                sheet.getWorkbook().addPicture(imgFile.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
    } else {
        patriarch.createPicture(anchor,
                sheet.getWorkbook().addPicture(imgDirectory.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
    }

    XSSFCell cell = row.createCell(offsetCol + col + 1);
    cell.setCellValue(file.getName());

    maxCol = Math.max(maxCol, col + 1);

    countFile++;
}

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

License:Open Source License

/**
 * @throws IOException// w w  w .jav a2 s.  c om
 */
public void createLogo(Workbook workbook, Sheet sheet) throws IOException {
    // FileInputStream obtains input bytes from the image file
    InputStream inputStream =

            new FileInputStream(
                    new File(config.getResourcePath(), "templates" + File.separator + "logo-ccafs.png"));
    // Get the contents of an InputStream as a byte[].
    byte[] bytes = IOUtils.toByteArray(inputStream);
    // Adds a picture to the workbook
    int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
    // close the input stream
    inputStream.close();

    // Creates the top-level drawing patriarch.
    XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch();

    // Set top-left corner for the image
    XSSFClientAnchor anchor = new XSSFClientAnchor();
    anchor.setAnchorType(2);
    anchor.setCol1(LOGO_POSITION_COLUMN);
    anchor.setRow1(LOGO_POSITION_ROW);

    // Creates a picture
    XSSFPicture pict = drawing.createPicture(anchor, pictureIdx);

    // Reset the image to the original size
    pict.resize();

}

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.
 * //from  ww w .  j  av a  2s .com
 * @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);
}