CellStyle.setAlignment(HorizontalAlignment.CENTER) error

Dre*_*w13 0 java apache-poi

CellStyle stringStyle = workbook.createCellStyle();
stringStyle.setAlignment(HorizontalAlignment.CENTER);
Run Code Online (Sandbox Code Playgroud)

this code produces error:

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

so I change the code to use a short:

CellStyle stringStyle = workbook.createCellStyle();
stringStyle.setAlignment((short)1);
Run Code Online (Sandbox Code Playgroud)

And my IDE complains saying:

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

So it complains that it wants a short when I give it a HorizontalAlignment and that it wants a HorizontalAligment when I give it a short.

Axe*_*ter 5

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

apache poi 3.12其中存在setAlignment(short align)无效并且存在公共最终静态short ALIGN_CENTER = 0x2; 。所以代码是

CellStyle stringStyle = workbook.createCellStyle();
stringStyle.setAlignment(CellStyle.ALIGN_CENTER);
Run Code Online (Sandbox Code Playgroud)

apache poi 3.12

但是,由于apache poi 3.15这种方法已被弃用,因为apache poi 3.17它被去除,现在只有空隙参考setAlignment(的Horizo​​ntalAlignment对齐)可以具有可以使用的Horizo​​ntalAlignment作为parammeter。所以代码是

CellStyle stringStyle = workbook.createCellStyle();
stringStyle.setAlignment(HorizontalAlignment.CENTER);
Run Code Online (Sandbox Code Playgroud)

apache poi 3.17

我怀疑你的问题,从这个事实你的IDE结果,那罐子从不同的版本apache poi 3.12,并apache poi 3.17在你的IDE的类路径。不支持此操作,必须避免。