Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text like numeric incorrectly (differently) handled. #47

Closed
WesolyPierozek opened this issue Jan 2, 2015 · 3 comments
Closed

Text like numeric incorrectly (differently) handled. #47

WesolyPierozek opened this issue Jan 2, 2015 · 3 comments

Comments

@WesolyPierozek
Copy link

I have in excel cell value "0682".

ExcelOpenXmlReader in function ReadSheetRow converts value to double on beginning. Is it correct approach? I though value returned by reader should be same as visible in excel workbook. For solving that issue i changed my code a bit:

if (_xmlReader.NodeType == XmlNodeType.Text && hasValue)
                    {
                        object o = _xmlReader.Value;

                        var style = NumberStyles.Any;
                        var culture = CultureInfo.InvariantCulture;

                        bool is_string = false;

                        if (null != a_t && a_t == XlsxWorksheet.N_inlineStr)
                        {
                            o = Helpers.ConvertEscapeChars(o.ToString());
                            if (_xmlReader.XmlSpace == XmlSpace.Preserve)
                            {
                                is_string = true;
                            }
                        }
                        else if (null != a_t && a_t == XlsxWorksheet.A_s) //if string
                        {
                            o = Helpers.ConvertEscapeChars(_workbook.SST[int.Parse(o.ToString())]);
                            is_string = true;
                        } // Requested change 4: missing (it appears that if should be else if)                        
                        else if (a_t == "b") //boolean
                        {
                            o = _xmlReader.Value == "1";                            
                        }

                        if (!is_string && a_s != null && !string.IsNullOrEmpty(o.ToString()))
                        {
                            XlsxXf xf = _workbook.Styles.CellXfs[int.Parse(a_s)];
                            double number;
                            long l_number;
                            int i_number;
                            if (IsDateTimeStyle(xf.NumFmtId) && double.TryParse(o.ToString(), out number))
                            {
                                o = Helpers.ConvertFromOATime(number);
                            }
                            else if (!xf.ApplyNumberFormat || xf.NumFmtId == 49)
                            {
                                o = o.ToString();
                            }
                            else if (int.TryParse(o.ToString(), NumberStyles.Integer, culture, out i_number))
                            {
                                o = i_number;
                            }
                            else if (long.TryParse(o.ToString(), NumberStyles.Integer, culture, out l_number))
                            {
                                o = l_number;
                            }
                            else if (double.TryParse(o.ToString(), NumberStyles.Any, culture ,out number))
                            {
                                o = number;
                            }                            
                        }



                        if (col - 1 < _cellsValues.Length)
                            _cellsValues[col - 1] = o;
                    }
@q-key
Copy link

q-key commented Sep 3, 2015

I got the same issue.
< c r="C7" s="21" t="str" > < f >' Sheet'!C23 < / f>< v >00011</ v ></ c ></ row >

It reads it as double: 11.0 wtf?

@scottdorman
Copy link

I haven't done a lot of testing yet, but it looks like the code change above will work and cause numbers stored as text to be treated as text and not numbers.

@andersnm
Copy link
Collaborator

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants