0、工作上框架引入的poi版本是5.X,而jxls支持的poi版本是4.1.2,在idea中排除jxls的poi过后能正常导出excel,但是部署到测试环境就会报错:Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath
1、源码里面是用类名去找的系统属性,然后再通过反射生成的PoiTransformer这个类,猜测poi更新版本过后可能找不到相关的系统属性了
2、解决办法:
-
排除jxls里面的poi <properties>
<jxls.version>2.9.0</jxls.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>${jxls.version}</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>${jxls.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>net.sf.jxls</groupId>
<artifactId>jxls-core</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
-
重写jxlshelper的createTransformer方法
@Override
public Transformer createTransformer(InputStream templateStream, OutputStream targetStream)
{
PoiTransformer transformer = PoiTransformer.createTransformer(templateStream, targetStream);
transformer.setEvaluateFormulas(false);
return transformer;
}
3、相关代码:
CusJxlsHelper类
public class CusJxlsHelper extends JxlsHelper
{
private static Logger logger = LoggerFactory.getLogger(CusJxlsHelper.class);
public static CusJxlsHelper instance()
{
return new CusJxlsHelper();
}
@Override
public void processTemplate(org.jxls.common.Context context, Transformer transformer) throws IOException
{
AreaBuilder areaBuilder = this.getAreaBuilder();
boolean processFormulas = this.isProcessFormulas();
areaBuilder.setTransformer(transformer);
List<Area> xlsAreaList = areaBuilder.build();
Iterator var4 = xlsAreaList.iterator();
Area xlsArea;
while (var4.hasNext())
{
xlsArea = (Area) var4.next();
xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), context);
}
if (processFormulas)
{
var4 = xlsAreaList.iterator();
while (var4.hasNext())
{
xlsArea = (Area) var4.next();
this.setFormulaProcessor(xlsArea);
xlsArea.processFormulas();
}
}
if (this.isDeleteTemplateSheet())
{
transformer.deleteSheet("TEMPLATE");
}
transformer.write();
}
private Area setFormulaProcessor(Area xlsArea)
{
FormulaProcessor fp = this.getFormulaProcessor();
if (fp == null)
{
if (this.isUseFastFormulaProcessor())
{
fp = new FastFormulaProcessor();
} else
{
fp = new StandardFormulaProcessor();
}
}
xlsArea.setFormulaProcessor(fp);
return xlsArea;
}
@Override
public Transformer createTransformer(InputStream templateStream, OutputStream targetStream)
{
PoiTransformer transformer = PoiTransformer.createTransformer(templateStream, targetStream);
transformer.setEvaluateFormulas(false);
return transformer;
}
}
|