====== 代码实现 VB 6 导出数据到 Excel ======
想从SQL 中读数据导出到 EXCEL 中,数据已取到recordset 中,因为数据大约有7000行,每行有60个字段,所以按每单元格写太慢,要循环420000次,所以想按数据行写,不知道该如何实现?或者有其他方法能实现?
'写数据行
rstTemp.MoveFirst
Do While Not rstTemp.EOF
'每单元格写太慢
For lngTemp = 0 To rstTemp.Fields.Count - 1
xlsSheet.Cells(rstTemp.AbsolutePosition + 1, lngTemp + 1) = rstTemp.Fields(lngTemp).Value
Next lngTemp
rstTemp.MoveNext
Loop
[解决办法]
这么多数据的确不会太快
提高导入数度可以用xghim((-)) 的方法,与循环相比速度提升是非常大的
下面是asp.net实现的代码示例,你可参考一下:
Dim DataArray(ds_r, ds_c) As Object '数据数组,为批量导入EXCEL
'填充数组
For i As Integer = 0 To ds_r - 1
For j As Integer = 0 To ds_c - 1
DataArray(i, j) = ds.Tables(0).Rows(i)(j).ToString
Next j
Next i
Dim oFileName As String = User.Identity.Name.Split( "/ ")(1).ToString & "_temp.xls "
Dim oExcel As Object = CreateObject( "Excel.Application ")
Dim oBook As Object = oExcel.Workbooks.Add
Dim oSheet As Object = oBook.Worksheets(1)
'完成导入EXCEL
oSheet.Range( "A1 ").Value = "日期 "
oSheet.Range( "B1 ").Value = "单号 "
oSheet.Range( "C1 ").Value = "发货 "
oSheet.Range( "D1 ").Value = "回款 "
oSheet.Range( "E1 ").Value = "备注 "
oSheet.Range( "A2 ").Resize(ds_r, ds_c).Value = DataArray
oBook.SaveAs(Page.Server.MapPath( ". ") & oFileName)
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
VB6下代码要修改一下,导入道理一样
[解决办法]
这个办法导出比较快
将下文加入到一个模块中,屏幕中调用如下ExporToExcel( "select * from table ")则实现将其导出到EXCEL中
Public Function ExporToExcel(strOpen As String)
'*********************************************************
'* 名称:ExporToExcel
'* 功能:导出数据到EXCEL
'* 用法:ExporToExcel(sql查询字符串)
'*********************************************************
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 = Cn
.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.Bold = True
'标题字体加粗
.Range(.Cells(1, 1), .Cells(Irowcount + 1, Icolcount)).Borders.LineStyle = xlContinuous
'设表格边框样式
End With
With xlSheet.PageSetup
.LeftHeader = " " & Chr(10) & "& " "楷体_GB2312,常规 " "&10公司名称: " ' & Gsmc
.CenterHeader = "& " "楷体_GB2312,常规 " "公司人员情况表& " "宋体,常规 " " " & Chr(10) & "& " "楷体_GB2312,常规 " "&10日 期: "
.RightHeader = " " & Chr(10) & "& " "楷体_GB2312,常规 " "&10单位: "
.LeftFooter = "& " "楷体_GB2312,常规 " "&10制表人: "
.CenterFooter = "& " "楷体_GB2312,常规 " "&10制表日期: "
.RightFooter = "& " "楷体_GB2312,常规 " "&10第&P页 共&N页 "
End With
xlApp.Application.Visible = True
Set xlApp = Nothing ' "交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing
End Function
注:须在程序中引用 'Microsoft Excel 9.0 Object Library '和ADO对象,机器必装Excel 2000
[解决办法]
用您的XLS名称应该这样写 :
xlsSheet.Cells.CopyFromRecordset rstTemp.Recordset
[解决办法]
CopyFromRecordset
[解决办法]
newsheet.Cells.CopyFromRecordset Adodc1.Recordset
没错,就是这个最快!
[解决办法]
飞火流星的方法速度还可以
不过也有更快的
这里有三种快速导入EXCEL的方法:
1:mysheet.Cells.CopyFromRecordset Adodc1.Recordset
'该条语句实现把recordset导入excel文件
mybook.SaveAs (filename)
'保存文件
2:BCP方式:
EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:\temp1.xls -c -q -S "GNETDATA/GNETDATA " -U "sa " -P " " '
这样导出速度最快,但是它实际上是一个连接,每次打开excel表格,都重新select服务器上的最新数据
3:就是一个cell一个cell的循环进去,这样的方式最慢:等于是一直在操作excel,导入期间严重占用系统资源(不喜欢)