gpt4 book ai didi

java - CellStyle.setAlignment(Horizo​​ntalAlignment.CENTER) 错误

转载 作者:行者123 更新时间:2023-12-01 07:46:21 24 4
gpt4 key购买 nike

CellStyle stringStyle = workbook.createCellStyle();
stringStyle.setAlignment(HorizontalAlignment.CENTER);

此代码产生错误:

method setAlignment in interface CellStyle cannot be applied to given types;

required: short

found: HorizontalAlignment

reason: actual argument HorizontalAlignment cannot be converted to short by method invocation conversion

所以我更改代码以使用简短的:

CellStyle stringStyle = workbook.createCellStyle();
stringStyle.setAlignment((short)1);

我的 IDE 提示说:

setAlignment (org.apache.poi.ss.usermodel.HorizontalAlignment) in CellStyle cannot be applied to (short)

所以它提示它想要一个 short当我给它 HorizontalAlignment它想要一个 HorizontalAligment当我给它 short .

最佳答案

您的类路径中必须有不同的 apache poi 版本。所以你的 IDE 对此感到困惑。

apache poi 3.12中有 void setAlignment(short align)还有public final static short ALIGN_CENTER = 0x2; 。所以代码是

CellStyle stringStyle = workbook.createCellStyle();
stringStyle.setAlignment(CellStyle.ALIGN_CENTER);

适用于 apache poi 3.12

但自 apache poi 3.15 起,此方法已被弃用,自 apache poi 3.17 起,该方法已被删除,现在仅 void setAlignment(HorizontalAlignment align)可以使用 HorizontalAlignment作为参数。所以代码是

CellStyle stringStyle = workbook.createCellStyle();
stringStyle.setAlignment(HorizontalAlignment.CENTER);

适用于 apache poi 3.17

我怀疑您的 IDE 问题是由于来自不同版本 apache poi 3.12apache poi 3.17 的 jar 位于 IDE 的类路径中。这是不支持的,必须避免。

关于java - CellStyle.setAlignment(Horizo​​ntalAlignment.CENTER) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51426037/

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