Aspose.Words.FileCorruptedException: The document appears to be corrupted and cannot be loaded

Posted 棉晗榜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Aspose.Words.FileCorruptedException: The document appears to be corrupted and cannot be loaded相关的知识,希望对你有一定的参考价值。

开发环境

  • C#
  • .NET 6
  • 控制台程序

异常消息:

Unhandled exception. Aspose.Words.FileCorruptedException: The document appears to be corrupted and cannot be loaded.
—> System.NullReferenceException: Object reference not set to an instance of an object.
at ??..ctor(Stream , Encoding )
at ???.(Stream , Encoding )
at Aspose.Words.Document.(Stream , LoadOptions )
— End of inner exception stack trace —
at Aspose.Words.Document.(Stream , LoadOptions )
at Aspose.Words.Document.(Stream , LoadOptions )
at Aspose.Words.Document.()
at Aspose.Words.Document…ctor(Boolean )
at Aspose.Words.Document…ctor()
at Program.$(String[] args) in D:\\WorkTest\\ConsoleAppWordTohtml\\ConsoleAppNET6.0\\Program.cs:line 9

解决办法:

多半是程序字符集问题,因为用的.net core 6,所以没有初始化字符集
使用如下代码注册字符集:

//注册编码提供程序
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

完整代码:

// See https://aka.ms/new-console-template for more information
using Aspose.Words;
using ConsoleAppNET6._0;
using System;
using System.Data;

Console.WriteLine("Hello, World!");
 
//注册编码提供程序
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

//测试生成饼图
Aspose.Words.Document doc = new Aspose.Words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);

以上是关于Aspose.Words.FileCorruptedException: The document appears to be corrupted and cannot be loaded的主要内容,如果未能解决你的问题,请参考以下文章