I want to set the CountryCode of an Aspose.Cells Workbook dynamically using the IsoCode of the culture of the user and I haven’t found any method or helper to do that.
I’m going to create my own hardcoded mapping method between my current known codes, but I wouldn’t change this code for every new culture of the application.
Thanks in advance,
MPM
@mpmsoftware
You can use the following API to set Regional Settings of Workbook.
POST /cells/{name}/settings
https://apireference.aspose.cloud/cells/#!/CellsWorkbook/CellsWorkbook_PostWorkbookSettings
For instance, if you want to set Region
and LanguageCode
to Canada, set settings
parameter to the following JSON:
{
"Region": "Canada",
"LanguageCode": "Canada"
}
Please go through the following guides to know more about the Region
and CountryCode
property.
I would like to confirm are you using Aspose REST APIs or Native APIs? So that I can guide you accordingly.
Sorry, I’m using Aspose.Cells for .net, not the Cloud version (I didn’t realize that the forum was for Cloud on posting topic).
So what I want is a conversion between ISO country code (“US”, “ES”, “PT”, “CA”, …) to Aspose.Cells.CountryCode.
That’s the code I’m using now, that receives a standard culture code (language-country: “ca-ES”, “es-ES”, “es-MX”, “pt-PT”, “pt-BR”, “en-GB”, “en-US”, …) but I would use a standard function.
/// <summary>
/// Converts an string containing standard culture [language]-[country] on [ISO 639-1]-[ISO 3166-2]
/// to Aspose.Cells.CountryCode
/// </summary>
/// <param name="isoCode"></param>
/// <returns></returns>
public static CountryCode IsoCodeToAsposeCountryCode(string isoCode)
{
try
{
var ci = new CultureInfo(isoCode);
if (ci != null)
{
var ri = new RegionInfo(ci.LCID);
string countryCode = ri.TwoLetterISORegionName;
if ("ES".Equals(countryCode, StringComparison.InvariantCultureIgnoreCase))
{
return CountryCode.Spain;
}
else if ("GB".Equals(countryCode, StringComparison.InvariantCultureIgnoreCase))
{
return CountryCode.UnitedKingdom;
}
else if ("US".Equals(countryCode, StringComparison.InvariantCultureIgnoreCase))
{
return CountryCode.USA;
}
else if ("PT".Equals(countryCode, StringComparison.InvariantCultureIgnoreCase))
{
return CountryCode.Portugal;
}
else if ("MX".Equals(countryCode, StringComparison.InvariantCultureIgnoreCase))
{
return CountryCode.Mexico;
}
else
{
Log.ErrorFormat("Country {0} not defined.", countryCode);
}
}
}
catch (Exception cnfe)
{
Log.Error(string.Format("Error converting culture '{0}' to Aspose.CountryCode.", isoCode), cnfe);
}
return CountryCode.Spain;
}
@mpmsoftware
There is no such standard function and your function looks great. Aspose.Cells Country Codes are just integer (enum) values. You have to map them to your desired values yourself.
Please see this link to find the enum and their corresponding integer values.
The following sample code converts the Country Code Brazil to integer and prints integer value and then it converts the integer value to enum Brazil and prints it.
C#
//Convert enum to integer
int ccode = (int)CountryCode.Brazil;
//Print integer value
Console.WriteLine(ccode);
//Convert integer to enum
var o = (CountryCode)ccode;
//Print enum value
Console.WriteLine(o);
Console Output
55
Brazil
Please also see this screenshot.
Ok, thanks. I’ll use my own function then.