我正在尝试使用Apache-POI将图像插入Excel工作表中,该工作表的左上角位于大字体(Calibri-32)行的中间。
使用众所周知的公式,我发现dy1中的XSSFClientAnchor值应约为260,000。
但是,当打开Excel文件时,出现错误,提示内容模糊不清。
接受警告后,影像仍然正确显示。

经过一些测试,我发现dy的最大值似乎是190,500,而没有从Excel中得到错误。
我使用的字体导致行高为55像素。
因此,该行的中途是0.5*55*Units.EMU_PER_PIXEL=261,938

当使用较小的字体但使图像在行尾附近开始时,会发生相同的问题。
在所有情况下,如果dy1的值都大于190500,则会出现错误。

有人知道吗?

更新:
我从xlsx文件中提取了xml,并且发现某个地方的cy值为负。我不太熟悉xlsx的内容,但我希望它对某些人有用:

<xdr:wsDr xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  <xdr:twoCellAnchor editAs="oneCell">
    <xdr:from>
      <xdr:col>2</xdr:col>
      <xdr:colOff>147637</xdr:colOff>
      <xdr:row>0</xdr:row>
      <xdr:rowOff>261937</xdr:rowOff>
    </xdr:from>
    <xdr:to>
      <xdr:col>5</xdr:col>
      <xdr:colOff>2309812</xdr:colOff>
      <xdr:row>13</xdr:row>
      <xdr:rowOff>14287</xdr:rowOff>
    </xdr:to>
    <xdr:pic>
      <xdr:nvPicPr>
        <xdr:cNvPr id="1" name="Picture 1" descr="Picture"/>
        <xdr:cNvPicPr>
          <a:picLocks noChangeAspect="true"/>
        </xdr:cNvPicPr>
      </xdr:nvPicPr>
      <xdr:blipFill>
        <a:blip r:embed="rId1"/>
        <a:stretch>
          <a:fillRect/>
        </a:stretch>
      </xdr:blipFill>
      <xdr:spPr>
        <a:xfrm>
          <a:off x="147637" y="261937"/>
          <a:ext cx="195263" cy="-71437"/>
        </a:xfrm>
        <a:prstGeom prst="rect">
          <a:avLst/>
        </a:prstGeom>
      </xdr:spPr>
    </xdr:pic>
    <xdr:clientData/>
  </xdr:twoCellAnchor>
</xdr:wsDr>


更新2:
以下代码显示该错误。如果dy1大于190500并且row2等于row1 + 1,则会发生这种情况

/**********************************************************************************************************************
 * Package specification
 *********************************************************************************************************************/
package test;

/**********************************************************************************************************************
 * Import definitions
 *********************************************************************************************************************/
import java.awt.Desktop;
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.*;
import org.apache.poi.xssf.streaming.*;
import org.apache.poi.xssf.usermodel.*;

/**********************************************************************************************************************
 * This class implements a Minimal, Complete and Verifiable example for the problem of the maximum dy value for the
 * {@link XSSFClientAnchor}.
 *********************************************************************************************************************/
public class TestPictureOffset
{
  /********************************************************************************************************************
   * This constants represents the name of the file with the picture to import within the sheet.
   *******************************************************************************************************************/
  private static final String FILENAME_PICTURE = "./excel.png";

  /********************************************************************************************************************
   * These constants represents the width and height of the big cell within the sheet.
   *******************************************************************************************************************/
  private static final short BIG_CELL_COLUMN_WIDTH_IN_PIXELS = 317;
  private static final short BIG_CELL_ROW_HEIGHT_IN_PIXELS = 56;

  /********************************************************************************************************************
   * This constants represents the default height of a cell within the sheet.
   *******************************************************************************************************************/
  private static final short DEFAULT_ROW_HEIGHT_IN_PIXELS = 20;

