Skip to content

Commit

Permalink
Merge pull request #1 from Michael-Greiner/master
Browse files Browse the repository at this point in the history
Fixed problem with namespace prefix for sheetData tag in worksheet.
  • Loading branch information
TheXDS authored Jan 30, 2020
2 parents 4938e05 + 1ff6e5d commit fe2a890
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions EPPlus/ExcelWorksheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ private void CreateXml()
stream.Dispose();
packPart.Stream = new MemoryStream();

//first char is invalid sometimes??
//first char is invalid sometimes?? Probably Byte Order Mark (BOM)
if (xml[0] != '<')
LoadXmlSafe(_worksheetXml, xml.Substring(1, xml.Length - 1), encoding);
else
Expand Down Expand Up @@ -1024,7 +1024,7 @@ private string GetWorkSheetXml(Stream stream, long start, long end, out Encoding
length += size;
}
while (length < start + 20 && length < end); //the start-pos contains the stream position of the sheetData element. Add 20 (with some safty for whitespace, streampointer diff etc, just so be sure).
startmMatch = Regex.Match(sb.ToString(), string.Format("(<[^>]*{0}[^>]*>)", "sheetData"));
startmMatch = Regex.Match(sb.ToString(), string.Format("<(?:([^:]+):)?[^>]*{0}[^>]*>", "sheetData")); // (?:xxx) means non capturing group, i.e. use parentheses for logical formatting, but do not add to found match groups
if (!startmMatch.Success) //Not found
{
encoding = sr.CurrentEncoding;
Expand Down Expand Up @@ -1072,6 +1072,15 @@ private string GetWorkSheetXml(Stream stream, long start, long end, out Encoding
}

encoding = sr.CurrentEncoding;
if (startmMatch.Groups[1].Success) // Default namespace (http://schemas.openxmlformats.org/spreadsheetml/2006/main) for worksheet has prefix. Just remove the prefix.
{
if (Regex.Matches(xml, ".<[^\\s/>:]+[\\s/>]").Count > 1) // Make sure we do not assimilate other tags without prefixes in the namespace. The only found tags should be the above inserted "<sheetData/>". The leading dot assures, we do not match the "<?xml".
{
throw new NotSupportedException("Default namespace for worksheet has prefix, but there are tag names in the worksheet that do not have prefixes. Sheet name: " + Name);
}
string namespacePrefix = startmMatch.Groups[1].Value;
xml = xml.Replace("</" + namespacePrefix + ":", "</").Replace("<" + namespacePrefix + ":", "<").Replace("xmlns:" + namespacePrefix + "=", "xmlns=");
}
return xml;
}
}
Expand Down

0 comments on commit fe2a890

Please sign in to comment.