在使用poi在excel中的同一单元格中添加图像和文本,然后图像覆盖文本 [英] on adding image and text in same cell in excel using poi then image override the text

查看:422
本文介绍了在使用poi在excel中的同一单元格中添加图像和文本,然后图像覆盖文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用

 anchor.setCol1(1);
     anchor.setRow1(1); 
     anchor.setCol2(2); 
     anchor.setRow2(1); 
     Picture pict = drawing.createPicture(anchor, pictureIdx);
     pict.resize();

,但是图像会覆盖该单元格中存在的文本.有没有一种方法可以在一个单元格中一个接一个地添加带有文本的图像,而无需多行显示.我也使用过autoSizeColumn()方法,但没有解决.

but the the image override the text present in that cell. Is there any way to add image with text one after other in single cell without having in multiple line. I have also used autoSizeColumn() method but not worked out.

推荐答案

Excel工作表中,图片不在单元格中,而是悬停在单元格上方的一层中.它们通过以下方式锚定到单元:

In Excel sheets pictures are not in cells but hovers in a layer over the cells. They are anchored to the cells in the following manner:

一个单元格锚点确定图片的左上位置.如果使用了图片,则必须将其调整为原始尺寸.

A one cell anchor determines the upper left position of the picture. If used, the picture must be resized to its native size.

两个单元格的锚点确定图片的左上位置.第一个锚点确定左上位置,而第二个锚点确定右下位置.这样就给出了大小.

A two cell anchor determines the upper left position and the size of the picture. The first anchor determines the upper left position while the second anchor determines the bottom right position. So the size is given.

每个锚可以指定行和列,还可以指定dxdy. dxdy将添加到列和行的位置,以确定最终位置. dxdy的测量单位是EMU. Apache poi提供org.apache.poi.util.Units以根据像素计算EMU.

Each anchor can have a row and column given but also a dx and dy. The dx and dy will be added to the column's and row's position to determine the final position. The measurement unit for dx and dy is EMU. Apache poi provides org.apache.poi.util.Units to calculate EMU from pixels for example.

示例:

import java.io.*;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;

public class ExcelDrawImagesOnCellLeft {

 private static void drawImageOnExcelSheet(XSSFSheet sheet, int row, int col, 
  int height, int width, int pictureIdx) throws Exception {

  CreationHelper helper = sheet.getWorkbook().getCreationHelper();

  Drawing drawing = sheet.createDrawingPatriarch();

  ClientAnchor anchor = helper.createClientAnchor();
  anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);

  anchor.setCol1(col); //first anchor determines upper left position
  anchor.setRow1(row);

  anchor.setRow2(row); //second anchor determines bottom right position
  anchor.setCol2(col);
  anchor.setDx2(Units.toEMU(width)); //dx = left + wanted width
  anchor.setDy2(Units.toEMU(height)); //dy= top + wanted height

  drawing.createPicture(anchor, pictureIdx);

 }

 public static void main(String[] args) throws Exception {
  Workbook wb = new XSSFWorkbook();
  Sheet sheet = wb.createSheet();

  InputStream is = new FileInputStream("samplePict.jpeg");
  byte[] bytes = IOUtils.toByteArray(is);
  int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
  is.close();

  String gap = "      ";

  for (int r = 0; r < 10; r++ ) {
   sheet.createRow(r).createCell(1).setCellValue(gap + "Picture " + (r+1));
   drawImageOnExcelSheet((XSSFSheet)sheet, r, 1, 12, 12, pictureIdx);
  }

  sheet.autoSizeColumn(1);

  wb.write(new FileOutputStream("ExcelDrawImagesOnCellLeft.xlsx"));
  wb.close();
 }
}

结果:

这篇关于在使用poi在excel中的同一单元格中添加图像和文本,然后图像覆盖文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