- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 Apache-POI,我尝试在 Excel 工作表中插入一张图像,该图像的左上角位于行的中间,并带有大字体 (Calibri-32)。
使用众所周知的公式,我发现 XSSFClientAnchor
中的 dy1
值应该类似于 260,000。
但是,当打开 Excel 文件时,出现错误,提示内容无法辨认。
接受警告,图像无论如何都会正确显示。
经过一些测试,我发现 dy 的最大值(Excel 没有出现错误)似乎为 190,500
。
我使用的字体导致行高为 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.getColumnWidthInPixels
和 ImageUtils.getRowHeightInPixels计算宽度和高度。
并且您需要根据字体高度设置行高。否则,该行将保持默认高度,直到 Excel
在 View 中呈现工作表。在默认行高下,如果大于 190500,则 dy1
位于行外。因此对于该行来说,它不是有效的 dy1
。
有private CTTransform2D createXfrm(XSSFClientAnchor anchor)在 XSSFDrawing.java
中,计算包含 cy
的 xfrm
。您可以看到,仅当 height +anchor.getDy2()
低于 anchor.getDy1()
时,才会出现负 cy
。
使用XSSF
这就足够了。但是使用SXSSF
使用ImageUtils.getRowHeightInPixels(sheet, row)
计算private CTTransform2D createXfrm(XSSFClientAnchoranchoranchor)
中的行高似乎是错误的。我怀疑这是因为由于流式传输方式,工作表中尚不知道行高。所以我们需要一个解决方法。根据大字体的需要设置默认行高,然后设置图片,然后重置默认行高。
以下内容对我有用:
/**********************************************************************************************************************
* 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
关于java - XSSFClientAnchor 的最大 dy 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47655361/
我的问题:非常具体。我正在尝试想出解析以下文本的最简单方法: ^^domain=domain_value^^version=version_value^^account_type=account_ty
好吧,这就是我的困境: 我正在为 Reddit 子版 block 开发常见问题解答机器人。我在 bool 逻辑方面遇到了麻烦,需要一双更有经验的眼睛(这是我在 Python 中的第一次冒险)。现在,该
它首先遍历所有 y 值,然后遍历所有 x 值。我需要 X 和 y 同时改变。 For x = 3 To lr + 1 For y = 2 To lr anyl.Cells(x, 1)
假设我有一个包含 2 列的 Excel 表格:单元格 A1 到 A10 中的日期和 B1 到 B10 中的值。 我想对五月日期的所有值求和。我有3种可能性: {=SUM((MONTH(A1:A10)=
如何转换 Z-score来自 Z-distribution (standard normal distribution, Gaussian distribution)到 p-value ?我还没有找到
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我遇到过包含日期格式的时间戳日期的情况。然后我想构建一个图表,显示“点击”项目的数量“每天”, //array declaration $array1 = array("Date" => 0); $a
我是scala的新手! 我的问题是,是否有包含成员的案例类 myItem:Option[String] 当我构造类时,我需要将字符串内容包装在: Option("some string") 要么 So
我正在用 PHP 创建一个登录系统。我需要用户使用他或她的用户名或电子邮件或电话号码登录然后使用密码。因为我知道在 Java 中我们会像 email==user^ username == user 这
我在 C++ 项目上使用 sqlite,但是当我在具有文本值的列上使用 WHERE 时出现问题 我创建了一个 sqlite 数据库: CREATE TABLE User( id INTEGER
当构造函数是显式时,它不用于隐式转换。在给定的代码片段中,构造函数被标记为 explicit。那为什么在 foo obj1(10.25); 情况下它可以工作,而在 foo obj2=10.25; 情况
我知道这是一个主观问题,所以如果需要关闭它,我深表歉意,但我觉得它经常出现,让我想知道是否普遍偏爱一种形式而不是另一种形式。 显然,最好的答案是“重构代码,这样你就不需要测试是否存在错误”,但有时没有
这两个 jQuery 选择器有什么区别? 以下是来自 w3schools.com 的定义: [attribute~=value] 选择器选择带有特定属性,其值包含特定字符串。 [attribute*=
为什么我们需要CSS [attribute|=value] Selector根本当 CSS3 [attribute*=value] Selector基本上完成相同的事情,浏览器兼容性几乎相似?是否存在
我正在解决 regx 问题。我已经有一个像这样的 regx [0-9]*([.][0-9]{2})。这是 amont 格式验证。现在,通过此验证,我想包括不应提供 0 金额。比如 10 是有效的,但
我正在研究计算机科学 A 考试的样题,但无法弄清楚为什么以下问题的正确答案是正确的。 考虑以下方法。 public static void mystery(List nums) { for (
好的,我正在编写一个 Perl 程序,它有一个我收集的值的哈希值(完全在一个完全独立的程序中)并提供给这个 Perl 脚本。这个散列是 (string,string) 的散列。 我想通过 3 种方式对
我有一个表数据如下,来自不同的表。仅当第三列具有值“债务”并且第一列(日期)具有最大值时,我才想从第四列中获取最大值。最终值基于 MAX(DATE) 而不是 MAX(PRICE)。所以用简单的语言来说
我有一个奇怪的情况,只有错误状态保存到数据库中。当“状态”应该为 true 时,我的查询仍然执行 false。 我有具有此功能的 Controller public function change_a
我有一个交易表(针对所需列进行了简化): id client_id value 1 1 200 2 2 150 3 1
我是一名优秀的程序员,十分优秀!