Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为.xlsx格式(Apache POI XSSF)的Excel注释插入背景图像?_Java_Excel_Apache Poi - Fatal编程技术网

Java 如何为.xlsx格式(Apache POI XSSF)的Excel注释插入背景图像?

Java 如何为.xlsx格式(Apache POI XSSF)的Excel注释插入背景图像?,java,excel,apache-poi,Java,Excel,Apache Poi,有一个问题可以解决如何使用HSSF Apache POI在2007之前的版本(format.xsl)中为Excel注释添加背景图像的问题 但是查看文档,我找不到XSSF Apache POI(.xslx格式)的等效方法 从HSSF移动到XSSF时,此键方法似乎已被删除: HSSFComment comment; ... comment.setBackgroundImage(picIndex); // set picture as background image 根据,将图像添

有一个问题可以解决如何使用HSSF Apache POI在2007之前的版本(format.xsl)中为Excel注释添加背景图像的问题

但是查看文档,我找不到XSSF Apache POI(.xslx格式)的等效方法

从HSSF移动到XSSF时,此方法似乎已被删除:

HSSFComment        comment;
...
comment.setBackgroundImage(picIndex); // set picture as background image
根据,将图像添加到评论中只是为HSSF添加的


我想您必须使用另一种方法,如ApachePOI。

使用
XSSFComment
方法是不支持的。但如果一个人知道需要创造什么,那么这并非不可能

首先,我们需要创建一个默认注释,如中所示

然后我们需要将图片数据添加到此工作簿,如中所示。我们需要
XSSFPictureData
用于以后添加引用

然后我们需要得到VML图纸<代码>XSSF命令存储在VML图形中,而不是默认的
XSSF图形
。这不是公共提供的,所以我们需要使用反射来实现

现在我们需要在VML绘图中设置与图片数据的关系

最后,我们需要从VML图形中获取注释形状,以设置注释形状的填充以显示图片。这方面没有高层次的方法。因此,我们需要使用低级
com.microsoft.schemas.vml.*
类的方法

下面的示例需要中提到的所有模式
ooxml-schemas-1.4.jar
的完整jar。它是使用ApachePOI4.1.1进行测试的

完整示例:

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.util.IOUtils;

class CreateXSSFCommentWithPicture {

 public static void main(String[] args) throws Exception {

  try (XSSFWorkbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   // First we create a default XSSFComment:

   XSSFCreationHelper factory = workbook.getCreationHelper();

   XSSFSheet sheet = workbook.createSheet("Sheet");
   XSSFRow row = sheet.createRow(3);
   XSSFCell cell = row.createCell(5);
   cell.setCellValue("F4");

   XSSFDrawing drawing = sheet.createDrawingPatriarch();

   XSSFClientAnchor anchor = factory.createClientAnchor();
   anchor.setCol1(cell.getColumnIndex());
   anchor.setCol2(cell.getColumnIndex()+2);
   anchor.setRow1(row.getRowNum());
   anchor.setRow2(row.getRowNum()+5);

   XSSFComment comment = drawing.createCellComment(anchor);
   XSSFRichTextString str = factory.createRichTextString("Hello, World!");
   comment.setString(str);
   comment.setAuthor("Apache POI");

   // assign the comment to the cell
   cell.setCellComment(comment);


   // Now we put the image as fill of the comment's shape:

   // add picture data to this workbook
   InputStream is = new FileInputStream("samplePict.jpeg");
   byte[] bytes = IOUtils.toByteArray(is);
   int pictureIdx = workbook.addPicture(bytes, XSSFWorkbook.PICTURE_TYPE_JPEG);
   is.close();
   // get picture data
   XSSFPictureData pictureData = workbook.getAllPictures().get(pictureIdx);

   // get VML drawing
   java.lang.reflect.Method getVMLDrawing = XSSFSheet.class.getDeclaredMethod("getVMLDrawing", boolean.class);
   getVMLDrawing.setAccessible(true);
   XSSFVMLDrawing vml = (XSSFVMLDrawing)getVMLDrawing.invoke(sheet, true);

   // set relation to the picture data in VML drawing
   org.apache.poi.ooxml.POIXMLDocumentPart.RelationPart rp = vml.addRelation(null, XSSFRelation.IMAGES, pictureData);

   // get comment shape
   com.microsoft.schemas.vml.CTShape commentShape = vml.findCommentShape(cell.getRow().getRowNum(), cell.getColumnIndex());
   // get fill of comment shape
   com.microsoft.schemas.vml.CTFill fill = commentShape.getFillArray(0);
   // already set color needs to be color2 now
   fill.setColor2(fill.getColor());
   fill.unsetColor();
   // set relation Id of the picture
   fill.setRelid(rp.getRelationship().getId());
   // set some other properties
   fill.setTitle("samplePict");
   fill.setRecolor(com.microsoft.schemas.vml.STTrueFalse.T);
   fill.setRotate(com.microsoft.schemas.vml.STTrueFalse.T);
   fill.setType(com.microsoft.schemas.vml.STFillType.FRAME);

   workbook.write(fileout);
  }

 }
}

您使用的是哪一版本的poi ooxml模式?即使代码对我有意义(我怀疑它与“microsoft vml”有关),图像也不会被渲染。statis import STFillType枚举在我的软件包下不可用。使用版本4.0.0和4.1.1Ok进行测试,诀窍是使用ooxml-schemas-1.4.jar,它包含所有模式,并在pom中明确排除“poi ooxml schemas”jar的任何可传递依赖项。@Strings:是的,您是正确的。我忘了提那件事了。现在我已相应地补充了我的答复。