private void GetXlsXSheetsProtection(ZipEntryFinder zipEntryFinder, Stream stream) { var openXmlFormat = OpenXmlFormatIdentifier.Resolve(stream); int sheetIndex = 1; string sheetPath = string.Format(@"xl\worksheets\sheet{0}.xml", sheetIndex); while (zipEntryFinder.HasEntry(sheetPath)) { ZipEntry zipEntry = zipEntryFinder.GetEntry(sheetPath); if (zipEntry == null) break; var zipFile = new ZipFile(stream) { IsStreamOwner = false }; XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; CommonNamespaces commonNamespaces = new CommonNamespaces(new NameTable(), openXmlFormat); settings.NameTable = commonNamespaces.NameTable; XmlReader reader = XmlReader.Create(zipFile.GetInputStream(zipEntry), settings); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name.ToLowerInvariant() == "sheetprotection") { m_fileData.DocumentProtected = true; } } } sheetPath = string.Format(@"xl\worksheets\sheet{0}.xml", ++sheetIndex); } }
private bool InitialiserMethodForXlsXFile() { if (!IsZipFile()) { return Workshare.Interop.Options.OptionApi.GetBool("UseDiskBasedFiles") ? InitialiseMethodForEncryptedXlsxFileOnDisk() : InitialiseMethodForEncryptedXlsxFile(); } using (Stream stream = m_fileData.BinaryFileData.AsStream()) using (ZipEntryFinder zipEntryFinder = new ZipEntryFinder(stream, false)) { if (!zipEntryFinder.HasEntry("[content_types].xml")) return false; ZipEntry zipEntry = zipEntryFinder.GetEntry(@"xl\workbook.xml"); if (zipEntry == null) return false; //Method to look at the XML within the document and determine the correct file type. //If we don't find a file type, then we assume that the type is ExcelSheetX FileType type = DetermineFileTypeForOfficeXMLFiles(zipEntryFinder, stream); switch (type) { case FileType.Unknown: type = FileType.ExcelSheetX; break; //Office 2003 compatibility packs don't support 2007 template types...so we don't either. case FileType.ExcelSheetTemplateX: case FileType.ExcelSheetMacroTemplateX: if (OfficeApplicationVersion.IsWord2003()) { type = FileType.Unknown; } break; } m_fileData.FileType = type; GetXlsXWriteProtection(zipEntry, stream); GetXlsXSheetsProtection(zipEntryFinder, stream); m_impl = new FileImpl(m_fileData); Logger.LogInfo(".xlsx;.xlsm;.xltx;.xltm file detected"); return true; } }