gpt4 book ai didi

java - 将图像添加到遵守排序和过滤规则的工作表

转载 作者:行者123 更新时间:2023-12-02 09:42:37 25 4
gpt4 key购买 nike

编辑:这可能是一个常见的 Excel 问题,我正在此处跟踪:https://superuser.com/questions/1457518/adding-images-to-excel-that-obey-both-filtering-and-sorting-rules

我正在生成工作表,其中某些行将嵌入图像。根据我嵌入图像的方式,当隐藏其行的其余数据时,图像不会隐藏,或者当工作表排序时,图像不会排序。

演示此问题的示例应用程序:https://github.com/dan-kirberger/poi-excel-image-issue - 它生成两个工作表。每个都展示了我的问题之一。如果您只想查看生成的工作簿,还有一个包含预先生成的工作表的 examples 文件夹。

在应用任何排序/过滤之前,工作表如下所示:

enter image description here

通过以下方式在工作表上启用排序/过滤:

sheet.setAutoFilter(new CellRangeAddress(sheet.getFirstRowNum(), sheet.getLastRowNum(), 0, 2));

添加图像的代码(也在上面的 github 链接中):

        Drawing drawing = cell.getSheet().createDrawingPatriarch();
XSSFClientAnchor anchor = new XSSFClientAnchor();
anchor.setAnchorType(imageAnchorType);
anchor.setCol1(cell.getColumnIndex());
anchor.setRow1(cell.getRowIndex());
Picture picture = drawing.createPicture(anchor, pictureId);
picture.resize(1, 1);

在该代码段中,imageAnchorType 是决定因素,如果设置为 MOVE_AND_RESIZE,则在过滤器中使用排序功能时,图像不会进行排序: enter image description here

请注意,图像不再与“文本”列匹配。 (带有“1”图片的图像现在位于“Two”文本旁边)

如果imageAnchorType设置为MOVE_DONT_RESIZE,图像会适当排序,但当应用删除图像行的过滤器时,图像仍然存在: enter image description here

我们应用了过滤器来显示“仅文本”列,因此“一”和“三”行数据消失了,但它们的图像仍然存在。

我还应该设置任何其他属性才能使其按我想要的方式工作吗?

最佳答案

问题不仅仅在于 anchor 类型。要同时提供排序和过滤功能,ClientAnchor.AnchorType.MOVE_AND_RESIZE 是正确的。对于排序,移动必须是可能的,对于过滤,调整大小必须是可能的(不可见行的行高为 0)。

但为了支持排序,图片还必须适合已排序的单元格。它们不能突出单元格的大小,否则它们将不会与单元格一起分类。所以 picture.resize 是不可能的,因为调整大小会将图片调整为其原始大小,这可能会大于图片锚定到的单元格的大小。

ClientAnchor提供以下设置:

setCol1 这是 anchor 锚定的第一列。图片的左上边缘从该列的左边缘开始。

setDx1 这是添加到 anchor 所锚定的第一列的左边缘的值。它将图片水平移动远离第一列的左边缘。

setRow1 这是 anchor 锚定的第一行。图片的左上边缘从该行的上边缘开始。

setDy1 这是添加到 anchor 锚定的第一行的上边缘的值。它将图片垂直移动远离第一行的顶部边缘。

setCol2 这是 anchor 锚定的第二列。图片的右下边缘结束于该列的左边缘。

setDx2 这是添加到 anchor 所锚定的第二列的左边缘的值。它将图片的右下边缘水平移动远离第二列的左边缘。这将横向拉宽图片。

setRow2 这是 anchor 锚定的第二行。图片的右下边缘结束于该行的上边缘。

setDy1 这是添加到 anchor 所锚定的第二行顶部边缘的值。它将图片的右下边缘垂直移离第二行的上边缘。这将垂直拉伸(stretch)图片。

要支持排序,Row1Row2 必须是同一行。这样在对该行进行排序时,图片就属于该行。这意味着图片高度只能由Dy2确定。并且图片高度必须适合行高。

以下代码显示了一个示例。图片是我从你的github下载的。

代码:

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.util.IOUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.Units;
import org.apache.poi.xssf.usermodel.*;

class CreateExcelPictures {

static String excelPath = "ExcelWithPictures.xlsx";
static String[][] data = new String[][]{
new String[]{"Image", "Text", "Type"},
new String[]{"", "One", "One and Three"},
new String[]{"", "Two", "Two only"},
new String[]{"", "Three", "One and Three"}
};
static String[] pictureFileNames = new String[]{"one.png", "two.png", "three.png"};
static int pictureWidthPx = 30;
static int pictureHeightPx = 25;
static XSSFWorkbook workbook;
static XSSFSheet sheet;

static void addImage(int col1, int row1, int col2, int row2,
int dx1, int dy1, int dx2, int dy2,
String imageFileName, ClientAnchor.AnchorType anchorType) throws Exception {

InputStream imageInputStream = new FileInputStream(imageFileName);
byte[] bytes = IOUtils.toByteArray(imageInputStream);
int pictureId = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
imageInputStream .close();

XSSFClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
anchor.setAnchorType(anchorType);
// set Col1, Dx1, Row1, Dy1, Col2, Dx2, Row2, Dy2
// only this determines the picture's size then
anchor.setCol1(col1);
anchor.setDx1(dx1);
anchor.setRow1(row1);
anchor.setDy1(dy1);
anchor.setCol2(col2);
anchor.setDx2(dx2);
anchor.setRow2(row2);
anchor.setDy2(dy2);

XSSFDrawing drawing = sheet.createDrawingPatriarch();

XSSFPicture picture = drawing.createPicture(anchor, pictureId);

}

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

workbook = new XSSFWorkbook();
sheet = workbook.createSheet();
int r = 0;
for (String[] rowData : data) {
XSSFRow row = sheet.createRow(r);
int c = 0;
for (String cellData : rowData) {
XSSFCell cell = row.createCell(c++);
cell.setCellValue(cellData);
}
if (r > 0) {
float rowHeight = (float)Units.pixelToPoints(pictureHeightPx); // picture's height must fit into row height
row.setHeightInPoints(rowHeight);
addImage(0, r, 0, r, /*all fits in one cell*/
/*Dx1 = 0 and Dy1 = 0, picture's top left edge starts on top left of the cell*/
Units.pixelToEMU(0), Units.pixelToEMU(0),
/*Dx2 is picture's width and Dy2 is picture's height, picture's bottom right edge ends on that point into the cell*/
Units.pixelToEMU(pictureWidthPx), Units.pixelToEMU(pictureHeightPx),
pictureFileNames[r-1], ClientAnchor.AnchorType.MOVE_AND_RESIZE);
}
r++;
}

sheet.setColumnWidth(2, 15*256);
sheet.setAutoFilter(new CellRangeAddress(0, 3, 0, 2));

FileOutputStream fos = new FileOutputStream(excelPath);
workbook.write(fos);
fos.close();
workbook.close();

}
}

结果:

enter image description here

可以进行排序和过滤。

关于java - 将图像添加到遵守排序和过滤规则的工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56940480/

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