org.apache.poi.POIXMLDocumentPart Java Examples

The following examples show how to use org.apache.poi.POIXMLDocumentPart. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PoiPublicUtil.java    From autopoi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取Excel2007图片
 * 
 * @param sheet
 *            当前sheet对象
 * @param workbook
 *            工作簿对象
 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
 */
public static Map<String, PictureData> getSheetPictrues07(XSSFSheet sheet, XSSFWorkbook workbook) {
	Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
	for (POIXMLDocumentPart dr : sheet.getRelations()) {
		if (dr instanceof XSSFDrawing) {
			XSSFDrawing drawing = (XSSFDrawing) dr;
			List<XSSFShape> shapes = drawing.getShapes();
			for (XSSFShape shape : shapes) {
				XSSFPicture pic = (XSSFPicture) shape;
				XSSFClientAnchor anchor = pic.getPreferredSize();
				CTMarker ctMarker = anchor.getFrom();
				String picIndex = ctMarker.getRow() + "_" + ctMarker.getCol();
				sheetIndexPicMap.put(picIndex, pic.getPictureData());
			}
		}
	}
	return sheetIndexPicMap;
}
 
Example #2
Source File: PoiPublicUtil.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取Excel2007图片
 * 
 * @param sheet
 *            当前sheet对象
 * @param workbook
 *            工作簿对象
 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
 */
public static Map<String, PictureData> getSheetPictrues07(XSSFSheet sheet, XSSFWorkbook workbook) {
	Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
	for (POIXMLDocumentPart dr : sheet.getRelations()) {
		if (dr instanceof XSSFDrawing) {
			XSSFDrawing drawing = (XSSFDrawing) dr;
			List<XSSFShape> shapes = drawing.getShapes();
			for (XSSFShape shape : shapes) {
				XSSFPicture pic = (XSSFPicture) shape;
				XSSFClientAnchor anchor = pic.getPreferredSize();
				CTMarker ctMarker = anchor.getFrom();
				String picIndex = ctMarker.getRow() + "_" + ctMarker.getCol();
				sheetIndexPicMap.put(picIndex, pic.getPictureData());
			}
		}
	}
	return sheetIndexPicMap;
}
 
Example #3
Source File: PoiPublicUtil.java    From easypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取Excel2007图片
 * 
 * @param sheet
 *            当前sheet对象
 * @param workbook
 *            工作簿对象
 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
 */
public static Map<String, PictureData> getSheetPictrues07(XSSFSheet sheet, XSSFWorkbook workbook) {
    Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
    for (POIXMLDocumentPart dr : sheet.getRelations()) {
        if (dr instanceof XSSFDrawing) {
            XSSFDrawing drawing = (XSSFDrawing) dr;
            List<XSSFShape> shapes = drawing.getShapes();
            for (XSSFShape shape : shapes) {
                XSSFPicture pic = (XSSFPicture) shape;
                XSSFClientAnchor anchor = pic.getPreferredSize();
                CTMarker ctMarker = anchor.getFrom();
                String picIndex = ctMarker.getRow() + "_" + ctMarker.getCol();
                sheetIndexPicMap.put(picIndex, pic.getPictureData());
            }
        }
    }
    return sheetIndexPicMap;
}
 
Example #4
Source File: PptTemplates.java    From PPT-Templates with Apache License 2.0 6 votes vote down vote up
/**
 * Replace an image with another while keeping
 * all the properties of the old image: z-index, border, shadow...
 */
