Android POI Word to HTML

Continuing from the previous article Android POI generates a word document to the local , and displays the generated word to the page. Android cannot directly display the word, first convert it to html, and then load it with webview. To convert html, you need to use 'fr.opensagres.xdocreport:xdocreport' .

1.gradle depends on POI
dependencies {
    
    
	... 
		implementation ('org.apache.poi:poi-ooxml:3.17'){
    
    
			//poi-ooxml-schemas3.17版本有问题,下面重新依赖.
	        exclude group:'org.apache.poi',module:'poi-ooxml-schemas'
	    }
	    implementation 'org.apache.poi:ooxml-schemas:1.3'
	    implementation 'org.apache.xmlbeans:xmlbeans:3.1.0'
	    implementation 'javax.xml.stream:stax-api:1.0'
	    implementation 'com.fasterxml:aalto-xml:1.2.2'
	    implementation'org.apache.poi:poi-scratchpad:3.17'
	    //word ==> html
	    implementation 'fr.opensagres.xdocreport:xdocreport:2.0.1'
	    }
    ...

Note:
1.xdocreport pay attention to the version number. At the beginning, it has been using 1.0.6, and the conversion has always reported an error NoSuchMethodError: No virtual method getPackageRelationship(). It is normal to replace it with version 2.0.1.
insert image description here

2. ooxml-schemas version problem, the class cannot be found, check the information poi3.17 ooxml-schemas version needs 1.3, other versions may not have complete code, but relying on poi depends on poi-ooxml-schemas3.17 by default, here Can only use exclude: exclude the default. Re-depend on ooxml-schemas1.3

insert image description here

2. word to html

The converted html and webview can be displayed normally, but the image cannot be accessed when opened locally by a third party. This should be caused by the fact that the android app cannot access the private directory of other applications. In the long run, you can inherit the ImageManager upload server and return the remote address to the html .

private void docxToHtml() {
    
    

        String path = FileUtils.getAppRootPth(this) + File.separator + "word" + File.separator + "测试25.docx";

        String img_path = FileUtils.getAppRootPth(this) + File.separator + "word_img"+ File.separator;

        String fileOutName = img_path  + "测试24.html";

        try (FileInputStream fis = new FileInputStream(path)) {
    
    

            XWPFDocument document = new XWPFDocument(fis);

            XHTMLOptions options = XHTMLOptions.create();
            //忽略未用到的样式 默认true
            options.setIgnoreStylesIfUnused(false);
            //将样式都写为内联样式,而不是写到style标签中 默认false
            options.setFragment(true);
            //设置html 图片储存位置.需要传服务器的,可以继承ImageManager,自己处理逻辑.
            options.setImageManager(new ImageManager(new File(img_path),"img"));

            File file = new File(fileOutName);
            //创建所有的父路径,如果不存在父目录的话
            file.getParentFile().mkdirs();
            FileOutputStream fos = new FileOutputStream(file);
            XHTMLConverter.getInstance().convert(document, fos, options);

            Log.i(TAG, "docxToHtml: 转换结束 - -");
            
            String sd_path = "file://" + fileOutName;
            Log.i(TAG, "docxToHtml: sd_path = " + sd_path);
            //android 10以上官方默认false
            mTestBinding.web.getSettings().setAllowFileAccess(true);
            mTestBinding.web.loadUrl(sd_path);
        } catch (IOException e) {
    
    
            throw new RuntimeException(e);
        }
    }

Guess you like

Origin blog.csdn.net/qq_35193677/article/details/129562429