【題外話】
這是2010年參加比賽時候做的研究,當時為了實現對Word、Excel、PowerPoint文件文字內容的抽取研究了很久,由於Java有POI庫,可以輕松的抽取各種Office文檔,而.NET雖然有移植的NPOI,但是只實現了最核心的Excel文件的讀寫,所以之后查了很多資料才實現了Word和PowerPoint文件文字的抽取。之后忙於各種事情一直沒時間整理,后來雖然想寫成文章但由於時間太久也記不清很多細節,現在重新查找資料並整理如下,希望對大家有用。
【系列索引】
- Office文件的奧秘——.NET平台下不借助Office實現Word、Powerpoint等文件的解析(一)
獲取Office二進制文檔的DocumentSummaryInformation以及SummaryInformation - Office文件的奧秘——.NET平台下不借助Office實現Word、Powerpoint等文件的解析(二)
獲取Word二進制文檔(.doc)的文字內容(包括正文、頁眉、頁腳、批注等等) - Office文件的奧秘——.NET平台下不借助Office實現Word、Powerpoint等文件的解析(三)
詳細介紹Office二進制文檔中的存儲結構,以及獲取PowerPoint二進制文檔(.ppt)的文字內容 - Office文件的奧秘——.NET平台下不借助Office實現Word、Powerpoint等文件的解析(完)
介紹Office Open XML文檔(.docx、.pptx)如何進行解析以及解析Office文件常見開源類庫
【文章索引】
- .NET下讀取Office文件的方式
- Windows復合二進制文件及其Header
- 我們從Directory開始
- DocumentSummaryInformation和SummaryInformation
- 相關鏈接
10年的時候參加比賽要做一個文件檢索的系統,要包含Word、PowerPoint等文件格式的全文檢索。由於之前用過.NET並且考慮到這些是微軟的格式,可能使用.NET讀取會更容易些,但沒想到.NET這邊查到的資料只有Interop的方式讀取Office文件。后來接觸了Java的POI,發現.NET也有移植的NPOI,但是只移植了核心的Excel讀寫,並沒有Word、PowerPoint等文件的讀寫,所以最后沒有辦法只能硬着頭皮自己去做Word和PowerPoint文件的解析。
那么Interop是什么?Interop的全稱是“Interoperability”,即微軟希望托管的.NET能與非托管的COM進行互相調用的一種方式。通過Interop讀寫Office即調用安裝在計算機上的Office軟件來實現Office的讀寫,其優點顯而易見,文件還是由Office生成或讀取的,所以與自己打開Office是沒有任何區別的;但缺點也非常明顯,即運行程序的計算機上必須安裝有對應版本的Office軟件,同時操作Office文件時實際上是打開了對應的Office組件,所以運行效率低、耗內存大並且還可能產生內存泄露的問題。關於Interop方式讀寫Office文件的例子網上有很多,有興趣的可以自行查閱,這里就不再多講了。
那么,有沒有方式不借助Office軟件實現Office文件的讀寫呢?答案肯定是肯定的,就像Java中的POI及.NET中的NPOI實現的那樣,即通過程序自己讀寫文件來實現Office文件的讀寫。不過由於Office文件結構非常復雜,這里只提供文件摘要信息和文件文本內容的解析。不過即使如此,對於全文檢索什么的還是足夠的。
前幾年,微軟開放了一些私有格式的規范,使得所有人都可以對其文件進行解析,而不需要支付任何費用,這也使得我們編寫解析文件的程序成為可能,相關鏈接在文章最后可以找到。對於一個Microsoft Office文件,其實質是一個Windows復合二進制文件(Windows Compound Binary File),文件的頭Header是固定的512字節,Header記錄文件最重要的參數。Header之后可以分為不同的Sector,Sector的種類有FAT、Mini-FAT(屬於Mini-Sector)、Directory、DIF、Stroage等五種。為了方便稱呼,我們規定每個Sector都有一個SectorID,Header后的Sector為第一個Sector,其SectorID為0。
我們先來說Header,一個Header的部分截圖及包含的信息如下,比較重要的用粗體表示。
- Header的前8字節Byte[],也就是整個文件的前8字節,都是固定的0xD0 0xCF 0x11 0xE0 0xA1 0xB1 0x1A 0xE1,如果不是則說明不是復合文件。
- 從008H到017H的16字節,是Class Id,不過很多文件都置的0。
- 從018H到019H的2字節UInt16,是文件格式的次要版本。
- 從01AH到01BH的2字節UInt16,是文件格式的主要版本。
- 從01CH到01DH的2字節UInt16,是固定為0xFE 0xFF,表示文檔使用的是Little Endian(低位在前,高位在后)。
- 從01EH到01FH的2字節UInt16,是Sector大小的冪,默認為9(0x09 0x00),即每個Sector為512字節。
- 從020H到021H的2字節UInt16,是Mini-Sector大小的冪,默認為6(0x06 0x00),即每個Mini-Sector為64字節。
- 從022H到023H的2字節UInt16,是預留的,必須置0。
- 從024H到027H的4字節UInt32,是預留的,必須置0。
- 從028H到02BH的4字節UInt32,是預留的,必須置0。
- 從02CH到02FH的4字節UInt32,是FAT的數量。
- 從030H到033H的4字節UInt32,是Directory開始的SectorID。
- 從034H到037H的4字節UInt32,是用於事務的,必須置0。
- 從038H到03BH的4字節UInt32,是最小串(Stream)的最大大小,默認為4096(0x00 0x10 0x00 0x10)。
- 從03CH到03FH的4字節UInt32,是MiniFAT表開始的SectorID。
- 從040H到043H的4字節UInt32,是MiniFAT表的數量。
- 從044H到047H的4字節UInt32,是DIFAT開始的SectorID。
- 從048H到04BH的4字節UInt32,是DIFAT的數量。
- 從04CH到1FFH的436字節UInt32[],是前109塊FAT表的SectorID。
那么我們可以寫如下的代碼將Header中重要的內容解析出來。

