读取Excel文件的版本

读取xls文件和xlsx文件创建的版本号。

虽然xlsx声明的是向前兼容,但是不知道OleDb是不是也是这样,没有办法所以要读取文件版本,限定只能读取Excel2007保存的文件。

 1 using ICSharpCode.SharpZipLib.Zip;
 2 
 3 public const ushort BIFF8 = 0x0600;
 4 public const ushort BIFF7 = 0x0500;
 5 public static string GetExcelVersion(string fileName)
 6         {
 7             string version = string.Empty;
 8             try
 9             {
10                 if (Path.GetExtension(fileName) == ".xls")
11                 {
12                     BinaryReader binReader = new BinaryReader(File.Open(fileName, FileMode.Open));
13                     try
14                     {
15                         byte[] testArray = new byte[2];
16                         int count = binReader.Read(testArray, 0, 2);
17 
18                         if (count != 0)
19                         {
20                             // Reset the position in the stream to zero.
21                             binReader.BaseStream.Seek(0, SeekOrigin.Begin);
22                             byte[] testArray2 = new byte[512];
23                             int count2 = binReader.Read(testArray2, 0, 512);
24                             ushort BOF = binReader.ReadUInt16();
25                             ushort Length = binReader.ReadUInt16();
26                             ushort Version = binReader.ReadUInt16();
27                             ushort file = binReader.ReadUInt16();
28                             if (Version == BIFF8)
29                             {
30                                 version = "Excel8.0";
31                             }
32                             if (Version == BIFF7)
33                             {
34                                 version = "Excel8.0";
35                             }
36                         }
37                     }
38                     catch (EndOfStreamException e)
39                     {
40                         throw e;
41                     }
42                     finally
43                     {
44                         binReader.Close();
45                     }
46                 }
47                 else if (Path.GetExtension(fileName) == ".xlsx")
48                 {
49                     ZipFile zip = new ZipFile(fileName);
50                     try
51                     {
52                         IEnumerator entries = zip.GetEnumerator();
53                         while (entries.MoveNext())
54                         {
55                             ZipEntry current = (ZipEntry)entries.Current;
56                             if (current.Name.Equals("docProps/app.xml"))
57                             {
58                                 Stream stream = zip.GetInputStream(current);
59                                 XPathNavigator navigator = new XPathDocument(stream).CreateNavigator();
60                                 XmlNamespaceManager resolver = new XmlNamespaceManager(navigator.NameTable);
61                                 resolver.AddNamespace("x", "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties");
62                                 XPathNodeIterator iterator = navigator.Select("//x:AppVersion", resolver);
63                                 iterator.MoveNext();
64                                 string attribute = iterator.Current.InnerXml;
65                                 version = attribute;
66                             }
67                         }
68                     }
69                     catch (IOException ioEx)
70                     {
71                         throw ioEx;
72                     }
73                     finally
74                     {
75                         if (zip != null)
76                         {
77                             zip.Close();
78                         }
79                     }
80                 }
81             }
82             catch (IOException ioEx)
83             {
84                 throw ioEx;
85             }
86 
87             return version;
88         }
View Code

调用:

 1   catch
 2                         {
 3                             try
 4                             {
 5                                 string version = GetExcelVersion(FilePath);
 6                                 string error = string.Empty;
 7                                 if (version == "")
 8                                 {
 9                                     error = "无法识别的Excel文件,请确保文件为有效的Excel2003或者Excel2007格式文件。";
10                                 }
11                                 else if (!version.Contains("12") && !version.Contains("Excel8.0"))
12                                 {
13                                     error = string.Format("由高版本的Excel程序创建,请转换为Excel2003或者Excel2007格式文件", version);
14                                 }
15                                 throw new InvalidDataException(error);
16                             }
17                             catch (IOException ioEx)
18                             {
19                                 throw ioEx;
20                             }
21                         }
View Code

 

posted @ 2014-04-15 12:55  太一吾鱼水  阅读(537)  评论(0编辑  收藏  举报