private static void replaceImageInPlace(POIXMLDocumentPart containerDocument, ImageToReplace imageToReplace,
		XSLFPictureData newPictureData, Rectangle2D newImageAnchor) {
	RelationPart rp = containerDocument.addRelation(null, XSLFRelation.IMAGES, newPictureData);
	CTPicture pictureXml = (CTPicture) imageToReplace.toReplace.getXmlObject();
	CTBlip pictureBlip = pictureXml.getBlipFill().getBlip();

	String relationId = pictureBlip.getEmbed();
	if(canRelationBeRemoved(containerDocument, relationId)) {
		// clean up the old picture data
		PptPoiBridge.removeRelation(containerDocument, containerDocument.getRelationById(relationId));
	}

	pictureBlip.setEmbed(rp.getRelationship().getId());

	imageToReplace.toReplace.setAnchor(newImageAnchor);
}
 
Example #5
Source File: ExcelPublicUtil.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/** 
 * 获取Excel2007图片 
 * @param sheet 当前sheet对象
 * @param workbook 工作簿对象 
 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData 
 */  
public static Map<String, PictureData> getSheetPictrues07(  
        XSSFSheet sheet, XSSFWorkbook workbook) {  
    Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();  
    for (POIXMLDocumentPart dr : sheet.getRelations()) {  
        if (dr instanceof XSSFDrawing) {  
            XSSFDrawing drawing = (XSSFDrawing) dr;  
            List<XSSFShape> shapes = drawing.getShapes();  
            for (XSSFShape shape : shapes) {  
                XSSFPicture pic = (XSSFPicture) shape;  
                XSSFClientAnchor anchor = pic.getPreferredSize();  
                CTMarker ctMarker = anchor.getFrom();  
                String picIndex = ctMarker.getRow() + "_" + ctMarker.getCol();  
                sheetIndexPicMap.put(picIndex, pic.getPictureData());  
            }  
        }  
    }  
    return sheetIndexPicMap;  
}
 
Example #6
Source File: ConfigResolver.java    From hui-core-autoreport with Apache License 2.0 5 votes vote down vote up
/**
 * 获取表格数据.
 *
 * @param chart     the chart
 * @param paramsMap the params map
 * @return the data map
 * @throws IOException the io exception
 * @since hui_project 1.0.0
 */
public List<ChartSeries> getDataMap(XSLFChart chart, Map<String, Object> paramsMap) throws IOException {
    POIXMLDocumentPart excelPart = chart.getRelations().get(0);
    InputStream excelInputStream = excelPart.getPackagePart().getInputStream();
    Workbook workbook = new XSSFWorkbook(excelInputStream);
    Sheet sheet = workbook.getSheetAt(0);
    String config = sheet.getRow(25).getCell(25).getStringCellValue();
    excelInputStream.close();
    workbook.close();
    Map<String, String> configMap = getConfig(config);
    String sql = "";
    Map<String, String> colRowValueMap = new HashMap<>();
    for (String key : configMap.keySet()) {
        if (CHART_SQL.equals(key)) {
            sql = configMap.get(key);
        } else if (CHART_COL_ROW_VALUE.equals(key)) {
            String colRowValue = configMap.get(key);
            String colRowValueArray[] = colRowValue.split(":");
            for (String s : colRowValueArray) {
                String mapKey = s.substring(0, s.indexOf(","));
                String mapValue = s.substring(s.indexOf(",") + 1, s.length());
                colRowValueMap.put(mapKey, mapValue);
            }
        }

    }
    return analysisGraphData(sql, colRowValueMap, paramsMap);
}
 
Example #7
Source File: WordTableCellContentOleObject.java    From sun-wordtable-read with Apache License 2.0 5 votes vote down vote up
/**
 * 读取Ole对象
 * 
 * @param embedId
 * @param xdoc
 * @return
 */
