Example usage for org.apache.poi.hssf.usermodel HSSFClientAnchor setRow1

List of usage examples for org.apache.poi.hssf.usermodel HSSFClientAnchor setRow1

Introduction

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

Prototype

public void setRow1(int row1) 

Source Link

Usage

From source file:com.haulmont.yarg.formatters.impl.xls.HSSFPicturesHelper.java

License:Apache License

public static void searchForAnchors(List escherRecords, List<HSSFClientAnchor> pictures) {
    Iterator recordIter = escherRecords.iterator();
    HSSFClientAnchor anchor = null;
    while (recordIter.hasNext()) {
        Object obj = recordIter.next();
        if (obj instanceof EscherRecord) {
            EscherRecord escherRecord = (EscherRecord) obj;
            if (escherRecord instanceof EscherClientAnchorRecord) {
                EscherClientAnchorRecord anchorRecord = (EscherClientAnchorRecord) escherRecord;
                if (anchor == null)
                    anchor = new HSSFClientAnchor();
                anchor.setDx1(anchorRecord.getDx1());
                anchor.setDx2(anchorRecord.getDx2());
                anchor.setDy1(anchorRecord.getDy1());
                anchor.setDy2(anchorRecord.getDy2());
                anchor.setRow1(anchorRecord.getRow1());
                anchor.setRow2(anchorRecord.getRow2());
                anchor.setCol1(anchorRecord.getCol1());
                anchor.setCol2(anchorRecord.getCol2());
            }//from w w  w  . ja  v a 2s  . co m
            // Recursive call.
            searchForAnchors(escherRecord.getChildRecords(), pictures);
        }
    }
    if (anchor != null)
        pictures.add(anchor);
}

From source file:com.haulmont.yarg.formatters.impl.XLSFormatter.java

License:Apache License

/**
 * Copies all pictures from template sheet to result sheet, shift picture depending on area dependencies
 *
 * @param templateSheet - template sheet
 * @param resultSheet   - result sheet// ww  w .j av  a 2  s  .c  om
 */
protected void copyPicturesFromTemplateToResult(HSSFSheet templateSheet, HSSFSheet resultSheet) {
    List<HSSFClientAnchor> list = getAllAnchors(getEscherAggregate(templateSheet));

    int i = 0;
    if (CollectionUtils.isNotEmpty(orderedPicturesId)) {//just a shitty workaround for anchors without pictures
        for (HSSFClientAnchor anchor : list) {
            Cell topLeft = getCellFromTemplate(new Cell(anchor.getCol1(), anchor.getRow1()));
            anchor.setCol1(topLeft.getCol());
            anchor.setRow1(topLeft.getRow());

            anchor.setCol2(topLeft.getCol() + anchor.getCol2() - anchor.getCol1());
            anchor.setRow2(topLeft.getRow() + anchor.getRow2() - anchor.getRow1());

            HSSFPatriarch sheetPatriarch = drawingPatriarchsMap.get(resultSheet);
            if (sheetPatriarch != null) {
                sheetPatriarch.createPicture(anchor, orderedPicturesId.get(i++));
            }
        }
    }
}

From source file:org.alanwilliamson.openbd.plugin.spreadsheet.functions.SpreadsheetAddImage.java

License:Open Source License

