Search in sources :

Example 1 with ImageData

use of com.alibaba.excel.metadata.data.ImageData in project easyexcel by alibaba.

the class WriteTest method imageWrite.

/**
 * 图片导出
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link ImageDemoData}
 * <p>
 * 2. 直接写即可
 */
@Test
public void imageWrite() throws Exception {
    String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx";
    String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg";
    try (InputStream inputStream = FileUtils.openInputStream(new File(imagePath))) {
        List<ImageDemoData> list = ListUtils.newArrayList();
        ImageDemoData imageDemoData = new ImageDemoData();
        list.add(imageDemoData);
        // 放入五种类型的图片 实际使用只要选一种即可
        imageDemoData.setByteArray(FileUtils.readFileToByteArray(new File(imagePath)));
        imageDemoData.setFile(new File(imagePath));
        imageDemoData.setString(imagePath);
        imageDemoData.setInputStream(inputStream);
        imageDemoData.setUrl(new URL("https://raw.githubusercontent.com/alibaba/easyexcel/master/src/test/resources/converter/img.jpg"));
        // 这里演示
        // 需要额外放入文字
        // 而且需要放入2个图片
        // 第一个图片靠左
        // 第二个靠右 而且要额外的占用他后面的单元格
        WriteCellData<Void> writeCellData = new WriteCellData<>();
        imageDemoData.setWriteCellDataFile(writeCellData);
        // 这里可以设置为 EMPTY 则代表不需要其他数据了
        writeCellData.setType(CellDataTypeEnum.STRING);
        writeCellData.setStringValue("额外的放一些文字");
        // 可以放入多个图片
        List<ImageData> imageDataList = new ArrayList<>();
        ImageData imageData = new ImageData();
        imageDataList.add(imageData);
        writeCellData.setImageDataList(imageDataList);
        // 放入2进制图片
        imageData.setImage(FileUtils.readFileToByteArray(new File(imagePath)));
        // 图片类型
        imageData.setImageType(ImageType.PICTURE_TYPE_PNG);
        // 上 右 下 左 需要留空
        // 这个类似于 css 的 margin
        // 这里实测 不能设置太大 超过单元格原始大小后 打开会提示修复。暂时未找到很好的解法。
        imageData.setTop(5);
        imageData.setRight(40);
        imageData.setBottom(5);
        imageData.setLeft(5);
        // 放入第二个图片
        imageData = new ImageData();
        imageDataList.add(imageData);
        writeCellData.setImageDataList(imageDataList);
        imageData.setImage(FileUtils.readFileToByteArray(new File(imagePath)));
        imageData.setImageType(ImageType.PICTURE_TYPE_PNG);
        imageData.setTop(5);
        imageData.setRight(5);
        imageData.setBottom(5);
        imageData.setLeft(50);
        // 设置图片的位置 假设 现在目标 是 覆盖 当前单元格 和当前单元格右边的单元格
        // 起点相对于当前单元格为0 当然可以不写
        imageData.setRelativeFirstRowIndex(0);
        imageData.setRelativeFirstColumnIndex(0);
        imageData.setRelativeLastRowIndex(0);
        // 前面3个可以不写  下面这个需要写 也就是 结尾 需要相对当前单元格 往右移动一格
        // 也就是说 这个图片会覆盖当前单元格和 后面的那一格
        imageData.setRelativeLastColumnIndex(1);
        // 写入数据
        EasyExcel.write(fileName, ImageDemoData.class).sheet().doWrite(list);
    }
}
Also used : WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) InputStream(java.io.InputStream) ImageData(com.alibaba.excel.metadata.data.ImageData) ArrayList(java.util.ArrayList) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 2 with ImageData

use of com.alibaba.excel.metadata.data.ImageData in project easyexcel by alibaba.

the class AbstractExcelWriteExecutor method fillImage.

protected void fillImage(CellWriteHandlerContext cellWriteHandlerContext, List<ImageData> imageDataList) {
    if (CollectionUtils.isEmpty(imageDataList)) {
        return;
    }
    Integer rowIndex = cellWriteHandlerContext.getRowIndex();
    Integer columnIndex = cellWriteHandlerContext.getColumnIndex();
    Sheet sheet = cellWriteHandlerContext.getWriteSheetHolder().getSheet();
    Workbook workbook = cellWriteHandlerContext.getWriteWorkbookHolder().getWorkbook();
    Drawing<?> drawing = sheet.getDrawingPatriarch();
    if (drawing == null) {
        drawing = sheet.createDrawingPatriarch();
    }
    CreationHelper helper = sheet.getWorkbook().getCreationHelper();
    for (ImageData imageData : imageDataList) {
        int index = workbook.addPicture(imageData.getImage(), FileTypeUtils.getImageTypeFormat(imageData.getImage()));
        ClientAnchor anchor = helper.createClientAnchor();
        if (imageData.getTop() != null) {
            anchor.setDy1(StyleUtil.getCoordinate(imageData.getTop()));
        }
        if (imageData.getRight() != null) {
            anchor.setDx2(-StyleUtil.getCoordinate(imageData.getRight()));
        }
        if (imageData.getBottom() != null) {
            anchor.setDy2(-StyleUtil.getCoordinate(imageData.getBottom()));
        }
        if (imageData.getLeft() != null) {
            anchor.setDx1(StyleUtil.getCoordinate(imageData.getLeft()));
        }
        anchor.setRow1(StyleUtil.getCellCoordinate(rowIndex, imageData.getFirstRowIndex(), imageData.getRelativeFirstRowIndex()));
        anchor.setCol1(StyleUtil.getCellCoordinate(columnIndex, imageData.getFirstColumnIndex(), imageData.getRelativeFirstColumnIndex()));
        anchor.setRow2(StyleUtil.getCellCoordinate(rowIndex, imageData.getLastRowIndex(), imageData.getRelativeLastRowIndex()) + 1);
        anchor.setCol2(StyleUtil.getCellCoordinate(columnIndex, imageData.getLastColumnIndex(), imageData.getRelativeLastColumnIndex()) + 1);
        if (imageData.getAnchorType() != null) {
            anchor.setAnchorType(imageData.getAnchorType().getValue());
        }
        drawing.createPicture(anchor, index);
    }
}
Also used : ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) XSSFClientAnchor(org.apache.poi.xssf.usermodel.XSSFClientAnchor) ImageData(com.alibaba.excel.metadata.data.ImageData) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook)

Aggregations

ImageData (com.alibaba.excel.metadata.data.ImageData)2 WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)1 File (java.io.File)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)1 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)1 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 XSSFClientAnchor (org.apache.poi.xssf.usermodel.XSSFClientAnchor)1 Test (org.junit.Test)1