private WcOleObject readOleObject(String embedId, final XWPFDocument xdoc) {
	if (StringUtils.isBlank(embedId)) {
		return null;
	}
	WcOleObject oleObject = null;
	List<POIXMLDocumentPart> parts = xdoc.getRelations();
	for (POIXMLDocumentPart poixmlDocumentPart : parts) {
		String id = poixmlDocumentPart.getPackageRelationship().getId();
		if (embedId.equals(id)) {
			PackagePart packagePart = poixmlDocumentPart.getPackagePart();

			oleObject = new WcOleObject();
			// oleObjectContent.setFileName(packagePart.getPartName().getName());

			// 解析Ole对象中的文件,参考:http://poi.apache.org/poifs/how-to.html
			try (InputStream is = packagePart.getInputStream();) {
				POIFSFileSystem poifs = new POIFSFileSystem(is);

				if (isOle10NativeObject(poifs.getRoot())) {
					oleObject = readOle10Native(poifs);
				} else {
					oleObject = readDocumentOle(poifs, is);
				}
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
	}

	return oleObject;
}
 
Example #8
Source File: PptTemplates.java    From PPT-Templates with Apache License 2.0 5 votes vote down vote up
private static boolean canRelationBeRemoved(POIXMLDocumentPart containerDocument, String relationId) {
	XSLFSlide currentSlide = findCurrentSlide(containerDocument);
	if(currentSlide == null) {
		// this case where a containerDocument in not part of a slide should not append,
		// but in doubt we don't allow relation to be changed here
		return false;
	}

	return StringUtils.countMatches(currentSlide.getXmlObject().toString(), "r:embed=\""+relationId+"\"") < 2;
}
 
Example #9
Source File: PptTemplates.java    From PPT-Templates with Apache License 2.0 5 votes vote down vote up
private static XSLFSlide findCurrentSlide(POIXMLDocumentPart containerDocument) {
	while(containerDocument != null) {
		if(containerDocument instanceof XSLFSlide) {
			return (XSLFSlide) containerDocument;
		}
		if(containerDocument instanceof XMLSlideShow) {
			return null;
		}

		containerDocument = containerDocument.getParent();
	}
	return null;
}
 
Example #10
Source File: ConfigResolver.java    From hui-core-autoreport with Apache License 2.0 4 votes vote down vote up
/**
 * 读取每页配置文件.
 *
 * @param slide     the slide
 * @param paramsMap the params map
 * @return the slide data
 * @throws IOException the io exception
 * @since hui_project 1.0.0
 */
private SlideData loadSlideConfig(XSLFSlide slide, Map<String, Object> paramsMap) throws IOException {
    SlideData slideData = new SlideData();
    List<ChartData> chartDataList = new ArrayList<>();
    Map<String, String> textMap = new HashMap<>();
    List<TableData> tableDataList = new ArrayList<>();

    List<POIXMLDocumentPart> partList = slide.getRelations();
    for (POIXMLDocumentPart part : partList) {
        if (part instanceof XSLFChart) {
            List<ChartSeries> seriesDataList = getDataMap((XSLFChart) part, paramsMap);
            ChartData chartData = new ChartData();
            chartData.setChartSeriesList(seriesDataList);
            chartDataList.add(chartData);
        }
    }

    List<XSLFShape> shapeList = slide.getShapes();

    for (XSLFShape shape : shapeList) {

        //判断文本框
        if (shape instanceof XSLFTextShape) {
            Map<String, String> tempMap = getDataMap((XSLFTextShape) shape,paramsMap);
            textMap.putAll(tempMap);
        }
        //判断表格
        if (shape instanceof XSLFTable) {
            List<TableRowData> tempList = getDataMap((XSLFTable) shape);
            TableData tableData = new TableData();
            tableData.setTableRowDataList(tempList);
            tableDataList.add(tableData);
        }
    }
    slideData.setTextMap(textMap);
    slideData.setTableDataList(tableDataList);
    slideData.setChartDataList(chartDataList);
    slideData.setSlidePage(slide.getSlideNumber());

    return slideData;
}
 
Example #11
Source File: PowerPointGenerator.java    From hui-core-autoreport with Apache License 2.0 4 votes vote down vote up
/**
 * 替换每一页数据
 *
 * @param slide
 * @param slideData
 * @throws IOException
 */
private static void generatorSlide(XSLFSlide slide, SlideData slideData) {
    List<POIXMLDocumentPart> partList = slide.getRelations();
    //当一页有多个图表的时候
    int chartNum = 0;
    for (POIXMLDocumentPart part : partList) {
        if (part instanceof XSLFChart) {
            ChartData chartData = slideData.getChartDataList().get(chartNum);
            if (chartData == null) {
                throw new ReportExcetion(ReportErrorEnum.CHART_DATA_ERROR.getMsg());
            }
            List<ChartSeries> seriesDataList = chartData.getChartSeriesList();
            if (seriesDataList == null) {
                throw new ReportExcetion(ReportErrorEnum.CHART_SERIER_DATA_ERROR.getMsg());
            }
            //构造PPT
            generatorChart((XSLFChart) part, seriesDataList);
            chartNum++;
        }
    }


    List<XSLFShape> shapeList = slide.getShapes();

    for (XSLFShape shape : shapeList) {
        Map<String, String> textMap = slideData.getTextMap();
        List<TableData> tableDataList = slideData.getTableDataList();
        //当一页有多个表格的时候
        int tableNum = 0;
        //判断文本框
        if (shape instanceof XSLFTextShape) {
            generatorTextBox((XSLFTextShape) shape, textMap);
        }
        //判断表格
        if (shape instanceof XSLFTable) {
            List<TableRowData> tableRowDataList = tableDataList.get(tableNum).getTableRowDataList();
            generatorTable((XSLFTable) shape, tableRowDataList);

        }
        tableNum++;
    }

}
 
Example #12
Source File: PptTemplates.java    From PPT-Templates with Apache License 2.0 4 votes vote down vote up
private static void replaceImage(XMLSlideShow ppt, ShapeContainer<XSLFShape, ?> shapeContainer, ImageToReplace imageToReplace) {
	byte[] newPictureResized = imageToReplace.imageMapper.getReplacementMode().resize(
		imageToReplace.imageMapper.getValue(),
		imageToReplace.imageMapper.getTargetFormat().name(),
		(int) imageToReplace.toReplace.getAnchor().getWidth(),
		(int) imageToReplace.toReplace.getAnchor().getHeight(),
		imageToReplace.getImageMapper().getQualityFactory(),
		imageToReplace.getImageMapper().getQualityMultiplicator()
	);
	if(newPictureResized == null) {
		// if an error occurred during the resizement of the image, the replacement cannot be processed
		return;
	}

	XSLFPictureData newPictureData = ppt.addPicture(newPictureResized, imageToReplace.imageMapper.getTargetFormat());
	Rectangle2D newImageAnchor = computeNewImageAnchor(
		imageToReplace.toReplace.getAnchor(),
		newPictureResized,
		imageToReplace.imageMapper.getReplacementMode(),
		imageToReplace.imageMapper.getQualityMultiplicator()
	);

	if(shapeContainer instanceof POIXMLDocumentPart) {
		replaceImageInPlace((POIXMLDocumentPart) shapeContainer, imageToReplace, newPictureData, newImageAnchor);
	}
	else if(shapeContainer instanceof XSLFGroupShape) {
		replaceImageInPlace(((XSLFGroupShape) shapeContainer).getSheet(), imageToReplace, newPictureData, newImageAnchor);
	}
	// If the container is not a POIXMLDocumentPart or a XSLFGroupShape,
	// the old image have to deleted along with its properties.
	// The new image will just be place in the same area of the old image.
	// This behavior is a fall back that should not append since
	// I don't think the image container can be something else
	// apart from POIXMLDocumentPart and XSLFGroupShape.
	else {
		XSLFPictureShape newPictureShape = (XSLFPictureShape) shapeContainer.createPicture(newPictureData);
		newPictureShape.setAnchor(newImageAnchor);

		shapeContainer.removeShape(imageToReplace.toReplace);
	}
}