public cfData execute(cfSession _session, List<cfData> parameters) throws cfmRunTimeException {
    cfSpreadSheetData spreadsheet = (cfSpreadSheetData) parameters.get(2);
    String imgPath = parameters.get(1).getString();
    String[] anchor = parameters.get(0).getString().split(",");

    // Check the anchor
    if (anchor.length != 4 && anchor.length != 8)
        throwException(_session, "Invalid Anchor parameter. Must be 4 or 8 comma separated numbers");

    // Determine the file type
    String fileExt = imgPath.substring(imgPath.lastIndexOf(".") + 1).toLowerCase();
    int imgTypeIndex = 0;
    if (fileExt.equals("dib")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_DIB;
    } else if (fileExt.equals("jpg")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_JPEG;
    } else if (fileExt.equals("emf")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_EMF;
    } else if (fileExt.equals("pict")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_PICT;
    } else if (fileExt.equals("png")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_PNG;
    } else if (fileExt.equals("wmf")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_WMF;
    } else// w w  w . j a v a 2s  . c  o m
        throwException(_session, "Unknown file type: " + imgPath);

    // Read the file
    FileInputStream fs = null;
    byte[] fileBuffer = null;
    try {
        fs = new FileInputStream(imgPath);
        fileBuffer = org.apache.poi.util.IOUtils.toByteArray(fs);
    } catch (Exception fe) {
        throwException(_session, "Unable to read file: " + imgPath);
    } finally {
        try {
            fs.close();
        } catch (IOException e) {
        }
    }

    // Add the picture
    int imageIndex = spreadsheet.getWorkBook().addPicture(fileBuffer, imgTypeIndex);

    HSSFClientAnchor clientAnchor = new HSSFClientAnchor();
    if (anchor.length == 4) {
        clientAnchor.setRow1(Integer.valueOf(anchor[0]) - 1);
        clientAnchor.setCol1(Integer.valueOf(anchor[1]) - 1);
        clientAnchor.setRow2(Integer.valueOf(anchor[2]) - 1);
        clientAnchor.setCol2(Integer.valueOf(anchor[3]) - 1);
    } else {
        clientAnchor.setDx1(Integer.valueOf(anchor[0]) - 1);
        clientAnchor.setDy1(Integer.valueOf(anchor[1]) - 1);
        clientAnchor.setDx2(Integer.valueOf(anchor[2]) - 1);
        clientAnchor.setDy2(Integer.valueOf(anchor[3]) - 1);

        clientAnchor.setRow1(Integer.valueOf(anchor[4]) - 1);
        clientAnchor.setCol1(Integer.valueOf(anchor[5]) - 1);
        clientAnchor.setRow2(Integer.valueOf(anchor[6]) - 1);
        clientAnchor.setCol2(Integer.valueOf(anchor[7]) - 1);
    }

    // finalise the image to the sheet
    spreadsheet.getActiveSheet().createDrawingPatriarch().createPicture(clientAnchor, imageIndex);

    return cfBooleanData.TRUE;
}

From source file:org.alanwilliamson.openbd.plugin.spreadsheet.functions.SpreadsheetSetCellComment.java

License:Open Source License

public cfData execute(cfSession _session, List<cfData> parameters) throws cfmRunTimeException {
    if (parameters.get(2).getDataType() != cfData.CFSTRUCTDATA)
        throwException(_session, "parameter must be of type structure");

    cfSpreadSheetData spreadsheet = null;
    cfStructData commentS = null;//from  w  ww  .j  av a 2s .  c  o m
    int rowNo, columnNo;

    /*
     * Collect up the parameters
     */
    spreadsheet = (cfSpreadSheetData) parameters.get(3);
    commentS = (cfStructData) parameters.get(2);
    rowNo = parameters.get(1).getInt() - 1;
    columnNo = parameters.get(0).getInt() - 1;

    if (rowNo < 0)
        throwException(_session, "row must be 1 or greater (" + rowNo + ")");
    if (columnNo < 0)
        throwException(_session, "column must be 1 or greater (" + columnNo + ")");

    /*
     * Perform the insertion
     */
    Sheet sheet = spreadsheet.getActiveSheet();
    Row row = sheet.getRow(rowNo);
    if (row == null)
        row = sheet.createRow(rowNo);

    Cell cell = row.getCell(columnNo);
    if (cell == null)
        cell = row.createCell(columnNo);

    // Create the anchor
    HSSFClientAnchor clientAnchor = new HSSFClientAnchor();
    if (commentS.containsKey("anchor")) {
        String[] anchor = commentS.getData("anchor").getString().split(",");
        if (anchor.length != 4)
            throwException(_session, "Invalid 'anchor' attribute, should be 4 numbers");

        clientAnchor.setRow1(Integer.valueOf(anchor[0]) - 1);
        clientAnchor.setCol1(Integer.valueOf(anchor[1]) - 1);
        clientAnchor.setRow2(Integer.valueOf(anchor[2]) - 1);
        clientAnchor.setCol2(Integer.valueOf(anchor[3]) - 1);
    } else {
        clientAnchor.setRow1(rowNo);
        clientAnchor.setCol1(columnNo);
        clientAnchor.setRow2(rowNo + 2);
        clientAnchor.setCol2(columnNo + 2);
    }

    // Create the comment
    Comment comment = spreadsheet.getActiveSheet().createDrawingPatriarch().createCellComment(clientAnchor);

    if (commentS.containsKey("author")) {
        comment.setAuthor(commentS.getData("author").getString());
    }

    if (commentS.containsKey("visible")) {
        comment.setVisible(commentS.getData("visible").getBoolean());
    }

    if (commentS.containsKey("comment")) {
        HSSFRichTextString richText = new HSSFRichTextString(commentS.getData("comment").getString());
        try {
            richText.applyFont(SpreadSheetFormatOptions.createCommentFont(spreadsheet.getWorkBook(), commentS));
        } catch (Exception e) {
            throwException(_session, e.getMessage());
        }

        comment.setString(richText);
    }

    cell.setCellComment(comment);
    return cfBooleanData.TRUE;
}