  /********************************************************************************************************************
   * This method places the specified picture on the sheet.
   *******************************************************************************************************************/
  private static void setPicture(int        picture_index,
                                 SXSSFSheet sheet)
  {
    // -----------------
    // Initialize anchor
    // -----------------
    XSSFClientAnchor anchor;
    anchor = (XSSFClientAnchor)sheet.getWorkbook().getCreationHelper().createClientAnchor();
    anchor.setAnchorType(XSSFClientAnchor.AnchorType.MOVE_AND_RESIZE);

    // -----------------------------
    // Set position
    // THIS IS WHERE THE FUN HAPPENS
    // -----------------------------
    anchor.setCol1(1);
    anchor.setRow1(0);
    anchor.setDx1((int)(0.5 * BIG_CELL_COLUMN_WIDTH_IN_PIXELS * Units.EMU_PER_PIXEL));
    anchor.setDy1((int)(0.4 * BIG_CELL_ROW_HEIGHT_IN_PIXELS * Units.EMU_PER_PIXEL));
    anchor.setCol2(anchor.getCol1() + 1);
    anchor.setRow2(anchor.getRow1() + 1);   // Fails if dy1 > 190500
    //anchor.setRow2(anchor.getRow1() + 2); // OK independently from dy1
    anchor.setDx2(0);
    anchor.setDy2(0);

    // ----------------------
    // Show some measurements
    // ----------------------
    System.out.println("Got dy1: " + anchor.getDy1());
    System.out.println("Maximum dy in default cell: " + (DEFAULT_ROW_HEIGHT_IN_PIXELS * Units.EMU_PER_PIXEL));

    // ----------------
    // Draw the picture
    // ----------------
    sheet.createDrawingPatriarch().createPicture(anchor, picture_index);

  } // setPicture

  /********************************************************************************************************************
   * This method runs the application.
   *******************************************************************************************************************/
  private static void run()
    throws Exception
  {
    // ---------------
    // Create workbook
    // ---------------
    SXSSFWorkbook workbook;
    workbook = new SXSSFWorkbook();
    workbook.setCompressTempFiles(true);

    // ------------
    // Create sheet
    // ------------
    SXSSFSheet sheet;
    sheet = workbook.createSheet("TestSheet");
    sheet.trackAllColumnsForAutoSizing();

    // --------------------------
    // Create style with big font
    // --------------------------
    Font          font;
    XSSFCellStyle style;
    font = workbook.createFont();
    font.setFontHeightInPoints((short)32);
    style = (XSSFCellStyle)workbook.createCellStyle();
    style.setFont(font);

    // -------------------
    // Write something big
    // -------------------
    SXSSFRow  row;
    SXSSFCell cell;
    row = sheet.createRow(0);
    cell = row.createCell(1);
    cell.setCellStyle(style);
    cell.setCellValue("Hello everybody");

    // -----------------------
    // Auto resize this column
    // -----------------------
    sheet.autoSizeColumn(1);

    // ------------
    // Load picture
    // ------------
    InputStream input_stream;
    byte[]      bytes;
    input_stream = new FileInputStream(FILENAME_PICTURE);
    bytes = IOUtils.toByteArray(input_stream);
    input_stream.close();

    // ---------------
    // Add to workbook
    // ---------------
    int picture_index;
    picture_index = workbook.addPicture(bytes, SXSSFWorkbook.PICTURE_TYPE_PNG);

    // -------------------------
    // Position picture in sheet
    // -------------------------
    setPicture(picture_index, sheet);

    // -------------
    // Save workbook
    // -------------
    File             output_file;
    FileOutputStream output_stream;
    output_file = new File("testxls.xlsx");
    output_stream = new FileOutputStream(output_file);
    workbook.write(output_stream);
    output_stream.close();
    workbook.close();

    // -------
    // Open it
    // -------
    Desktop.getDesktop().open(output_file);

  } // run

