asp.net + C# 如何判断客户端的Excel版本
在ASP.NET中服务器上没有安装MSOFFICE,没有EXCEL我要如何判断客户端上安装的是Excel是哪个版本?请帮忙详细点。最好有代码。...
在ASP.NET中 服务器上没有安装MS OFFICE,没有EXCEL我要如何判断客户端上安装的是Excel是哪个版本?
请帮忙详细点。最好有代码。 展开
请帮忙详细点。最好有代码。 展开
3个回答
展开全部
简单的方法是通过文件扩展名来辨别,EXCEL 2003扩展名.xls,2007则是.xlsx,如果扩展名不规范这种方法就不行了。所以最好是对两种文档的关键字节进行对比来判断版本,因为不同版本,文件存储方式是不一样的。
C#的没有,VB的手头有个。可供参考:
''看Excel文档的版本
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Using dialog As New OpenFileDialog
With dialog
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Console.WriteLine(Global.LzmTW.Data.MSOffice.VersionChecker.Check(.FileName, MSOffice.AppType.Excel).ToString)
End If
End With
End Using
End Sub
代码:
Namespace MSOffice
Public Enum AppType
Access
Excel
Word
End Enum
End Namespace
Namespace MSOffice
Public Enum VersionType
V2003
V2007
Unknown
End Enum
End Namespace
Namespace MSOffice
Public Class VersionChecker
Private Sub New()
End Sub
Shared Sub New()
Dim info As CompareInfo
info = New CompareInfo
With info
.Position = 0
.Length = &HD
.PivotalValues2003 = New Byte() {&HD0, &HCF, &H11, &HE0, &HA1, &HB1, &H1A, &HE1, &H0, &H0, &H0, &H0, &H0, &H0}
.PivotalValues2007 = New Byte() {&H50, &H4B, &H3, &H4, &H14, &H0, &H6, &H0, &H8, &H0, &H0, &H0, &H21, &H0}
End With
Collection.Add(AppType.Excel, info)
info = New CompareInfo
With info
.Position = 0
.Length = &HD
.PivotalValues2003 = New Byte() {&HD0, &HCF, &H11, &HE0, &HA1, &HB1, &H1A, &HE1, &H0, &H0, &H0, &H0, &H0, &H0}
.PivotalValues2007 = New Byte() {&H50, &H4B, &H3, &H4, &H14, &H0, &H6, &H0, &H8, &H0, &H0, &H0, &H21, &H0}
End With
Collection.Add(AppType.Word, info)
info = New CompareInfo
With info
.Position = 0
.Length = &HF
.PivotalValues2003 = New Byte() {&H0, &H1, &H0, &H0, &H53, &H74, &H61, &H6E, &H64, &H61, &H72, &H64, &H20, &H4A, &H65, &H74}
.PivotalValues2007 = New Byte() {&H0, &H1, &H0, &H0, &H53, &H74, &H61, &H6E, &H64, &H61, &H72, &H64, &H20, &H41, &H43, &H45}
End With
Collection.Add(AppType.Access, info)
End Sub
Public Shared Function Check(ByVal file As String, ByVal apptype As AppType) As VersionType
Dim fileInfo As New System.IO.FileInfo(file)
If Not fileInfo.Exists Then Return VersionType.Unknown
Dim info As CompareInfo = Collection.Item(apptype)
If fileInfo.Length < info.Length Then Return VersionType.Unknown
Dim bytes(info.Length) As Byte
Using streamReader As System.IO.FileStream = fileInfo.OpenRead
With streamReader
.Read(bytes, info.Position, info.Length + 1)
End With
End Using
Dim IsMatch As Boolean
IsMatch = info.IsMatch2003(bytes)
If IsMatch Then Return VersionType.V2003
IsMatch = info.IsMatch2007(bytes)
If IsMatch Then
Return VersionType.V2007
Else
Return VersionType.Unknown
End If
End Function
Private Shared Collection As New Dictionary(Of AppType, CompareInfo)
Private Class CompareInfo
Private gPosition As Integer = 0
Public Property Position() As Integer
Get
Return gPosition
End Get
Set(ByVal value As Integer)
gPosition = value
End Set
End Property
Private gLength As Integer
Public Property Length() As Integer
Get
Return gLength
End Get
Set(ByVal value As Integer)
gLength = value
End Set
End Property
Private gPivotalValues2003 As Byte()
Public Property PivotalValues2003() As Byte()
Get
Return gPivotalValues2003
End Get
Set(ByVal value As Byte())
gPivotalValues2003 = value
End Set
End Property
Private gPivotalValues2007 As Byte()
Public Property PivotalValues2007() As Byte()
Get
Return gPivotalValues2007
End Get
Set(ByVal value As Byte())
gPivotalValues2007 = value
End Set
End Property
Public Function IsMatch2003(ByVal bytes As Byte()) As Boolean
Return Compare(Me.PivotalValues2003, bytes)
End Function
Public Function IsMatch2007(ByVal bytes As Byte()) As Boolean
Return Compare(Me.PivotalValues2007, bytes)
End Function
Private Function Compare(ByVal bytes1 As Byte(), ByVal bytes2 As Byte()) As Boolean
Dim mIsMatch As Boolean = True
For i As Integer = 0 To bytes1.Length - 1
mIsMatch = bytes1(i).Equals(bytes2(i)) And mIsMatch
If Not mIsMatch Then Exit For
Next
Return mIsMatch
End Function
End Class
End Class
End Namespace
C#的没有,VB的手头有个。可供参考:
''看Excel文档的版本
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Using dialog As New OpenFileDialog
With dialog
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Console.WriteLine(Global.LzmTW.Data.MSOffice.VersionChecker.Check(.FileName, MSOffice.AppType.Excel).ToString)
End If
End With
End Using
End Sub
代码:
Namespace MSOffice
Public Enum AppType
Access
Excel
Word
End Enum
End Namespace
Namespace MSOffice
Public Enum VersionType
V2003
V2007
Unknown
End Enum
End Namespace
Namespace MSOffice
Public Class VersionChecker
Private Sub New()
End Sub
Shared Sub New()
Dim info As CompareInfo
info = New CompareInfo
With info
.Position = 0
.Length = &HD
.PivotalValues2003 = New Byte() {&HD0, &HCF, &H11, &HE0, &HA1, &HB1, &H1A, &HE1, &H0, &H0, &H0, &H0, &H0, &H0}
.PivotalValues2007 = New Byte() {&H50, &H4B, &H3, &H4, &H14, &H0, &H6, &H0, &H8, &H0, &H0, &H0, &H21, &H0}
End With
Collection.Add(AppType.Excel, info)
info = New CompareInfo
With info
.Position = 0
.Length = &HD
.PivotalValues2003 = New Byte() {&HD0, &HCF, &H11, &HE0, &HA1, &HB1, &H1A, &HE1, &H0, &H0, &H0, &H0, &H0, &H0}
.PivotalValues2007 = New Byte() {&H50, &H4B, &H3, &H4, &H14, &H0, &H6, &H0, &H8, &H0, &H0, &H0, &H21, &H0}
End With
Collection.Add(AppType.Word, info)
info = New CompareInfo
With info
.Position = 0
.Length = &HF
.PivotalValues2003 = New Byte() {&H0, &H1, &H0, &H0, &H53, &H74, &H61, &H6E, &H64, &H61, &H72, &H64, &H20, &H4A, &H65, &H74}
.PivotalValues2007 = New Byte() {&H0, &H1, &H0, &H0, &H53, &H74, &H61, &H6E, &H64, &H61, &H72, &H64, &H20, &H41, &H43, &H45}
End With
Collection.Add(AppType.Access, info)
End Sub
Public Shared Function Check(ByVal file As String, ByVal apptype As AppType) As VersionType
Dim fileInfo As New System.IO.FileInfo(file)
If Not fileInfo.Exists Then Return VersionType.Unknown
Dim info As CompareInfo = Collection.Item(apptype)
If fileInfo.Length < info.Length Then Return VersionType.Unknown
Dim bytes(info.Length) As Byte
Using streamReader As System.IO.FileStream = fileInfo.OpenRead
With streamReader
.Read(bytes, info.Position, info.Length + 1)
End With
End Using
Dim IsMatch As Boolean
IsMatch = info.IsMatch2003(bytes)
If IsMatch Then Return VersionType.V2003
IsMatch = info.IsMatch2007(bytes)
If IsMatch Then
Return VersionType.V2007
Else
Return VersionType.Unknown
End If
End Function
Private Shared Collection As New Dictionary(Of AppType, CompareInfo)
Private Class CompareInfo
Private gPosition As Integer = 0
Public Property Position() As Integer
Get
Return gPosition
End Get
Set(ByVal value As Integer)
gPosition = value
End Set
End Property
Private gLength As Integer
Public Property Length() As Integer
Get
Return gLength
End Get
Set(ByVal value As Integer)
gLength = value
End Set
End Property
Private gPivotalValues2003 As Byte()
Public Property PivotalValues2003() As Byte()
Get
Return gPivotalValues2003
End Get
Set(ByVal value As Byte())
gPivotalValues2003 = value
End Set
End Property
Private gPivotalValues2007 As Byte()
Public Property PivotalValues2007() As Byte()
Get
Return gPivotalValues2007
End Get
Set(ByVal value As Byte())
gPivotalValues2007 = value
End Set
End Property
Public Function IsMatch2003(ByVal bytes As Byte()) As Boolean
Return Compare(Me.PivotalValues2003, bytes)
End Function
Public Function IsMatch2007(ByVal bytes As Byte()) As Boolean
Return Compare(Me.PivotalValues2007, bytes)
End Function
Private Function Compare(ByVal bytes1 As Byte(), ByVal bytes2 As Byte()) As Boolean
Dim mIsMatch As Boolean = True
For i As Integer = 0 To bytes1.Length - 1
mIsMatch = bytes1(i).Equals(bytes2(i)) And mIsMatch
If Not mIsMatch Then Exit For
Next
Return mIsMatch
End Function
End Class
End Class
End Namespace
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询