本文介绍: POI-XWPF组件在3.X版本以上POI下报错问题处理
Caused by: java.lang.NoClassDefFoundError: org/apache/poi/POIXMLDocumentPart
        at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.getFontsDocument(XWPFStylesDocument.java:1477)

报错内容如上: NoClassDefFoundError: org/apache/poi/POIXMLDocumentPart

根据搜索得到该错误是由于POI组件XWPF最新版本也只支持3.X版本的POI导致。由于某段时间框架调整升级到了4.X,但未对该代码相关功能进行回归测试,所以出现异常

参考https://blog.csdn.net/u011852988/article/details/123789135 内容处理方式整理如下

1、下载Central Repository: fr/opensagres/xdocreport/org.apache.poi.xwpf.converter.core/1.0.6源码

2、将报错类源文件复制工程代码

 3、修改源文件中报错的部分

3.1 更换导入的POIXMLDocumentPart类为 

import org.apache.poi.ooxml.POIXMLDocumentPart;

3.2修改报错方法

    private static XWPFSettings getSettings( XWPFDocument document )
    {
        for ( POIXMLDocumentPart p : document.getRelations() )
        {
            PackageRelationshipCollection relationships = p.getPackagePart().getPackage().getRelationships();
            for (PackageRelationship relationship : relationships) {
                String relationshipType = relationship.getRelationshipType();
                if ( relationshipType.equals( XWPFRelation.SETTINGS.getRelation() ) )
                {
                    return (XWPFSettings) p;
                }
            }
        }
        return null;
    }


    private static List<ThemeDocument> getThemeDocuments( XWPFDocument document )
    {
        List<ThemeDocument> themeDocuments = new ArrayList<ThemeDocument>();

        for ( POIXMLDocumentPart p : document.getRelations() )
        {
            PackageRelationshipCollection relationships = p.getPackagePart().getPackage().getRelationships();
            for (PackageRelationship relationship : relationships) {
                String relationshipType = relationship.getRelationshipType();
                if ( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme".equals( relationshipType ) )
                {
                    try
                    {
                        InputStream inputStream = p.getPackagePart().getInputStream();
                        ThemeDocument theme = ThemeDocument.Factory.parse( inputStream );
                        themeDocuments.add( theme );
                    }
                    catch ( Throwable e )
                    {
                        e.printStackTrace();
                    }
                }
            }
        }

        return themeDocuments;
    }

    private static List<FontsDocument> getFontsDocument( XWPFDocument document )
    {

        List<FontsDocument> fontsDocuments = new ArrayList<FontsDocument>();

        for ( POIXMLDocumentPart p : document.getRelations() )
        {
            PackageRelationshipCollection relationships = p.getPackagePart().getPackage().getRelationships();
            for (PackageRelationship relationship : relationships) {
                String relationshipType = relationship.getRelationshipType();
                // "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable"
                if ( XWPFRelation.FONT_TABLE.getRelation().equals( relationshipType ) )
                {
                    try
                    {
                        InputStream inputStream = p.getPackagePart().getInputStream();
                        FontsDocument fontsDocument = FontsDocument.Factory.parse( inputStream );
                        fontsDocuments.add( fontsDocument );
                    }
                    catch ( Exception e )
                    {
                        e.printStackTrace();
                    }
        
                }
            }
        }
        return fontsDocuments;
    }

 

重新部署,可正常运行

原文地址:https://blog.csdn.net/tian168no/article/details/128693916

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_20920.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注