1 #region 字段 2 private FileStream m_stream; 3 private BinaryReader m_reader; 4 private Int64 m_length; 5 private DirectoryEntry m_dirRootEntry; 6 7 #region 頭部信息 8 private UInt32 m_sectorSize;//Sector大小 9 private UInt32 m_miniSectorSize;//Mini-Sector大小 10 private UInt32 m_fatCount;//FAT數量 11 private UInt32 m_dirStartSectorID;//Directory開始的SectorID 12 private UInt32 m_miniFatStartSectorID;//Mini-FAT開始的SectorID 13 private UInt32 m_miniFatCount;//Mini-FAT數量 14 private UInt32 m_difStartSectorID;//DIF開始的SectorID 15 private UInt32 m_difCount;//DIF數量 16 #endregion 17 #endregion 18 19 #region 讀取頭部信息 20 private void ReadHeader() 21 { 22 if (this.m_reader == null) 23 { 24 return; 25 } 26 27 //先判斷是否是Office文件格式 28 Byte[] sig = (this.m_length > 512 ? this.m_reader.ReadBytes(8) : null); 29 if (sig == null || 30 sig[0] != 0xD0 || sig[1] != 0xCF || sig[2] != 0x11 || sig[3] != 0xE0 || 31 sig[4] != 0xA1 || sig[5] != 0xB1 || sig[6] != 0x1A || sig[7] != 0xE1) 32 { 33 throw new Exception("該文件不是Office文件!"); 34 } 35 36 //讀取頭部信息 37 this.m_stream.Seek(22, SeekOrigin.Current); 38 this.m_sectorSize = (UInt32)Math.Pow(2, this.m_reader.ReadUInt16()); 39 this.m_miniSectorSize = (UInt32)Math.Pow(2, this.m_reader.ReadUInt16()); 40 41 this.m_stream.Seek(10, SeekOrigin.Current); 42 this.m_fatCount = this.m_reader.ReadUInt32(); 43 this.m_dirStartSectorID = this.m_reader.ReadUInt32(); 44 45 this.m_stream.Seek(8, SeekOrigin.Current); 46 this.m_miniFatStartSectorID = this.m_reader.ReadUInt32(); 47 this.m_miniFatCount = this.m_reader.ReadUInt32(); 48 this.m_difStartSectorID = this.m_reader.ReadUInt32(); 49 this.m_difCount = this.m_reader.ReadUInt32(); 50 } 51 #endregion
說個比較有意思的,.NET中的BinaryReader有很多讀取的方法,比如ReadUInt16、ReadInt32之類的,只有ReadUInt16的Summary寫着“使用 Little-Endian 編碼...”(見下圖),其實不僅僅是ReadUInt16,所有ReadIntX、ReadUIntX、ReadSingle、ReadDouble都是使用Little-Endian編碼方式從流中讀的,大家可以放心使用,而不需要一個字節一個字節的讀再反轉數組,我在10年的時候就走過彎路。解釋在MSDN各個方法中的備注里:http://msdn.microsoft.com/zh-cn/library/vstudio/system.io.binaryreader_methods.aspx
復合文檔中其實存放着很多內容,這么多內容需要有個目錄,那么Directory就是這個目錄。從Header中我們可以讀取出Directory開始的SectorID,我們可以Seek到這個位置(0x200 + sectorSize * dirStartSectorID)。Directory中每個DirectoryEntry固定為128字節,其主要結構如下:
- 從000H到040H的64字節,是存儲DirectoryEntry名稱的,並且是以Unicode存儲的,即每個字符占2個字節,其實可以看做是UInt16。
- 從041H到042H的2字節UInt16,是DirectoryEntry名稱的長度(包括最后的“\0”)。
- 從042H到042H的1字節Byte,是DirectoryEntry的類型。(主要的有:1為目錄,2為節點,5為根節點)
- 從044H到047H的4字節UInt32,是該DirectoryEntry左兄弟的EntryID(第一個DirectoryEntry的EntryID為0,下同)。
- 從048H到04BH的4字節UInt32,是該DirectoryEntry右兄弟的EntryID。
- 從04CH到04FH的4字節UInt32,是該DirectoryEntry一個孩子的EntryID。
- 從074H到077H的4字節UInt32,是該DirectoryEntry開始的SectorID。
- 從078H到07BH的4字節UInt32,是該DirectoryEntry存儲的所有字節長度。
顯然,Directory其實是一個樹形的結構,我們只要從第一個Entry(Root Entry)開始遞歸搜索就可以了。
為了方便開發,我們創建一個DirectoryEntry的類

