用VB編DLL﹐將數據導入EXCEL﹐ASP調用DLL﹐進程中有EXCEL.EXE﹐但是卻不可見。望高手指點。在線等﹗
用VB編DLL﹐將數據導入EXCEL﹐ASP調用DLL﹐進程中有EXCEL.EXE﹐但是卻不可見。望高手指點。在線等﹗
DLL代碼如下﹕
Public Function ExporToExcel(strsql As Variant, strcon As Variant)
Dim cn As New ADODB.Connection
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
cn.ConnectionString = strcon
cn.ConnectionTimeout = 20
cn.CommandTimeout = 600
cn.Open
With Rs_Data
If .State = adStateOpen Then
.Close
End If
.ActiveConnection = cn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Source = strsql
.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
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.Bold = True '字体加粗
.Range(.Cells(1, 1), .Cells(Irowcount + 1, Icolcount)).Borders.LineStyle = xlContinuous '表格邊框樣式
End With
xlApp.Application.Visible = True
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
cn.Close
End Function
ASP調用代碼如下﹕
<%
str="Provider=SQLOLEDB.1; Persist Security Info=True; User ID=sa; Password=juning; Data Source=10.192.35.142; Initial Catalog=SQC"
strsql = "select * from tbl_user"
set ex=server.CreateObject("IEDSDLL.clsExcel")
ex.ExporToExcel strsql,str
%>