27 public partial class WorkbookReader : IPluginBaseReader
29 private Stream stream;
30 private IPasswordReader passwordReader;
65 public void Init(Stream stream,
Workbook workbook, IOptions readerOptions, Action<Stream, Workbook, string, IOptions, int?> inlinePluginHandler)
68 this.Workbook = workbook;
69 this.Options = readerOptions;
70 this.InlinePluginHandler = inlinePluginHandler;
71 this.passwordReader = PlugInLoader.GetPlugIn<IPasswordReader>(PlugInUUID.PasswordReader,
new LegacyPasswordReader());
72 this.passwordReader.Init(PasswordType.WorkbookProtection, (ReaderOptions)readerOptions);
85 using (XmlReader reader = XmlReader.Create(stream, XmlStreamUtils.CreateSettings()))
89 if (reader.NodeType != XmlNodeType.Element)
93 if (XmlStreamUtils.IsElement(reader,
"sheets"))
95 GetWorksheetInformation(reader);
97 else if (XmlStreamUtils.IsElement(reader,
"bookViews"))
99 GetViewInformation(reader);
101 else if (XmlStreamUtils.IsElement(reader,
"workbookProtection"))
103 GetProtectionInformation(reader);
110 catch (NotSupportedContentException)
116 throw new IOException(
"The XML entry could not be read from the input stream. Please see the inner exception:", ex);
126 private void GetProtectionInformation(XmlReader reader)
128 bool lockStructure =
false;
129 bool lockWindows =
false;
130 string attribute = reader.GetAttribute(
"lockWindows");
131 if (attribute !=
null)
133 lockWindows = ParserUtils.ParseBinaryBool(attribute) == 1;
135 attribute = reader.GetAttribute(
"lockStructure");
136 if (attribute !=
null)
138 lockStructure = ParserUtils.ParseBinaryBool(attribute) == 1;
140 Workbook.SetWorkbookProtection(
true, lockWindows, lockStructure,
null);
142 using (XmlReader subtree = reader.ReadSubtree())
144 subtree.MoveToContent();
145 outerXml = subtree.ReadOuterXml();
147 XmlDocument miniDoc =
new XmlDocument { XmlResolver =
null };
148 miniDoc.LoadXml(outerXml);
149 passwordReader.ReadXmlAttributes(miniDoc.DocumentElement);
150 if (passwordReader.PasswordIsSet())
152 Workbook.WorkbookProtectionPassword.CopyFrom(passwordReader);
160 private void GetViewInformation(XmlReader reader)
162 using (XmlReader subtree = reader.ReadSubtree())
165 while (subtree.Read())
167 if (!XmlStreamUtils.IsElement(subtree,
"workbookView"))
171 string attribute = subtree.GetAttribute(
"visibility");
172 if (attribute !=
null && ParserUtils.ToLower(attribute) ==
"hidden")
174 Workbook.Hidden =
true;
176 attribute = subtree.GetAttribute(
"activeTab");
177 if (!
string.IsNullOrEmpty(attribute))
179 Workbook.AuxiliaryData.SetData(PlugInUUID.WorkbookReader, PlugInUUID.SelectedWorksheetEntity, ParserUtils.ParseInt(attribute));
189 private void GetWorksheetInformation(XmlReader reader)
191 int visibleWorksheetOrder = 0;
192 using (XmlReader subtree = reader.ReadSubtree())
195 while (subtree.Read())
197 if (!XmlStreamUtils.IsElement(subtree,
"sheet"))
203 string sheetName = subtree.GetAttribute(
"name") ??
"worksheet1";
204 int id = ParserUtils.ParseInt(subtree.GetAttribute(
"sheetId"));
205 string relId = subtree.GetAttribute(
"r:id");
206 string state = subtree.GetAttribute(
"state");
207 bool hidden = state !=
null && ParserUtils.ToLower(state) ==
"hidden";
208 WorksheetDefinition definition =
new WorksheetDefinition(
id, sheetName, relId)
212 Workbook.AuxiliaryData.SetData(PlugInUUID.WorkbookReader, PlugInUUID.WorksheetDefinitionEntity, visibleWorksheetOrder, definition);
213 visibleWorksheetOrder++;
217 throw new IOException(
"The workbook information could not be resolved. Please see the inner exception:", e);
void Init(Stream stream, Workbook workbook, IOptions readerOptions, Action< Stream, Workbook, string, IOptions, int?> inlinePluginHandler)
Initialization method (interface implementation).