- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:这可能是一个常见的 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
文件夹。
在应用任何排序/过滤之前,工作表如下所示:
通过以下方式在工作表上启用排序/过滤:
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
,则在过滤器中使用排序功能时,图像不会进行排序:
请注意,图像不再与“文本”列匹配。 (带有“1”图片的图像现在位于“Two”文本旁边)
如果imageAnchorType
设置为MOVE_DONT_RESIZE
,图像会适当排序,但当应用删除图像行的过滤器时,图像仍然存在:
我们应用了过滤器来显示“仅文本”列,因此“一”和“三”行数据消失了,但它们的图像仍然存在。
我还应该设置任何其他属性才能使其按我想要的方式工作吗?
最佳答案
问题不仅仅在于 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)图片。
要支持排序,Row1
和 Row2
必须是同一行。这样在对该行进行排序时,图片就属于该行。这意味着图片高度只能由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();
}
}
结果:
可以进行排序和过滤。
关于java - 将图像添加到遵守排序和过滤规则的工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56940480/
我最近一直在学习 Clojure。 Clojure 世界中是否有类似 Scala 的工作表这样的东西,我可以在其中放入任何代码并在保存后立即对其进行评估?或者也许 Clojure 有类似的解决方案?
有人可以帮我吗?我想知道如何过滤工作表中的多个选项卡(C1-C19)。这是我所做的: 我创建了一张表格,将所有回复存储在我的谷歌表单(事件注册表单)中。每个参与者将收到一个坦克编号,每个坦克编号根据其
这就是我将打开的面板显示为 float 窗口的方式。 有人可以帮我将面板作为工作表运行吗?窗口对象是mWindow。我使用的许多标准代码都已被折旧。 NSOpenPanel *openPanel =
当您仅键入 worksheets() 时,默认范围 ActiveWorkbook 或 ThisWorkbook 是什么?对于那些不了解这些区别的人来说,它们非常重要,尤其是在 Excel 2013 中
我有一个带有一些图表的 HTML 页面。我想要做的是编写一个加载 javascript 函数,它将从 excel 表中读取值,将它们存储在变量中并在 html 页面上使用它们。我的问题是是否有任何 j
我需要将参数 callFrom 传递给 SwiftUI 中的工作表。 奇怪的是,该参数在第一次调用时没有使用,但对以下调用有效。 import SwiftUI struct ContentView:
我试着 var tempSheet = wrksheets[sheetName] as Worksheet; 在哪里 wrksheets是类型表 sheetName 是“带空格的工作表名称” 如果
该函数用作“ Assets 类别分配”引擎(在参数范围内具有约束)并在数组的每一行上模拟投资组合模型。我尝试使用四种方法将数组发布到工作表上,但每种方法都失败了。 对于 Assets A、B、C、D
目前,我的 excel 文件有两张表,一张名为“English”,一张名为“French”。 我以编程方式打开我的工作簿并编辑我的英文表,没有任何问题。当我打开第二张工作表时,出现以下错误: The
我添加了一个 VBA 表单 userform和一个模块 Module1在 Excel 中打开 Microsoft VBA 编辑器 (Alt+F11)。 现在,每当我打开任何其他 Excel 时,按 A
在单个 Excel 工作簿中,我想选择各种工作表来运行 VBA 子例程。我找到了显示如何遍历选定工作表的代码,它使用“MsgBox sh.Name”;但是,当我将代码放入其中时,它只会影响选择的最后一
我想知道是否有一个函数可以在 Excel 中加载特定于 Python 的工作表,例如,如果我有 34 张工作表只加载前 25 张工作表。通过以下行,我加载了所有工作表。 xlsx=pd.ExcelFi
我有一个名为“A”、“B”、“C”等的工作表的 xlsx。我需要形成一个名称为“A”、“B”、“C”的表作为第一列,以及来自的一些数据每个工作表中与第二列相同的单元格。例如,这可能看起来像: S
我有一张用密码保护的工作表。当我使用 VBA 更改该表上的任何内容时,我会像这样取消保护: Private Sub Worksheet_Change(ByVal target As Range)
我想将 Excel 文档插入 Excel 工作表。我可以通过以下步骤手动执行此操作; 插入/文本/对象/从文件创建(勾选显示为图标)/浏览。 然后我选择文件并插入文档。 我想通过宏来做到这一点。 (录
是否可以创建 批处理文件那将执行以下操作? 重命名 Excel 文件中的单个工作表(不是 Excel 工作簿/文件本身) 将简单格式应用于 Excel 文件 - 例如,将字体和字体大小应用于整个工作簿
Private Sub CommandButton1_Click() Dim ws As Worksheet With Application.FileDialog(msoFileDialog
我想知道是否可以在不复制该工作表的情况下引用另一本工作簿中的 Excel 工作表? 情况:我有一些非常大的工作表,其中充满了各种数据,但我不想在我的工作簿中保留它们的副本,因为虽然每个工作簿都使用相同
我有这个 Python 字典,我想将这个数据写入 Excel 文件。 注意:有很多类别,每个类别有很多汽车(为简单起见,我使用了 2 个类别) data = {"Category": {"Diesel
我有一个 excel 工作簿,在工作簿中我有 2 张名为 Front Page 和 Drafting 的工作表。起草工作表引用了首页工作表中的一些值。这只是一个基本的引用 我有像这样的公式:='Fro
我是一名优秀的程序员,十分优秀!