  /********************************************************************************************************************
   *                                                M A I N
   *******************************************************************************************************************/
  public static void main(String[] args)
  {
    try
    {
      run();
    }
    catch (Exception exception)
    {
      exception.printStackTrace();
    }

  } // main

} // class TestPictureOffset

最佳答案

因此,我们需要确定负数cy的来源。

首先:您不应对BIG_CELL_COLUMN_WIDTH_IN_PIXELS和BIG_CELL_ROW_HEIGHT_IN_PIXELS使用常量。而是与apache poi一样,并使用sheet.getColumnWidthInPixelsImageUtils.getRowHeightInPixels计算宽度和高度。

并且您需要根据字体高度设置行高。否则,该行将保持默认高度,直到Excel在视图中呈现工作表。并且在默认的行高中,如果dy1大于190500,则dy1在该行的外部。因此,对于该行,它不是有效的XSSFDrawing.java

xfrm中有private CTTransform2D createXfrm(XSSFClientAnchor anchor),可计算包含cycy。可以看到,只有height + anchor.getDy2()低于anchor.getDy1()时,才会出现负的XSSF

使用SXSSF就足够了。但是使用private CTTransform2D createXfrm(XSSFClientAnchor anchor)ImageUtils.getRowHeightInPixels(sheet, row)计算中的行高似乎是错误的。我怀疑这是因为流方式导致表格中行高度未知。因此,我们需要一种解决方法。将默认行高设置为大字体所需的大,然后设置图片,然后重置默认行高。

以下对我有用:

/**********************************************************************************************************************
 * Import definitions
 *********************************************************************************************************************/
import java.awt.Desktop;
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.*;
import org.apache.poi.xssf.streaming.*;
import org.apache.poi.xssf.usermodel.*;

import org.apache.poi.ss.util.ImageUtils;

/**********************************************************************************************************************
 * This class implements a Minimal, Complete and Verifiable example for the problem of the maximum dy value for the
 * {@link XSSFClientAnchor}.
 *********************************************************************************************************************/
public class TestPictureOffset
{
  /********************************************************************************************************************
   * This constants represents the name of the file with the picture to import within the sheet.
   *******************************************************************************************************************/
  private static final String FILENAME_PICTURE = "./excel.png";

  /********************************************************************************************************************
   * These constants represents the width and height of the big cell within the sheet.
   *******************************************************************************************************************/
  // Don't do that. Instead do the same as apache poi does and use
  // sheet.getColumnWidthInPixels and
  // ImageUtils.getRowHeightInPixels to calculate width and height

  private static final short BIG_CELL_COLUMN_WIDTH_IN_PIXELS = 317;
  private static final short BIG_CELL_ROW_HEIGHT_IN_PIXELS = 56;

  /********************************************************************************************************************
   * This constants represents the default height of a cell within the sheet.
   *******************************************************************************************************************/
  private static final short DEFAULT_ROW_HEIGHT_IN_PIXELS = 20;

  /********************************************************************************************************************
   * This method places the specified picture on the sheet.
   *******************************************************************************************************************/
  private static void setPicture(int        picture_index,
                                 Sheet sheet)
  {
    // -----------------
    // Initialize anchor
    // -----------------
    ClientAnchor anchor;
    anchor = sheet.getWorkbook().getCreationHelper().createClientAnchor();
    anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);

    // -----------------------------
    // Set position
    // THIS IS WHERE THE FUN HAPPENS
    // -----------------------------
    anchor.setCol1(1);
    anchor.setRow1(0);
    //anchor.setDx1((int)(0.5 * BIG_CELL_COLUMN_WIDTH_IN_PIXELS * Units.EMU_PER_PIXEL));
    //anchor.setDy1((int)(0.4 * BIG_CELL_ROW_HEIGHT_IN_PIXELS * Units.EMU_PER_PIXEL));
    anchor.setDx1((int)(0.5 * sheet.getColumnWidthInPixels(1) * Units.EMU_PER_PIXEL));
    anchor.setDy1((int)(0.4 * ImageUtils.getRowHeightInPixels(sheet, 0) * Units.EMU_PER_PIXEL));
    anchor.setCol2(anchor.getCol1() + 1);
    anchor.setRow2(anchor.getRow1() + 1);   // Fails if dy1 > 190500
    //anchor.setRow2(anchor.getRow1() + 2); // OK independently from dy1
    anchor.setDx2(0);
    anchor.setDy2(0);

