admin 管理员组

文章数量: 1086866

【Unity】在Unity中使用Apose.Word报“The document appears to be corrupted and cannot be loaded.

【Unity】在Unity中使用Apose.Word报“The document appears to be corrupted and cannot be loaded.“ 实例Document时候报错

在Unity里使用Apose.Word做文档处理时遇到一个问题,在Editor中代码跑得好好的,但是一打包成可执行程序,在构造Document实例时就会报错
NullReferenceException: Object reference not set to an instance of an objectat ​ ..ctor (System.IO.Stream , System.Text.Encoding ) [0x0005c] in <3744d28b9011402ac2d2d8a596ab4432>:0 at    . (System.IO.Stream , System.Text.Encoding ) [0x00000] in <3744d28b9011402ac2d2d8a596ab4432>:0 at Aspose.Words.Document. (System.IO.Stream , Aspose.Words.LoadOptions ) [0x0007e] in <3744d28b9011402ac2d2d8a596ab4432>:0 
Rethrow as FileCorruptedException: The document appears to be corrupted and cannot be loaded.at Aspose.Words.Document. (System.IO.Stream , Aspose.Words.LoadOptions ) [0x00118] in <3744d28b9011402ac2d2d8a596ab4432>:0 at Aspose.Words.Document. (System.IO.Stream , Aspose.Words.LoadOptions ) [0x00061] in <3744d28b9011402ac2d2d8a596ab4432>:0 at Aspose.Words.Document..ctor (System.String fileName, Aspose.Words.LoadOptions loadOptions) [0x000a2] in <3744d28b9011402ac2d2d8a596ab4432>:0 at Aspose.Words.Document..ctor (System.String fileName) [0x00000] in <3744d28b9011402ac2d2d8a596ab4432>:0 at ConsoleApp1.Program.Main (System.String[] args) [0x00019] in <34b337260559449a931b945d69583ede>:0 

找了半天问题,最后发现是Aspose.Word中有部分依赖没有导入,解决方法:

  1. 找到当前API库文件夹
    首先确定ProjectSetting -> Player -> Other Settings -> Api Compatibility Level,如果是.NET Standard 2.0,库在Editor安装路径\Editor\Data\MonoBleedingEdge\lib\mono\2.0-api中;如果是.NET 4.x,库在Editor安装路径\Editor\Data\MonoBleedingEdge\lib\mono\4.5中。

笔者这里使用的是4.5的库,如果还不行可以多试试几个不同版本的库

2.复制缺失的dll文件
在库文件夹中找到I18N.*文件

将这些dll文件↑ 复制到可执行程序根目录↓

如果这个不行,将库文件夹下的System.Drawing.Dll也复制进来

环境:Aspose.Words, Version=18.7.0.0

​ Unity: Unity2019.4.0f1

​ windows 10

借鉴

本文标签: Unity在Unity中使用AposeWord报“The document appears to be corrupted and cannot be loaded