From source file:org.jxstar.report.util.ReportXlsUtil.java

/**
 * ???1SHEET1SHEET/*from w w w  .j ava 2 s. co m*/
 * 
 * @param destBook -- 
 * @param srcBook -- ??
 */
private static void copySheetImage(HSSFWorkbook destBook, HSSFWorkbook srcBook) {
    //???
    HSSFSheet srcSheet = srcBook.getSheetAt(0);
    //?
    HSSFSheet destSheet = destBook.getSheetAt(0);

    //???
    int endRowNum = destSheet.getPhysicalNumberOfRows();

    //????
    List<HSSFPicture> lsSrcPicture = getAllPicture(srcSheet);
    _log.showDebug("----------source picture size:" + lsSrcPicture.size());
    if (lsSrcPicture.isEmpty())
        return;

    //?????
    List<HSSFPictureData> lsPicData = null;
    try {
        lsPicData = srcBook.getAllPictures();
    } catch (Exception e) {
        _log.showWarn(
                "book?getAllPictures?book??");

        HSSFWorkbook tmpBook = copyWorkbook(srcBook);
        if (tmpBook != null) {
            lsPicData = tmpBook.getAllPictures();
            tmpBook = null;
        }
        /* ????
        //???
        lsPicData = destBook.getAllPictures();
        if (lsPicData == null || lsPicData.isEmpty()) return;
                
        //??1?
        List<HSSFPictureData> destData = FactoryUtil.newList();
        for (int i = 0, n = lsSrcPicture.size(); i < n; i++) {
           destData.add(lsPicData.get(0));
        }
        lsPicData = destData;*/
    }
    if (lsPicData == null || lsPicData.isEmpty())
        return;
    _log.showDebug("----------source data size:" + lsPicData.size());

    //????
    //????sheet???book
    if (lsSrcPicture.size() > lsPicData.size()) {
        _log.showWarn("?????");
        return;
    }

    //??
    HSSFPatriarch destDraw = destSheet.getDrawingPatriarch();
    if (destDraw == null) {
        destDraw = destSheet.createDrawingPatriarch();
    }

    //??
    List<HSSFPicture> lsDestPicture = getAllPicture(destSheet);
    int index = lsDestPicture.size();

    for (int i = 0, n = lsSrcPicture.size(); i < n; i++) {
        //?
        HSSFPicture picture = lsSrcPicture.get(i);
        //?????
        HSSFPictureData picdata = lsPicData.get(i);
        //??
        byte[] datas = picdata.getData();

        //???
        HSSFClientAnchor anchor = (HSSFClientAnchor) picture.getAnchor();

        //??
        anchor.setRow1(anchor.getRow1() + endRowNum);
        anchor.setRow2(anchor.getRow2() + endRowNum);

        //???
        destBook.addPicture(datas, picdata.getFormat());
        //???????+1??
        index++;
        _log.showDebug("---------copy new image index=" + index);

        destDraw.createPicture(anchor, index);
    }
}

From source file:org.seasar.fisshplate.core.element.Picture.java

License:Apache License

/**
 * ???//w w  w  .  j a v  a  2 s  . c o m
 *
 * @param width
 * @param height
 * @param cellNo
 * @param rowNo
 * @param rowRangeIntVal
 * @param cellRangeIntVal
 * @return
 */
private ClientAnchor createAnchor(int width, int height, int cellNo, int rowNo, int cellRangeIntVal,
        int rowRangeIntVal) {
    HSSFClientAnchor anchor = new HSSFClientAnchor();
    // TODO ????????
    anchor.setDx1(0);
    anchor.setDx2(0);
    anchor.setDy1(0);
    anchor.setDy2(255);

    int fromCellNo = cellNo;
    int toCellNo = cellNo + cellRangeIntVal;
    int fromRowNo = rowNo;
    int toRowNo = rowNo + rowRangeIntVal;

    anchor.setCol1((short) fromCellNo);
    anchor.setCol2((short) toCellNo);
    anchor.setRow1(fromRowNo);
    anchor.setRow2(toRowNo);
    anchor.setAnchorType(2);
    return anchor;
}