26 public partial class WorkbookReader : IPluginBaseReader
28 private MemoryStream stream;
29 private IPasswordReader passwordReader;
64 public void Init(MemoryStream stream,
Workbook workbook, IOptions readerOptions, Action<MemoryStream, Workbook, string, IOptions, int?> inlinePluginHandler)
67 this.Workbook = workbook;
68 this.Options = readerOptions;
69 this.InlinePluginHandler = inlinePluginHandler;
70 this.passwordReader = PlugInLoader.GetPlugIn<IPasswordReader>(PlugInUUID.PasswordReader,
new LegacyPasswordReader());
71 this.passwordReader.Init(PasswordType.WorkbookProtection, (ReaderOptions)readerOptions);
84 XmlDocument xr =
new XmlDocument() { XmlResolver =
null };
85 using (XmlReader reader = XmlReader.Create(stream,
new XmlReaderSettings() { XmlResolver =
null }))
88 foreach (XmlNode node
in xr.DocumentElement.ChildNodes)
90 if (node.LocalName.Equals(
"sheets", StringComparison.OrdinalIgnoreCase) && node.HasChildNodes)
92 GetWorksheetInformation(node.ChildNodes);
94 else if (node.LocalName.Equals(
"bookViews", StringComparison.OrdinalIgnoreCase) && node.HasChildNodes)
96 GetViewInformation(node.ChildNodes);
98 else if (node.LocalName.Equals(
"workbookProtection", StringComparison.OrdinalIgnoreCase))
100 GetProtectionInformation(node);
107 catch (NotSupportedContentException)
113 throw new IOException(
"The XML entry could not be read from the input stream. Please see the inner exception:", ex);
121 private void GetProtectionInformation(XmlNode node)
123 bool lockStructure =
false;
124 bool lockWindows =
false;
127 if (attribute !=
null)
129 int value = ParserUtils.ParseBinaryBool(attribute);
131 lockWindows = value == 1;
134 if (attribute !=
null)
136 int value = ParserUtils.ParseBinaryBool(attribute);
138 lockStructure = value == 1;
140 Workbook.SetWorkbookProtection(
true, lockWindows, lockStructure,
null);
141 passwordReader.ReadXmlAttributes(node);
142 if (passwordReader.PasswordIsSet())
144 Workbook.WorkbookProtectionPassword.CopyFrom(passwordReader);
152 private void GetViewInformation(XmlNodeList nodes)
154 foreach (XmlNode node
in nodes)
156 if (node.LocalName.Equals(
"workbookView", StringComparison.OrdinalIgnoreCase))
158 string attribute = ReaderUtils.GetAttribute(node,
"visibility");
159 if (attribute !=
null && ParserUtils.ToLower(attribute) ==
"hidden")
161 this.Workbook.Hidden =
true;
163 attribute = ReaderUtils.GetAttribute(node,
"activeTab");
164 if (!
string.IsNullOrEmpty(attribute))
166 Workbook.AuxiliaryData.SetData(PlugInUUID.WorkbookReader, PlugInUUID.SelectedWorksheetEntity, ParserUtils.ParseInt(attribute));
176 private void GetWorksheetInformation(XmlNodeList nodes)
178 foreach (XmlNode node
in nodes)
180 if (node.LocalName.Equals(
"sheet", StringComparison.OrdinalIgnoreCase))
184 string sheetName = ReaderUtils.GetAttribute(node,
"name",
"worksheet1");
185 int id = ParserUtils.ParseInt(ReaderUtils.GetAttribute(node,
"sheetId"));
186 string relId = ReaderUtils.GetAttribute(node,
"r:id");
187 string state = ReaderUtils.GetAttribute(node,
"state");
189 if (state !=
null && ParserUtils.ToLower(state) ==
"hidden")
193 WorksheetDefinition definition =
new WorksheetDefinition(
id, sheetName, relId)
197 Workbook.AuxiliaryData.SetData(PlugInUUID.WorkbookReader, PlugInUUID.WorksheetDefinitionEntity,
id, definition);
201 throw new IOException(
"The workbook information could not be resolved. Please see the inner exception:", e);
Action< MemoryStream, Workbook, string, IOptions, int?> InlinePluginHandler
Reference to the ReaderPlugInHandler, to be used for post operations in the Execute method.
void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions, Action< MemoryStream, Workbook, string, IOptions, int?> inlinePluginHandler)
Initialization method (interface implementation).