word导出手机端乱码或者打不开解决办法

word导出手机端乱码或者打不开解决办法

前言:

本次导出还是使用之前freemarker模板导出word,这次出现手机端打不开是因为手机上的wps版本比较低,因为亲测ios和安卓系统都能打开,但是wps10版本的打开是源码或者乱码,所以找到了两种解决方案可以在低版本wps和microsoft office的手机上打开。
因为是省厅内网,手机上的应用是统一管理的,就当与内网里面有个应用市场,不是你说更新就更新,而且要提交一大串的申请。

先看之前的两种方式导出word:

1.freemarker模板导出word循环图片表格详细教程

这个可以电脑端 ios和安卓高版本的wps打开。

2.freemarker模板导出带表格word详细教程

这个可以在电脑端和ios打开,安卓手机不管什么wps版本打开都乱码。

解决思路:

其实上述的方法最终并不是纯正的word,只是xml格式的word文档,咱们需要吧生成后的文档转换成真正的word格式,才能在老版本的wpsh和microsoft office等应用打开。解决办法还是有好几种的。

解决办法:

一:利用jacob动态链接库进行转存,此方法只适用于winodws,不适用Linux。

1.下载jacob-1.18-x64.dll

2.1.8之前放置在jdk/bin目录下,重新启动项目即可(jdk1.8放置在jdk/jre/bin ),有多种办法,我用的简单的一种。

3.上代码demo:

public static String createNewWord(String path) {
       /**这个path是原来的生成的地址*/
        ActiveXComponent _app = new ActiveXComponent("Word.Application");
        _app.setProperty("Visible", Variant.VT_FALSE);
        Dispatch documents = _app.getProperty("Documents").toDispatch();
        // 打开FreeMarker生成的Word文档
        Dispatch doc = Dispatch.call(documents, "Open",path, Variant.VT_FALSE, Variant.VT_TRUE).toDispatch();
        // 另存为新的Word文档
        String newPath ="D:/daye.doc";
        Dispatch.call(doc, "SaveAs", newPath, Variant.VT_FALSE, Variant.VT_TRUE);

        Dispatch.call(doc, "Close", Variant.VT_FALSE);
        _app.invoke("Quit", new Variant[] {});
        ComThread.Release();
        return newPath;
    }

二:利用Spire.Doc组件读取与写入Word

1.下载jar包:

Spire.Doc.jar

2.pom引入:`

<repositories>
	<repository>
		<id>com.e-iceblue</id>
		<name>e-iceblue</name>
		<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
	</repository>
</repositories>`

<dependency>
		<groupId>e-iceblue</groupId>
		<artifactId>spire.doc</artifactId>
		<version>3.11.0</version>
	</dependency>

3.实战编码:也就是把之前的word转存下

##savePath是之前的地址 savePathResult是转完存的地址
Document document = new Document(savePath);
document.saveToFile(savePathResult, FileFormat.Docx);

三.利用XWPFDocument 或者HWPFDocument 转换生成word

1.pom文件

<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>4.0.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-scratchpad</artifactId>
			<version>3.16</version>
		</dependency>

2.实战编码:

        try{
            OPCPackage open = OPCPackage.open("D:\\home\\ketech\\zj_zby\\zj_zby\\log\\appfile\\20201127101756\\202011275666受害人现勘笔录.doc");
            XWPFDocument xwpfDocument =new XWPFDocument(open);
            FileOutputStream out = new FileOutputStream("D:\\hello.doc");
            xwpfDocument.write(out);
            System.out.println("eee");
//            Document document = new Document("D:\\2020.doc");
//            document.saveToFile("BBB.docx", FileFormat.Docx_2013);
//
//            System.out.println("eee");
//            FileInputStream in = new FileInputStream("D:\\2020.doc");
//            XWPFDocument document = new XWPFDocument(in);
//            HWPFDocument doc = new HWPFDocument(in);
//            CharacterProperties props = new CharacterProperties();
//            FileOutputStream out = new FileOutputStream("D:\\hello.doc");
//            doc.write(out);
//            out.flush();
//            out.close();

        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }

总结

其实上述三种方法都需要在进行操作一次原文档,最好的办法当然是更新手机应用版本,实在不行再这几个方法,而且这几种方法每个里面还有其他操作文档的方法,就看你怎么结合实际应用。

后记

内网WPS版本统一升级了。。。。。就当自己锻炼升级好了,差不多花了四五天时间。

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java大表哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值