asp.net + C# 如何判断客户端的Excel版本

在ASP.NET中服务器上没有安装MSOFFICE,没有EXCEL我要如何判断客户端上安装的是Excel是哪个版本?请帮忙详细点。最好有代码。... 在ASP.NET中 服务器上没有安装MS OFFICE,没有EXCEL我要如何判断客户端上安装的是Excel是哪个版本?
请帮忙详细点。最好有代码。
展开
 我来答
隔壁的虫子
2009-08-06 · TA获得超过1336个赞
知道小有建树答主
回答量:614
采纳率:0%
帮助的人:0
展开全部
简单的方法是通过文件扩展名来辨别,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
server126
2009-08-06 · TA获得超过597个赞
知道小有建树答主
回答量:877
采纳率:0%
帮助的人:187万
展开全部
二楼好像太复杂了吧 没有那么麻烦
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
caojiayin1984
2009-08-06 · 超过99用户采纳过TA的回答
知道小有建树答主
回答量:168
采纳率:100%
帮助的人:189万
展开全部
try{}吧。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式