Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Commented Task: bug with localized conditional formatting [9361]

$
0
0
I have encountered an issue with conditional formatting when my PC's region and language settings are set to something other than English (specifically, French or Italian). When I create my spreadsheet and add a conditional format, in the <conditionalFormatting> section of sheet.xml, the <x:formula> field is storing the value with the CurrentCulture decimal separator (i.e. ',') instead of InvariantCulture (i.e. '.'). When the spreadsheet is opened by Excel I get an error saying Excel found unreadable content. If I click Yes to recover, I get the following message from the recovery report:

Removed Feature: Conditional formatting from /xl/worksheets/sheet.xml part

I have tried changing the CurrentCulture to "en" just before adding a conditional format (and restoring after) but then the <x:formula> field is stored as a string (although with the correct decimal separator) which gives undesired results in Excel.

Please Advise,
James Martin

Comments: My application reads in a binary data file, parses the data into a class, and then exports the data to an excel spreadsheet. (it does much more but that is a simplified birds eye view). I have an old demo app that I used to use for testing but I will need some time to update it to show this issue. In the meantime, I am attaching four .xlsx files that used the exact same input data to create. All were created using the latest 0.74.0.0 version of the ClosedXml.dll except for one using the patched version provided. The first file was created with my PC Region and Language settings set to English (United States) and the rest with PC Region and Language settings (Format and Current Location) set to French (France). Before you replied with the patch, I had developed a workaround that worked for me. Here is that method: ``` internal virtual void UpdateConditionalFormatting(String xlfile) { try { using (SpreadsheetDocument ssd = SpreadsheetDocument.Open(xlfile, true)) { WorkbookPart wbPart = ssd.WorkbookPart; Sheets sheets = wbPart.Workbook.Sheets; foreach (Sheet sheet in sheets) { WorksheetPart wsPart = (WorksheetPart)wbPart.GetPartById(sheet.Id); foreach (var cf in wsPart.Worksheet.Descendants<ConditionalFormatting>()) { foreach (var cfr in cf.Descendants<ConditionalFormattingRule>().Where(x => x.Type == "cellIs")) { foreach (var formula in cfr.Descendants<Formula>()) { formula.Text = formula.Text.Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, "."); } } } wsPart.Worksheet.Save(); } } } catch (Exception ex) { Debug.Write("Failed during UpdateConditionalFormatting(): " + ex.Message); throw; } } ``` Regards, Theon

Viewing all articles
Browse latest Browse all 1877

Trending Articles