27 public partial class WorkbookReader : IDocumentReader
29 private Stream stream;
30 private IPasswordReader passwordReader;
37 public virtual string DocumentType {
get {
return @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"; } }
70 public void Init(Stream stream,
Workbook workbook, IOptions readerOptions, Action<Stream, Workbook, string, IOptions, int?> inlinePluginHandler)
73 this.Workbook = workbook;
74 this.Options = readerOptions;
75 this.InlinePluginHandler = inlinePluginHandler;
76 this.passwordReader = PlugInLoader.GetPlugIn<IPasswordReader>(PlugInUUID.PasswordReader,
new LegacyPasswordReader());
77 this.passwordReader.Init(PasswordType.WorkbookProtection, (ReaderOptions)readerOptions);
90 using (XmlReader reader = XmlReader.Create(stream, XmlStreamUtils.CreateSettings()))
94 if (reader.NodeType != XmlNodeType.Element)
98 if (XmlStreamUtils.IsElement(reader,
"sheets"))
100 GetWorksheetInformation(reader);
102 else if (XmlStreamUtils.IsElement(reader,
"bookViews"))
104 GetViewInformation(reader);
106 else if (XmlStreamUtils.IsElement(reader,
"workbookProtection"))
108 GetProtectionInformation(reader);
110 else if (XmlStreamUtils.IsElement(reader,
"definedNames"))
112 GetDefinedNamesInformation(reader);
119 catch (NotSupportedContentException)
125 throw new IOException(
"The XML entry could not be read from the input stream. Please see the inner exception:", ex);
135 private void GetProtectionInformation(XmlReader reader)
137 bool lockStructure =
false;
138 bool lockWindows =
false;
139 string attribute = reader.GetAttribute(
"lockWindows");
140 if (attribute !=
null)
142 lockWindows = ParserUtils.ParseBinaryBool(attribute) == 1;
144 attribute = reader.GetAttribute(
"lockStructure");
145 if (attribute !=
null)
147 lockStructure = ParserUtils.ParseBinaryBool(attribute) == 1;
149 Workbook.SetWorkbookProtection(
true, lockWindows, lockStructure,
null);
151 using (XmlReader subtree = reader.ReadSubtree())
153 subtree.MoveToContent();
154 outerXml = subtree.ReadOuterXml();
156 XmlDocument miniDoc =
new XmlDocument { XmlResolver =
null };
157 miniDoc.LoadXml(outerXml);
158 passwordReader.ReadXmlAttributes(miniDoc.DocumentElement);
159 if (passwordReader.PasswordIsSet())
161 Workbook.WorkbookProtectionPassword.CopyFrom(passwordReader);
171 private void GetDefinedNamesInformation(XmlReader reader)
174 using (XmlReader subtree = reader.ReadSubtree())
177 while (subtree.Read())
179 if (!XmlStreamUtils.IsElement(subtree,
"definedName"))
183 DefinedNameDefinition definition =
new DefinedNameDefinition
185 Name = subtree.GetAttribute(
"name"),
186 Comment = subtree.GetAttribute(
"comment")
188 string localSheetId = subtree.GetAttribute(
"localSheetId");
189 if (!
string.IsNullOrEmpty(localSheetId))
191 definition.LocalSheetId = ParserUtils.ParseInt(localSheetId);
193 definition.Reference = ReadDefinedNameReference(subtree);
194 Workbook.AuxiliaryData.SetData(PlugInUUID.WorkbookReader, PlugInUUID.DefinedNameEntity, index, definition);
207 internal static string ReadDefinedNameReference(XmlReader reader)
209 if (reader.IsEmptyElement)
213 System.Text.StringBuilder sb =
new System.Text.StringBuilder();
214 while (reader.Read())
216 if (reader.NodeType == XmlNodeType.EndElement)
220 if (reader.NodeType == XmlNodeType.Text || reader.NodeType == XmlNodeType.CDATA || reader.NodeType == XmlNodeType.SignificantWhitespace)
222 sb.Append(reader.Value);
225 return sb.ToString();
232 private void GetViewInformation(XmlReader reader)
234 using (XmlReader subtree = reader.ReadSubtree())
237 while (subtree.Read())
239 if (!XmlStreamUtils.IsElement(subtree,
"workbookView"))
243 string attribute = subtree.GetAttribute(
"visibility");
244 if (attribute !=
null && ParserUtils.ToLower(attribute) ==
"hidden")
246 Workbook.Hidden =
true;
248 attribute = subtree.GetAttribute(
"activeTab");
249 if (!
string.IsNullOrEmpty(attribute))
251 Workbook.AuxiliaryData.SetData(PlugInUUID.WorkbookReader, PlugInUUID.SelectedWorksheetEntity, ParserUtils.ParseInt(attribute));
261 private void GetWorksheetInformation(XmlReader reader)
263 int visibleWorksheetOrder = 0;
264 using (XmlReader subtree = reader.ReadSubtree())
267 while (subtree.Read())
269 if (!XmlStreamUtils.IsElement(subtree,
"sheet"))
275 string sheetName = subtree.GetAttribute(
"name") ??
"worksheet1";
276 int id = ParserUtils.ParseInt(subtree.GetAttribute(
"sheetId"));
277 string relId = subtree.GetAttribute(
"r:id");
278 string state = subtree.GetAttribute(
"state");
279 bool hidden = state !=
null && ParserUtils.ToLower(state) ==
"hidden";
280 WorksheetDefinition definition =
new WorksheetDefinition(
id, sheetName, relId)
284 Workbook.AuxiliaryData.SetData(PlugInUUID.WorkbookReader, PlugInUUID.WorksheetDefinitionEntity, visibleWorksheetOrder, definition);
285 visibleWorksheetOrder++;
289 throw new IOException(
"The workbook information could not be resolved. Please see the inner exception:", e);