gpt4 book ai didi

java - Apache POI HSSF : How to add shape which would not resize along with corresponding cell?

转载 作者:行者123 更新时间:2023-12-01 04:58:39 28 4
gpt4 key购买 nike

我需要使用 POI 将形状(特别是圆形)添加到 xls 文档。因此,我编写了以下代码:

private void drawLabel(HSSFSheet sheet, HSSFCell cell) {
final short columnIndex = (short) cell.getColumnIndex();
final int rowIndex = cell.getRowIndex();
HSSFClientAnchor anchor = new HSSFClientAnchor(60, 60, 200, 200, columnIndex, rowIndex, columnIndex, rowIndex);
anchor.setAnchorType(ClientAnchor.MOVE_DONT_RESIZE);
HSSFSimpleShape shape = sheet.getDrawingPatriarch().createSimpleShape(anchor);
shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
...
}

它有效。但是,如果我尝试以编程方式更改列宽(例如 sheet.setColumnWidth(0, 5 * 256); (假设形状已添加到列索引 == 0 的单元格中),则圆形会扭曲并且变得像一个椭圆形(尽管 anchor 的类型设置为 MOVE_DONT_RESIZE)。我的方法有什么问题,有没有办法绘制一个不会随相应单元格调整大小的形状?

最佳答案

我还没有找到这个问题的正确解决方案。但解决问题的一种方法是根据其实际大小缩放形状(特别是其 anchor ):

private void drawLabel(HSSFSheet sheet, HSSFCell cell) {
final int defaultColumnWidth = 2048; // column width before resizing
final int defaultRowHeight = 255; // row height before resizing
final double xDistortionFactor = 1.0 * defaultColumnWidth / sheet.getColumnWidth(cell.getColumnIndex());
final double yDistortionFactor = 1.0 * defaultRowHeight / cell.getRow().getHeight();

final int dx1 = (int) (60 * xDistortionFactor);
final int dy1 = (int) (60 * yDistortionFactor);
final int dx2 = (int) (200 * xDistortionFactor);
final int dy2 = (int) (200 * yDistortionFactor);
final short columnIndex = (short) cell.getColumnIndex();
final int rowIndex = cell.getRowIndex();
final HSSFClientAnchor anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, columnIndex, rowIndex, columnIndex, rowIndex);
anchor.setAnchorType(ClientAnchor.MOVE_DONT_RESIZE);

HSSFSimpleShape shape = sheet.getDrawingPatriarch().createSimpleShape(anchor);
shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
...
}

这个解决方案并不优雅,但它确实有效。

关于java - Apache POI HSSF : How to add shape which would not resize along with corresponding cell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13686389/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com