Search in sources :

Example 1 with XSSFPicture

use of org.apache.poi.xssf.usermodel.XSSFPicture in project bamboobsc by billchen198318.

the class SimpleUtils method setCellPicture.

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);
    myAnchor.setRow1(row);
    XSSFPicture myPicture = drawing.createPicture(myAnchor, myPictureId);
    myPicture.resize();
}
Also used : XSSFClientAnchor(org.apache.poi.xssf.usermodel.XSSFClientAnchor) XSSFPicture(org.apache.poi.xssf.usermodel.XSSFPicture) XSSFDrawing(org.apache.poi.xssf.usermodel.XSSFDrawing)

Example 2 with XSSFPicture

use of org.apache.poi.xssf.usermodel.XSSFPicture in project hutool by looly.

the class ExcelPicUtil method getPicMapXlsx.

/**
 * 获取XLSX工作簿指定sheet中图片列表
 *
 * @param workbook 工作簿{@link Workbook}
 * @param sheetIndex sheet的索引
 * @return 图片映射,键格式:行_列,值:{@link PictureData}
 */
private static Map<String, PictureData> getPicMapXlsx(XSSFWorkbook workbook, int sheetIndex) {
    final Map<String, PictureData> sheetIndexPicMap = new HashMap<>();
    final XSSFSheet sheet = workbook.getSheetAt(sheetIndex);
    XSSFDrawing drawing;
    for (POIXMLDocumentPart dr : sheet.getRelations()) {
        if (dr instanceof XSSFDrawing) {
            drawing = (XSSFDrawing) dr;
            final List<XSSFShape> shapes = drawing.getShapes();
            XSSFPicture pic;
            CTMarker ctMarker;
            for (XSSFShape shape : shapes) {
                if (shape instanceof XSSFPicture) {
                    pic = (XSSFPicture) shape;
                    ctMarker = pic.getPreferredSize().getFrom();
                    sheetIndexPicMap.put(StrUtil.format("{}_{}", ctMarker.getRow(), ctMarker.getCol()), pic.getPictureData());
                }
            // 其他类似于图表等忽略,see: https://gitee.com/loolly/hutool/issues/I38857
            }
        }
    }
    return sheetIndexPicMap;
}
Also used : CTMarker(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker) XSSFShape(org.apache.poi.xssf.usermodel.XSSFShape) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) HashMap(java.util.HashMap) POIXMLDocumentPart(org.apache.poi.ooxml.POIXMLDocumentPart) XSSFPicture(org.apache.poi.xssf.usermodel.XSSFPicture) XSSFDrawing(org.apache.poi.xssf.usermodel.XSSFDrawing) HSSFPictureData(org.apache.poi.hssf.usermodel.HSSFPictureData) PictureData(org.apache.poi.ss.usermodel.PictureData)

Aggregations

XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)2 XSSFPicture (org.apache.poi.xssf.usermodel.XSSFPicture)2 HashMap (java.util.HashMap)1 HSSFPictureData (org.apache.poi.hssf.usermodel.HSSFPictureData)1 POIXMLDocumentPart (org.apache.poi.ooxml.POIXMLDocumentPart)1 PictureData (org.apache.poi.ss.usermodel.PictureData)1 XSSFClientAnchor (org.apache.poi.xssf.usermodel.XSSFClientAnchor)1 XSSFShape (org.apache.poi.xssf.usermodel.XSSFShape)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 CTMarker (org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker)1