1 public enum DirectoryEntryType : byte 2 { 3 Invalid = 0, 4 Storage = 1, 5 Stream = 2, 6 LockBytes = 3, 7 Property = 4, 8 Root = 5 9 } 10 11 public class DirectoryEntry 12 { 13 #region 字段 14 private UInt32 m_entryID; 15 private String m_entryName; 16 private DirectoryEntryType m_entryType; 17 private UInt32 m_sectorID; 18 private UInt32 m_length; 19 20 private DirectoryEntry m_parent; 21 private List<DirectoryEntry> m_children; 22 #endregion 23 24 #region 屬性 25 /// <summary> 26 /// 獲取DirectoryEntry的EntryID 27 /// </summary> 28 public UInt32 EntryID 29 { 30 get { return this.m_entryID; } 31 } 32 33 /// <summary> 34 /// 獲取DirectoryEntry名稱 35 /// </summary> 36 public String EntryName 37 { 38 get { return this.m_entryName; } 39 } 40 41 /// <summary> 42 /// 獲取DirectoryEntry類型 43 /// </summary> 44 public DirectoryEntryType EntryType 45 { 46 get { return this.m_entryType; } 47 } 48 49 /// <summary> 50 /// 獲取DirectoryEntry的SectorID 51 /// </summary> 52 public UInt32 SectorID 53 { 54 get { return this.m_sectorID; } 55 } 56 57 /// <summary> 58 /// 獲取DirectoryEntry的內容大小 59 /// </summary> 60 public UInt32 Length 61 { 62 get { return this.m_length; } 63 } 64 65 /// <summary> 66 /// 獲取DirectoryEntry的父節點 67 /// </summary> 68 public DirectoryEntry Parent 69 { 70 get { return this.m_parent; } 71 } 72 73 /// <summary> 74 /// 獲取DirectoryEntry的子節點 75 /// </summary> 76 public List<DirectoryEntry> Children 77 { 78 get { return this.m_children; } 79 } 80 #endregion 81 82 #region 構造函數 83 /// <summary> 84 /// 初始化新的DirectoryEntry 85 /// </summary> 86 /// <param name="parent">父節點</param> 87 /// <param name="entryID">DirectoryEntryID</param> 88 /// <param name="entryName">DirectoryEntry名稱</param> 89 /// <param name="entryType">DirectoryEntry類型</param> 90 /// <param name="sectorID">SectorID</param> 91 /// <param name="length">內容大小</param> 92 public DirectoryEntry(DirectoryEntry parent, UInt32 entryID, String entryName, DirectoryEntryType entryType, UInt32 sectorID, UInt32 length) 93 { 94 this.m_entryID = entryID; 95 this.m_entryName = entryName; 96 this.m_entryType = entryType; 97 this.m_sectorID = sectorID; 98 this.m_length = length; 99 this.m_parent = parent; 100 101 if (entryType == DirectoryEntryType.Root || entryType == DirectoryEntryType.Storage) 102 { 103 this.m_children = new List<DirectoryEntry>(); 104 } 105 } 106 #endregion 107 108 #region 方法 109 public void AddChild(DirectoryEntry entry) 110 { 111 if (this.m_children == null) 112 { 113 this.m_children = new List<DirectoryEntry>(); 114 } 115 116 this.m_children.Add(entry); 117 } 118 119 public DirectoryEntry GetChild(String entryName) 120 { 121 for (Int32 i = 0; i < this.m_children.Count; i++) 122 { 123 if (String.Equals(this.m_children[i].EntryName, entryName)) 124 { 125 return this.m_children[i]; 126 } 127 } 128 129 return null; 130 } 131 #endregion 132 }
然后我們遞歸搜索就可以了

1 #region 常量 2 private const UInt32 HeaderSize = 0x200;//512字節 3 private const UInt32 DirectoryEntrySize = 0x80;//128字節 4 #endregion 5 6 #region 讀取目錄信息 7 private void ReadDirectory() 8 { 9 if (this.m_reader == null) 10 { 11 return; 12 } 13 14 UInt32 leftSiblingEntryID, rightSiblingEntryID, childEntryID; 15 this.m_dirRootEntry = GetDirectoryEntry(0, null, out leftSiblingEntryID, out rightSiblingEntryID, out childEntryID); 16 this.ReadDirectoryEntry(this.m_dirRootEntry, childEntryID); 17 } 18 19 private void ReadDirectoryEntry(DirectoryEntry rootEntry, UInt32 entryID) 20 { 21 UInt32 leftSiblingEntryID, rightSiblingEntryID, childEntryID; 22 DirectoryEntry entry = GetDirectoryEntry(entryID, rootEntry, out leftSiblingEntryID, out rightSiblingEntryID, out childEntryID); 23 24 if (entry == null || entry.EntryType == DirectoryEntryType.Invalid) 25 { 26 return; 27 } 28 29 rootEntry.AddChild(entry); 30 31 if (leftSiblingEntryID < UInt32.MaxValue)//有左兄弟節點 32 { 33 this.ReadDirectoryEntry(rootEntry, leftSiblingEntryID); 34 } 35 36 if (rightSiblingEntryID < UInt32.MaxValue)//有右兄弟節點 37 { 38 this.ReadDirectoryEntry(rootEntry, rightSiblingEntryID); 39 } 40 41 if (childEntryID < UInt32.MaxValue)//有孩子節點 42 { 43 this.ReadDirectoryEntry(entry, childEntryID); 44 } 45 } 46 47 private DirectoryEntry GetDirectoryEntry(UInt32 entryID, DirectoryEntry parentEntry, out UInt32 leftSiblingEntryID, out UInt32 rightSiblingEntryID, out UInt32 childEntryID) 48 { 49 leftSiblingEntryID = UInt16.MaxValue; 50 rightSiblingEntryID = UInt16.MaxValue; 51 childEntryID = UInt16.MaxValue; 52 53 this.m_stream.Seek(GetDirectoryEntryOffset(entryID), SeekOrigin.Begin); 54 55 if (this.m_stream.Position >= this.m_length) 56 { 57 return null; 58 } 59 60 StringBuilder temp = new StringBuilder(); 61 for (Int32 i = 0; i < 32; i++) 62 { 63 temp.Append((Char)this.m_reader.ReadUInt16()); 64 } 65 66 UInt16 nameLen = this.m_reader.ReadUInt16(); 67 String name = (temp.ToString(0, (temp.Length < (nameLen / 2 - 1) ? temp.Length : nameLen / 2 - 1))); 68 Byte type = this.m_reader.ReadByte(); 69 70 if (type > 5) 71 { 72 return null; 73 } 74 75 this.m_stream.Seek(1, SeekOrigin.Current); 76 leftSiblingEntryID = this.m_reader.ReadUInt32(); 77 rightSiblingEntryID = this.m_reader.ReadUInt32(); 78 childEntryID = this.m_reader.ReadUInt32(); 79 80 this.m_stream.Seek(36, SeekOrigin.Current); 81 UInt32 sectorID = this.m_reader.ReadUInt32(); 82 UInt32 length = this.m_reader.ReadUInt32(); 83 84 return new DirectoryEntry(parentEntry, entryID, name, (DirectoryEntryType)type, sectorID, length); 85 } 86 #endregion 87 88 #region 輔助方法 89 private Int64 GetSectorOffset(UInt32 sectorID) 90 { 91 return HeaderSize + this.m_sectorSize * sectorID; 92 } 93 94 private Int64 GetDirectoryEntryOffset(UInt32 sectorID) 95 { 96 return HeaderSize + this.m_sectorSize * this.m_dirStartSectorID + DirectoryEntrySize * sectorID; 97 } 98 #endregion
【四、DocumentSummaryInformation和SummaryInformation】
Office文檔包含很多摘要信息,比如標題、作者、編輯時間等等,如下圖。
摘要信息又分為兩類,一類是DocumentSummaryInformation,另一類是SummaryInformation,分別包含不同種類的摘要信息。通過上述的代碼應該能獲取到Root Entry下有一個叫“\005DocumentSummaryInformation”的Entry和一個叫“\005SummaryInformation”的Entry。
對於DocumentSummaryInformation,其結構如下
- 從018H到01BH的4字節UInt32,是存儲屬性組的個數。
- 從01CH開始的每20字節,是屬性組的信息:
- 對於前16字節Byte[],如果是0x02 0xD5 0xCD 0xD5 0x9C 0x2E 0x1B 0x10 0x93 0x97 0x08 0x00 0x2B 0x2C 0xF9 0xAE,則表示是DocumentSummaryInformation;如果是0x05 0xD5 0xCD 0xD5 0x9C 0x2E 0x1B 0x10 0x93 0x97 0x08 0x00 0x2B 0x2C 0xF9 0xAE,則表示是UserDefinedProperties。
- 對於后4字節UInt32,則是該屬性組相對於Entry的偏移。
對於每個屬性組,其結構如下:
- 從000H到003H的4字節UInt32,是屬性組大小。
- 從004H到007H的4字節UInt32,是屬性組中屬性的個數。 從008H開始的每8字節,是屬性的信息:
- 對於前4字節UInt32,是屬性編號,表示屬性的種類。
- 對於后4字節UInt32,是屬性內容相對於屬性組的偏移。
常見的屬性編號有以下這些:

1 public enum DocumentSummaryInformationType : uint 2 { 3 Unknown = 0x00, 4 CodePage = 0x01, 5 Category = 0x02, 6 PresentationTarget = 0x03, 7 Bytes = 0x04, 8 LineCount = 0x05, 9 ParagraphCount = 0x06, 10 Slides = 0x07, 11 Notes = 0x08, 12 HiddenSlides = 0x09, 13 MMClips = 0x0A, 14 Scale = 0x0B, 15 HeadingPairs = 0x0C, 16 DocumentParts = 0x0D, 17 Manager = 0x0E, 18 Company = 0x0F, 19 LinksDirty = 0x10, 20 CountCharsWithSpaces = 0x11, 21 SharedDoc = 0x13, 22 HyperLinksChanged = 0x16, 23 Version = 0x17, 24 ContentStatus = 0x1B 25 }
對於每個屬性,其結構如下:
- 從000H到003H的4字節UInt32,是屬性內容的類型。
- 類型為0x02時為UInt16。
- 類型為0x03時為UInt32。
- 類型為0x0B時為Boolean。
- 類型為0x1E時為String。
- 剩余的字節為屬性的內容。
- 除了類型是String時為不定長,其余三種均為4位字節(多余字節置0)。
- 類型是String時前4字節是字符串的長度(包括“\0”),所以沒法使用BinaryReader的ReadString讀取。之后長度為字符串內容,字符串是使用單字節編碼進行存儲的,可以使用Encoding中的GetString獲取字符串內容。
為了方便開發,我們創建一個DocumentSummary的類。比較有意思的是,不論DocumentSummaryInformation還是SummaryInformation,第一個屬性都是記錄該組內容的代碼頁編碼,可以通過Encoding.GetEncoding()獲取對應的編碼然后用GetString把對應的字符串解析出來:

1 public class DocumentSummaryInformation 2 { 3 #region 字段 4 private DocumentSummaryInformationType m_propertyID; 5 private Object m_data; 6 #endregion 7 8 #region 屬性 9 /// <summary> 10 /// 獲取屬性類型 11 /// </summary> 12 public DocumentSummaryInformationType Type 13 { 14 get { return this.m_propertyID; } 15 } 16 17 /// <summary> 18 /// 獲取屬性數據 19 /// </summary> 20 public Object Data 21 { 22 get { return this.m_data; } 23 } 24 #endregion 25 26 #region 構造函數 27 /// <summary> 28 /// 初始化新的非字符串型DocumentSummaryInformation 29 /// </summary> 30 /// <param name="propertyID">屬性ID</param> 31 /// <param name="propertyType">屬性數據類型</param> 32 /// <param name="data">屬性數據</param> 33 public DocumentSummaryInformation(UInt32 propertyID, UInt32 propertyType, Byte[] data) 34 { 35 this.m_propertyID = (DocumentSummaryInformationType)propertyID; 36 if (propertyType == 0x02) this.m_data = BitConverter.ToUInt16(data, 0); 37 else if (propertyType == 0x03) this.m_data = BitConverter.ToUInt32(data, 0); 38 else if (propertyType == 0x0B) this.m_data = BitConverter.ToBoolean(data, 0); 39 } 40 41 /// <summary> 42 /// 初始化新的字符串型DocumentSummaryInformation 43 /// </summary> 44 /// <param name="propertyID">屬性ID</param> 45 /// <param name="propertyType">屬性數據類型</param> 46 /// <param name="codePage">代碼頁標識符</param> 47 /// <param name="data">屬性數據</param> 48 public DocumentSummaryInformation(UInt32 propertyID, UInt32 propertyType, Int32 codePage, Byte[] data) 49 { 50 this.m_propertyID = (DocumentSummaryInformationType)propertyID; 51 if (propertyType == 0x1E) this.m_data = Encoding.GetEncoding(codePage).GetString(data).Replace("\0", ""); 52 } 53 #endregion 54 }
然后我們進行讀取就可以了:

1 private List<DocumentSummaryInformation> m_documentSummaryInformation; 2 3 #region 讀取DocumentSummaryInformation 4 private void ReadDocumentSummaryInformation() 5 { 6 DirectoryEntry entry = this.m_dirRootEntry.GetChild('\x05' + "DocumentSummaryInformation"); 7 8 if (entry == null) 9 { 10 return; 11 } 12 13 Int64 entryStart = this.GetSectorOffset(entry.SectorID); 14 15 this.m_stream.Seek(entryStart + 24, SeekOrigin.Begin); 16 UInt32 propertysCount = this.m_reader.ReadUInt32(); 17 UInt32 docSumamryStart = 0; 18 19 for (Int32 i = 0; i < propertysCount; i++) 20 { 21 Byte[] clsid = this.m_reader.ReadBytes(16); 22 if (clsid[0] == 0x02 && clsid[1] == 0xD5 && clsid[2] == 0xCD && clsid[3] == 0xD5 && 23 clsid[4] == 0x9C && clsid[5] == 0x2E && clsid[6] == 0x1B && clsid[7] == 0x10 && 24 clsid[8] == 0x93 && clsid[9] == 0x97 && clsid[10] == 0x08 && clsid[11] == 0x00 && 25 clsid[12] == 0x2B && clsid[13] == 0x2C && clsid[14] == 0xF9 && clsid[15] == 0xAE)//如果是DocumentSummaryInformation 26 { 27 docSumamryStart = this.m_reader.ReadUInt32(); 28 break; 29 } 30 else 31 { 32 this.m_stream.Seek(4, SeekOrigin.Current); 33 } 34 } 35 36 if (docSumamryStart == 0) 37 { 38 return; 39 } 40 41 this.m_stream.Seek(entryStart + docSumamryStart, SeekOrigin.Begin); 42 this.m_documentSummaryInformation = new List<DocumentSummaryInformation>(); 43 UInt32 docSummarySize = this.m_reader.ReadUInt32(); 44 UInt32 docSummaryCount = this.m_reader.ReadUInt32(); 45 Int64 offsetMark = this.m_stream.Position; 46 Int32 codePage = Encoding.Default.CodePage; 47 48 for (Int32 i = 0; i < docSummaryCount; i++) 49 { 50 this.m_stream.Seek(offsetMark, SeekOrigin.Begin); 51 UInt32 propertyID = this.m_reader.ReadUInt32(); 52 UInt32 properyOffset = this.m_reader.ReadUInt32(); 53 54 offsetMark = this.m_stream.Position; 55 56 this.m_stream.Seek(entryStart + docSumamryStart + properyOffset, SeekOrigin.Begin); 57 UInt32 propertyType = this.m_reader.ReadUInt32(); 58 DocumentSummaryInformation info = null; 59 Byte[] data = null; 60 61 if (propertyType == 0x1E) 62 { 63 UInt32 strLen = this.m_reader.ReadUInt32(); 64 data = this.m_reader.ReadBytes((Int32)strLen); 65 info = new DocumentSummaryInformation(propertyID, propertyType, codePage, data); 66 } 67 else 68 { 69 data = this.m_reader.ReadBytes(4); 70 info = new DocumentSummaryInformation(propertyID, propertyType, data); 71 72 if (info.Type == DocumentSummaryInformationType.CodePage)//如果找到CodePage的屬性 73 { 74 codePage = (Int32)(UInt16)info.Data; 75 } 76 } 77 78 this.m_documentSummaryInformation.Add(info); 79 } 80 } 81 #endregion
而SummaryInformation與DocumentSummaryInformation相比讀取方式是一樣的,只不過屬性組的16位標識為0xE0 0x85 0x9F 0xF2 0xF9 0x4F 0x68 0x10 0xAB 0x91 0x08 0x00 0x2B 0x27 0xB3 0xD9。
常見的SummaryInformation屬性的屬性編號如下:

1 public enum SummaryInformationType : uint 2 { 3 Unknown = 0x00, 4 CodePage = 0x01, 5 Title = 0x02, 6 Subject = 0x03, 7 Author = 0x04, 8 Keyword = 0x05, 9 Commenct = 0x06, 10 Template = 0x07, 11 LastAuthor = 0x08, 12 Reversion = 0x09, 13 EditTime = 0x0A, 14 CreateDateTime = 0x0C, 15 LastSaveDateTime = 0x0D, 16 PageCount = 0x0E, 17 WordCount = 0x0F, 18 CharCount = 0x10, 19 ApplicationName = 0x12, 20 Security = 0x13 21 }
其他代碼由於與DocumentSummaryInformation相近就不再單獨給出了。
附,本文所有代碼下載:https://github.com/mayswind/SimpleOfficeReader
1、Microsoft Open Specifications:http://www.microsoft.com/openspecifications/en/us/programs/osp/default.aspx
2、用PHP讀取MS Word(.doc)中的文字:https://imethan.com/post-2009-10-06-17-59.html
3、Office檔案格式:http://www.programmer-club.com.tw/ShowSameTitleN/general/2681.html
4、LAOLA file system:http://stuff.mit.edu/afs/athena/astaff/project/mimeutils/share/laola/guide.html
【后記】
花了好幾天的時間才寫完讀取DocumentSummaryInformation和SummaryInformation,果然自己寫程序用和寫成文章區別太大了,前者差不多就行,后者還得仔細查閱資料。如果您覺得好就點下推薦唄。