怎么根据当前EXCEL版本,调用相应的EXCEL程序 将程序中的数据导出到EXCEL文件
如何根据当前EXCEL版本,调用相应的EXCEL程序 将程序中的数据导出到EXCEL文件
以下是我将数据导出到EXCEL文件的代码,现在的问题是我的程序中引用的是EXCEL2003版的程序,当客户电脑上安装的不是office2003,比如是OFFICE2007版时,以下代码无法调用EXCEL2007,以致无法将数据导出到EXCEL.
请问,我应该怎么处理,才能调用不同版本的EXCEL程序,以实现将数据导出到EXCEL程序?
------解决方案--------------------
这个,2007好像兼容2003吧,你使用2007的类库,遇到客户安装2007的就可以兼容;
当然,如果客户是2010,那没办法,你程序得引用2010的库才行;
最简单的办法,自己定义一个版本,要求客户必须安装这个版本
以下是我将数据导出到EXCEL文件的代码,现在的问题是我的程序中引用的是EXCEL2003版的程序,当客户电脑上安装的不是office2003,比如是OFFICE2007版时,以下代码无法调用EXCEL2007,以致无法将数据导出到EXCEL.
请问,我应该怎么处理,才能调用不同版本的EXCEL程序,以实现将数据导出到EXCEL程序?
Public Function ExporToExcel(strOpen As String)
Dim Rs_Data As New ADODB.Recordset
Dim Irowcount As Integer
Dim Icolcount As Integer
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlQuery As Excel.QueryTable
With Rs_Data
If .State = adStateOpen Then
.Close
End If
.ActiveConnection = Conn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Source = strOpen
.Open
End With
With Rs_Data
If .RecordCount < 1 Then
MsgBox ("没有记录!")
Exit Function
End If
'记录总数
Irowcount = .RecordCount
'字段总数
Icolcount = .Fields.Count
End With
Set xlApp = CreateObject("Excel.Application")
Set xlBook = Nothing
Set xlSheet = Nothing
Set xlBook = xlApp.Workbooks().Add
Set xlSheet = xlBook.Worksheets("sheet1")
xlApp.Visible = True
'添加查询语句,导入EXCEL数据
Set xlQuery = xlSheet.QueryTables.Add(Rs_Data, xlSheet.Range("a1"))
With xlQuery
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
End With
xlQuery.FieldNames = True '显示字段名
xlQuery.Refresh
With xlSheet
.Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Name = "黑体"
.Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Size = 10
'设标题为黑体字
' .Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Bold = True
'标题字体加粗
.Range(.Cells(1, 1), .Cells(Irowcount + 1, Icolcount)).Borders.LineStyle = xlContinuous
'设表格边框样式
End With
With xlSheet.PageSetup
.Orientation = xlLandscape
End With
xlApp.Application.Visible = True
Set xlApp = Nothing '"交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing
End Function
------解决方案--------------------
这个,2007好像兼容2003吧,你使用2007的类库,遇到客户安装2007的就可以兼容;
当然,如果客户是2010,那没办法,你程序得引用2010的库才行;
最简单的办法,自己定义一个版本,要求客户必须安装这个版本