簡體   English   中英

如何為 .xlsx 格式 (Apache POI XSSF) 的 Excel 注釋插入背景圖像?

[英]How to insert a background image for an Excel Comment in .xlsx formats (Apache POI XSSF)?

有一個問題解決了如何使用 HSSF Apache POI 在 2007 之前的版本(格式 .xsl)中為 Excel 注釋添加背景圖像。

apache poi 插入帶圖片的評論

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

當從 HSSF 移動到 XSSF 時,這個關鍵方法似乎被刪除了:

HSSFComment        comment;
...
comment.setBackgroundImage(picIndex); // set picture as background image

不支持使用XSSFComment方法。 但如果知道需要創造什么,那也不是不可能。

首先,我們需要創建一個默認注釋,如Quick-Quide CellComments 所示

然后我們需要將圖片數據添加到此工作簿中,如Quick-Guide Images 所示 我們需要XSSFPictureData以便稍后添加引用。

然后我們需要得到 VML 繪圖。 XSSFComments存儲在 VML 圖形中,而不是默認的XSSFDrawings 這不是公開提供的,所以我們需要使用反射來做到這一點。

現在我們需要在 VML 繪圖中設置與圖片數據的關系。

最后我們需要從VML繪圖中取出評論形狀來設置評論形狀的填充以顯示圖片。 沒有高級方法可以做到這一點。 所以我們需要使用低級com.microsoft.schemas.vml.*類的方法。

以下示例需要常見問題中提到的所有模式ooxml-schemas-1.4.jar的完整 jar。 它使用apache poi 4.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);
  }

 }
}

根據這個,添加圖像注釋只是增加了HSSF。

我想你必須使用另一種方法,比如在 apache poi guide 中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM