jxls的poi版本冲突

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

jxls的poi版本冲突_第1张图片

1、源码里面是用类名去找的系统属性,然后再通过反射生成的PoiTransformer这个类,猜测poi更新版本过后可能找不到相关的系统属性了

jxls的poi版本冲突_第2张图片

jxls的poi版本冲突_第3张图片

2、解决办法:

  • 排除jxls里面的poi

        <properties>
            <jxls.version>2.9.0jxls.version>
        properties>
        <dependencies>
            
            <dependency>
                <groupId>org.jxlsgroupId>
                <artifactId>jxlsartifactId>
                <version>${jxls.version}version>
                <exclusions>
                    <exclusion>
                        <groupId>ch.qos.logbackgroupId>
                        <artifactId>logback-coreartifactId>
                    exclusion>
                exclusions>
            dependency>
    
            <dependency>
                <groupId>org.jxlsgroupId>
                <artifactId>jxls-poiartifactId>
                <version>${jxls.version}version>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.poigroupId>
                        <artifactId>poi-ooxmlartifactId>
                    exclusion>
                    <exclusion>
                        <groupId>org.apache.poigroupId>
                        <artifactId>poiartifactId>
                    exclusion>
                exclusions>
            dependency>
    
            <dependency>
                <groupId>org.jxlsgroupId>
                <artifactId>jxls-jexcelartifactId>
                <version>1.0.9version>
            dependency>
    
            
            <dependency>
                <groupId>net.sf.jxlsgroupId>
                <artifactId>jxls-coreartifactId>
                <version>1.8version>
            dependency>
    
            
      dependencies>
    
  • 重写jxlshelper的createTransformer方法

	@Override
	public Transformer createTransformer(InputStream templateStream, OutputStream targetStream)
	{

		PoiTransformer transformer = PoiTransformer.createTransformer(templateStream, targetStream);

//		if (super.expressionNotationBegin != null && expressionNotationEnd != null) {
//			transformer.getTransformationConfig().buildExpressionNotation(expressionNotationBegin,
//																		  expressionNotationEnd);
//		}
		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();
	}

	//重写了processTemplate这个方法
	@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();
			}
		}
		//增加了删除template sheet的业务控制
		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);

//		if (super.expressionNotationBegin != null && expressionNotationEnd != null) {
//			transformer.getTransformationConfig().buildExpressionNotation(expressionNotationBegin,
//																		  expressionNotationEnd);
//		}
		transformer.setEvaluateFormulas(false);
		return transformer;

	}
}

你可能感兴趣的:(jxls,poi,java,spring,boot)