word匯出手機端亂碼或者打不開解決辦法

Java大表哥發表於2020-11-30

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版本統一升級了。。。。。就當自己鍛鍊升級好了,差不多花了四五天時間。

相關文章