Example usage for org.apache.poi.xssf.usermodel XSSFWorkbook addPicture

List of usage examples for org.apache.poi.xssf.usermodel XSSFWorkbook addPicture

Introduction

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

Prototype

public int addPicture(InputStream is, int format) throws IOException 

Source Link

Document

Adds a picture to the workbook.

Usage

From source file:com.netsteadfast.greenstep.util.SimpleUtils.java

License:Apache License

public static void setCellPicture(XSSFWorkbook wb, XSSFSheet sh, byte[] iconBytes, int row, int col)
        throws Exception {
    int myPictureId = wb.addPicture(iconBytes, XSSFWorkbook.PICTURE_TYPE_PNG);

    XSSFDrawing drawing = sh.createDrawingPatriarch();
    XSSFClientAnchor myAnchor = new XSSFClientAnchor();

    myAnchor.setCol1(col);//  w w w . ja v a  2 s .  c  om
    myAnchor.setRow1(row);

    XSSFPicture myPicture = drawing.createPicture(myAnchor, myPictureId);
    myPicture.resize();
}

From source file:com.respam.comniq.models.POIexcelExporter.java

License:Open Source License

public void excelWriter(JSONObject parsedObj, int rownum) throws IOException {
    String path = System.getProperty("user.home") + File.separator + "comniq" + File.separator + "output";
    File file = new File(path + File.separator + "POImovieInfo.xlsx");

    String thumbnailPath = System.getProperty("user.home") + File.separator + "comniq" + File.separator
            + "output" + File.separator + "thumbnails";
    File posterFile = new File(thumbnailPath + File.separator + parsedObj.get("Title") + ".jpg");

    if (!file.exists()) {
        createFile();/*from  www .  jav a  2 s .  c  o m*/
    }

    if (file.exists() && checked.equals(false)) {
        findLastRow();
    }

    try {
        FileInputStream fis = new FileInputStream(file);
        XSSFWorkbook workbook = new XSSFWorkbook(fis);

        XSSFSheet sheet = workbook.getSheet("Movies");

        Map<String, Object[]> label = new TreeMap<>();
        label.put("1",
                new Object[] { "", parsedObj.get("Title"), parsedObj.get("Released"),
                        parsedObj.get("Metascore"), parsedObj.get("imdbRating"), parsedObj.get("Plot"),
                        parsedObj.get("imdbID"), parsedObj.get("Genre"), parsedObj.get("Director"),
                        parsedObj.get("Actors"), parsedObj.get("Rated"), parsedObj.get("Runtime") });

        Set<String> keyset = label.keySet();

        // Setting Style for the Label Row

        XSSFCellStyle contentStyle = workbook.createCellStyle();
        contentStyle.setWrapText(true);
        contentStyle.setVerticalAlignment(VerticalAlignment.TOP);

        rownum = rownum + lastRow;

        if (posterFile.exists()) {
            InputStream imageStream = new FileInputStream(
                    thumbnailPath + File.separator + parsedObj.get("Title") + ".jpg");
            byte[] imageBytes = IOUtils.toByteArray(imageStream);
            pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);
            imageStream.close();

            CreationHelper helper = workbook.getCreationHelper();
            drawing = sheet.createDrawingPatriarch();
            anchor = helper.createClientAnchor();

        }

        for (String key : keyset) {

            Row row = sheet.createRow(rownum++);
            row.setHeight((short) 2000);
            Object[] objArr = label.get(key);
            int cellnum = 0;
            for (Object obj : objArr) {
                Cell cell = row.createCell(cellnum++);
                cell.setCellStyle(contentStyle);
                cell.setCellValue((String) obj);
            }
            if (posterFile.exists()) {
                anchor.setCol1(0);
                anchor.setRow1(rownum - 1);
                anchor.setCol2(0);
                anchor.setRow2(rownum - 1);
                Picture pict = drawing.createPicture(anchor, pictureureIdx);
                pict.resize(1, 1);
            }
        }
        FileOutputStream out = new FileOutputStream(file);
        workbook.write(out);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.vodafone.poms.ii.helpers.ExportManager.java

private static int addImageToWorkbook(XSSFWorkbook wb, String fileName, int pictureType) throws IOException {
    FileInputStream fis = new FileInputStream(fileName);
    int imgId = wb.addPicture(fis, pictureType);
    fis.close();/* w w w .ja v a  2s . com*/
    return imgId;
}

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();// ww w  .  j av a 2  s . c o m

    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();

}