CultureNotFoundException while reading XLS files

Hi,

We’re using Aspose.Cells 19.11.0 for .NET in a .NET Core 3.1 application. When reading any XLS file, a CultureNotFoundException is thrown with the following message:

Culture is not supported. (Parameter 'culture')
1043 (0x0413) is an invalid culture identifier.

Here are the exception details:

System.Globalization.CultureNotFoundException
  HResult=0x80070057
  Message=Culture is not supported. 

  Source=System.Private.CoreLib
  StackTrace:
   at System.Globalization.CultureData.GetCultureData(Int32 culture, Boolean bUseUserOverride)
   at System.Globalization.CultureInfo..ctor(Int32 culture, Boolean useUserOverride)
   at  ​.(Int16 )
   at  ​.(CountryCode )
   at  ​.(CountryCode )
   at Aspose.Cells.WorkbookSettings.set_Region(CountryCode value)
   at    . (    )
   at    . (    )
   at    .     (Stream )
   at    .(    , Stream )
   at    .(    , Stream )
   at   .(    , String )
   at   .()
   at   .(String , Stream , LoadOptions )
   at Aspose.Cells.Workbook.(String , LoadOptions )
   at Aspose.Cells.Workbook..ctor(String file)
   at Tools.IO.ExcelConverter.ConvertToXlsxStream(String fullPath)

Interestingly this exception is not thrown if we directly read an XLS file via a .NET Core console application. But in our .NET Core Web API project, this exception occurs. We tried to set the current culture to invariant or ‘en-US’ in many different ways, but nothing solves this error.

Any ideas how to fix this? So that we can use Aspose.Cells in our API project?

@infotron,
I have tried the scenario by creating a .NET Core 3.1 based Web API and opened a sample XLS/XLSX file in it. No issue is observed and Excel file is opened successfully. Could you please give a try to the attached sample code and share the feedback. If your issue is not resolved, please create a bare minimum solution and share with us along with the template XLS/XLSX file for our analysis. We will test the solution here and share our feedback. You may share your environment details as well.

I have used VS 2019 for macOS and Aspose.Cells for .NET 19.12 installed from NuGet package manager.

TestAsposeCellsInWebApi.zip (39.2 KB)

@infotron,
We have investigated bit more, for this trouble, the most possible reason should be that the environment does not support some CultureInfo specified by some number formatting in the template file. Please try the simple code like below to check whether it will fail with the same exception:

new CultureInfo("nl-NL", true);

Or

new CultureInfo(0x0413, true)

For such kind of issue, commonly it needs you to change the environment to support all required CultureInfos. If you cannot change their environment but does need to process the file without such kind of exception, we may provide fix with some kind of solution to handle it.

Let us know your feedback.

We created a bare minimum solution that reproduces this exception. This example is created with VS 2019 16.4 on Windows 10. Aspose.Cells for .NET 19.12 is used from NuGet.

TestAsposeCellsInWebApi.zip (15.8 KB)

While creating this inidividual project, we traced down the source of the exception. In the .csproj of the project the following setting is configured:

<ItemGroup>
  <RuntimeHostConfigurationOption Include="System.Globalization.Invariant" Value="true" />
</ItemGroup>

Removing this setting resolves the exception. So the conclusion is that reading XLS files with Aspose.Cells does not work when .NET Core Globalization Invariant Mode is enabled (new in .NET Core 2).

For now we can resolve this by turning the setting off in our API project. But it is interesting for Aspose.Cells, as other .NET Core projects can also have this setting enabled.

Thank you for your quick response!

@infotron,
I have tried your project and observed the exception. I tried to remove the settings mentioned by you but still, the exception is there. Could you please share another sample project where this setting is removed and the exception is also removed? It will help us to analyze the scenario and provide our feedback.

Secondly, I created different types of ASP.NET Core app projects in Windows 7, VS 2019 but could not observe the issue. Please share which type of exact project is created by you using VS 2019 for our reference.

@infotron,
We have logged the issue in our database for further investigation and for a fix. Once, we will have some news for you, we will update you in this topic.

This issue has been logged as

CELLSNETCORE-47 – CultureNotFoundException while reading XLS file in .NET Core 3.1 applications

To reproduce: open the TestAsposeCellsInWebApi solution, build and run the project. The ‘Culture not found’ exception should occur. Then remove the System.Globalization.Invariant setting from the .csproj, clean the solution (important!), rebuild and run the project. The exception should not occur.

The project was created in Windows 10 with VS 2019 16.4. We created a ASP.NET Core Web Application, and selected the ‘Empty project’ template for .NET Core 3.1. Then we added the code to create a simple Web API, as we shared in the attached example solution.

According to the .NET Core Globalization Invariant Mode documentation, I would theoretically expect this error to occur in every .NET Core 2 project or higher that has the globalization invariant mode enabled. Note that it may depend on the XLS file you’re reading, the provided sample XLS is created on a machine with “nl-NL” culture.

Thanks for logging this issue!

@infotron,
Thank you for sharing the information. We have logged it with the ticket for our later reference. We will write back here once any feedback is ready to share.

@infotron

We have tested and found Asp.NetCore does not support some of the culture id, for example:

CultureInfo cl = new CultureInfo(1043 & 0xFFFF, true);

This code will return a “nl-NL” culture in .NETCore, but will get an error in Asp.NetCore.
To solve this problem we have provided a solution in Aspose.Cells for .NET v20.1 (latest version).

//First, declare a class in your code:
class CustomImp : Aspose.Cells.CustomImplementationFactory {
public override System.Globalization.CultureInfo CreateCultureInfo(int lcid) {
try {
return new CultureInfo(lcid);
}
catch {
//You may modify this, implemment your culture here when exception occurs in asp.netcore
return CultureInfo.CurrentCulture;
}
}
}
//do the CustomImplementationFactory assignment before you open a workbook
Aspose.Cells.CellsHelper.CustomImplementationFactory = new CustomImp();

Please declare a class like this and do the CustomImplementationFactory assignment before you open a workbook. You may get Aspose.Cells 20.1 version from nuget repos here, We also provided an example in the test project: TestAsposeCellsInWebApi.