【Unity】在Unity中使用Apose.Word报“The document appears to be corrupted and cannot be loaded.“的一个解决方案

在Unity里使用Apose.Word做文档处理时遇到一个问题,在Editor中代码跑得好好的,但是一打包成可执行程序,在构造Document实例时就会报错:

NullReferenceException: Object reference not set to an instance of an object
  at ​ ..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文件↑ 复制到可执行程序根目录↓
在这里插入图片描述
之后,错误消失,功能恢复正常。最后提供我的环境参数:

  • Windows 10 专业版 19043.1466
  • Unity 2020.3.1f1c1 Personal
  • Aspose 18.12

猜你喜欢

转载自blog.csdn.net/qq_37484084/article/details/126000485