Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Fixed problem with namespace prefix for sheetData tag in worksheet. #611

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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