    // ----------------------
    // Show some measurements
    // ----------------------
    System.out.println("Got dy1: " + anchor.getDy1());
    System.out.println("Maximum dy in default cell: " + (DEFAULT_ROW_HEIGHT_IN_PIXELS * Units.EMU_PER_PIXEL));

    // ----------------
    // Draw the picture
    // ----------------
    sheet.createDrawingPatriarch().createPicture(anchor, picture_index);

  } // setPicture

  /********************************************************************************************************************
   * This method runs the application.
   *******************************************************************************************************************/
  private static void run()
    throws Exception
  {
    // ---------------
    // Create workbook
    // ---------------
    SXSSFWorkbook workbook;
    workbook = new SXSSFWorkbook();
    workbook.setCompressTempFiles(true);

    // ------------
    // Create sheet
    // ------------
    SXSSFSheet sheet;
    sheet = workbook.createSheet("TestSheet");
    sheet.trackAllColumnsForAutoSizing();

    // --------------------------
    // Create style with big font
    // --------------------------
    Font          font;
    CellStyle style;
    font = workbook.createFont();
    font.setFontHeightInPoints((short)32);
    style = workbook.createCellStyle();
    style.setFont(font);

    // -------------------
    // Write something big
    // -------------------
    SXSSFRow  row;
    SXSSFCell cell;
    row = sheet.createRow(0);
    cell = row.createCell(1);
    cell.setCellStyle(style);
    cell.setCellValue("Hello everybody");

    // -----------------------
    // Set row heigth according to the fonts height
    // -----------------------
    row.setHeightInPoints(workbook.getFontAt(style.getFontIndex()).getFontHeightInPoints()*1.275f);

    // -----------------------
    // Auto resize this column
    // -----------------------
    sheet.autoSizeColumn(1);

    // ------------
    // Load picture
    // ------------
    InputStream input_stream;
    byte[]      bytes;
    input_stream = new FileInputStream(FILENAME_PICTURE);
    bytes = IOUtils.toByteArray(input_stream);
    input_stream.close();

    // ---------------
    // Add to workbook
    // ---------------
    int picture_index;
    picture_index = workbook.addPicture(bytes, SXSSFWorkbook.PICTURE_TYPE_PNG);

    // -------------------------
    // Position picture in sheet
    // -------------------------
    // Workaround for SXSSFSheet which has not the correct row height in
    // private CTTransform2D createXfrm(XSSFClientAnchor anchor)
    // because of the streaming manner.
    // So set default row height that big:
    sheet.setDefaultRowHeight(sheet.getRow(0).getHeight());
    // set the picture then:
    setPicture(picture_index, sheet);
    // and reset the default row height after that:
    sheet.setDefaultRowHeight((short)(15*20));

    // -------------
    // Save workbook
    // -------------
    File             output_file;
    FileOutputStream output_stream;
    output_file = new File("testxls.xlsx");
    output_stream = new FileOutputStream(output_file);
    workbook.write(output_stream);
    output_stream.close();
    workbook.close();

    workbook.dispose();

    // -------
    // Open it
    // -------
    Desktop.getDesktop().open(output_file);

  } // run

  /********************************************************************************************************************
   *                                                M A I N
   *******************************************************************************************************************/
  public static void main(String[] args)
  {
    try
    {
      run();
    }
    catch (Exception exception)
    {
      exception.printStackTrace();
    }

  } // main

} // class TestPictureOffset

10-07 17:19