@@ -48,6 +48,11 @@ public sealed class XlsxCursor : ITabularCursor
4848 private const string StrictRelationshipNamespace =
4949 "http://purl.oclc.org/ooxml/officeDocument/relationships" ;
5050
51+ /// <summary>SpreadsheetML's own namespace, transitional and strict — identifiers, never fetched.</summary>
52+ private const string SpreadsheetNamespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main" ;
53+
54+ private const string StrictSpreadsheetNamespace = "http://purl.oclc.org/ooxml/spreadsheetml/main" ;
55+
5156 private readonly ZipArchive _package ;
5257
5358 /// <summary>
@@ -212,6 +217,10 @@ private static string Resolve(string folder, string target)
212217 private static string ? PathOfType ( Dictionary < string , Relationship > relationships , string typeSuffix ) =>
213218 relationships . Values . FirstOrDefault ( r => r . Type . EndsWith ( typeSuffix , StringComparison . Ordinal ) ) . Path ;
214219
220+ /// <summary>Whether a namespace is SpreadsheetML's own, transitional or strict.</summary>
221+ private static bool IsSpreadsheetNamespace ( string uri ) =>
222+ uri is SpreadsheetNamespace or StrictSpreadsheetNamespace ;
223+
215224 /// <summary>One relationship: its type, and the part it points at, resolved.</summary>
216225 private readonly record struct Relationship ( string Type , string Path ) ;
217226
@@ -522,6 +531,7 @@ private string ReadInlineString(SheetScanner scanner, CancellationToken cancella
522531
523532 _inlineText . Clear ( ) ;
524533 bool inText = false ;
534+ bool inPhonetic = false ; // inside <rPh>, whose text is a reading aid, not the value
525535 int sinceCheck = 0 ;
526536
527537 while ( scanner . Read ( ) )
@@ -541,10 +551,12 @@ private string ReadInlineString(SheetScanner scanner, CancellationToken cancella
541551
542552 case XmlNodeKind . EndElement :
543553 inText &= ! scanner . Name . SequenceEqual ( "t" ) ;
554+ inPhonetic &= ! scanner . Name . SequenceEqual ( "rPh" ) ;
544555 break ;
545556
546557 case XmlNodeKind . Element :
547- inText = scanner . Name . SequenceEqual ( "t" ) && ! scanner . IsEmptyElement ;
558+ inPhonetic |= scanner . Name . SequenceEqual ( "rPh" ) && ! scanner . IsEmptyElement ;
559+ inText = ! inPhonetic && scanner . Name . SequenceEqual ( "t" ) && ! scanner . IsEmptyElement ;
548560 break ;
549561
550562 case XmlNodeKind . Text when inText :
@@ -765,25 +777,50 @@ private static string ReadSharedStringItem(
765777 break ;
766778 }
767779
768- advance = true ;
769-
770780 if ( reader . NodeType == XmlNodeType . EndElement && reader . LocalName == "si" )
771781 {
772782 break ;
773783 }
774784
775- if ( reader . NodeType == XmlNodeType . Element && reader . LocalName == "t" )
776- {
785+ advance = reader . NodeType != XmlNodeType . Element
786+ || ReadItemElement ( reader , text , chunk , maxChars , cancellationToken ) ;
787+ }
788+
789+ return text . ToString ( ) ;
790+ }
791+
792+ /// <summary>
793+ /// Takes what one element inside <c><si></c> contributes, and says whether the loop should
794+ /// advance past where it leaves the reader.
795+ /// </summary>
796+ private static bool ReadItemElement (
797+ XmlReader reader ,
798+ StringBuilder text ,
799+ char [ ] chunk ,
800+ int maxChars ,
801+ CancellationToken cancellationToken )
802+ {
803+ switch ( reader . LocalName )
804+ {
805+ case "rPh" :
806+ // The phonetic guide — furigana over Japanese text — is a rendering aid, not part of
807+ // the value; its <t> would otherwise be appended to the text it annotates. Skip
808+ // leaves the reader on the node after it, which the loop must look at.
809+ reader . Skip ( ) ;
810+ return false ;
811+
812+ case "t" :
777813 // AppendText leaves the reader on the node after the element's content, which the
778814 // loop must look at rather than skip. An empty <t/> has no content: the reader has
779815 // not moved, and not advancing past it read the same element forever — any workbook
780816 // holding an empty shared string hung its first read.
781- advance = reader . IsEmptyElement ;
817+ bool empty = reader . IsEmptyElement ;
782818 AppendText ( reader , text , chunk , maxChars , cancellationToken ) ;
783- }
784- }
819+ return empty ;
785820
786- return text . ToString ( ) ;
821+ default :
822+ return true ;
823+ }
787824 }
788825
789826 /// <summary>
@@ -922,7 +959,10 @@ private Dictionary<string, Relationship> ReadRelationships(
922959 continue ;
923960 }
924961
925- if ( reader . LocalName == "workbookPr" )
962+ // The workbook's own properties, not an extension's: Excel 2013 and later also write
963+ // <x15:workbookPr chartTrackingRefBase="1"/>, and reading that one as well reset a
964+ // 1904 workbook to the 1900 epoch — every date four years and a day early.
965+ if ( reader . LocalName == "workbookPr" && IsSpreadsheetNamespace ( reader . NamespaceURI ) )
926966 {
927967 string ? value = reader . GetAttribute ( "date1904" ) ;
928968 date1904 = value is "1" or "true" ;
@@ -1123,8 +1163,12 @@ private static bool[] FlattenDateStyles(List<int> cellFormats, Dictionary<int, s
11231163 }
11241164
11251165 /// <summary>The number format identifiers the specification reserves for dates and times.</summary>
1166+ /// <remarks>
1167+ /// 14–22 and 45–47 everywhere, and 27–36 and 50–58 for the East Asian locales, which a workbook
1168+ /// saved by a Japanese, Chinese or Korean Excel uses without declaring a format code.
1169+ /// </remarks>
11261170 private static bool IsBuiltInDateFormat ( int numFmtId ) =>
1127- numFmtId is ( >= 14 and <= 22 ) or ( >= 45 and <= 47 ) ;
1171+ numFmtId is ( >= 14 and <= 22 ) or ( >= 27 and <= 36 ) or ( >= 45 and <= 47 ) or ( >= 50 and <= 58 ) ;
11281172
11291173 /// <summary>
11301174 /// Decides whether a custom format code renders a date, by looking for date tokens outside the
@@ -1223,7 +1267,10 @@ private static bool TryFromSerial(double serial, bool date1904, out DateTime dat
12231267 days = serial ;
12241268 }
12251269
1226- double ticks = days * TimeSpan . TicksPerDay ;
1270+ // Rounded to the millisecond, which is all a serial date carries. The fraction for 16:00 is
1271+ // stored a hair below its true value, and truncating the ticks read it as 15:59:59.999.
1272+ double ticks = Math . Round ( days * TimeSpan . TicksPerDay / TimeSpan . TicksPerMillisecond )
1273+ * TimeSpan . TicksPerMillisecond ;
12271274
12281275 if ( ticks < ( DateTime . MinValue - epoch ) . Ticks || ticks > ( DateTime . MaxValue - epoch ) . Ticks )
12291276 {
0 commit comments