[upload=rar]viewFile.asp?ID=26[/upload] [upload=rar]viewFile.asp?ID=27[/upload] 更新进度条的子程序 Sub UpdateProgress(PB As Control, ByVal Percent) \'本实例使用一个PictureBox控件模拟滚动条效果 \'百分比 Dim Num As String On Error GoTo Err_UpdateProgress If Not PB.AutoRedraw Then PB.AutoRedraw = -1 End If \'清空PictureBox PB.Cls PB.ScaleWidth = 100 \'xor画刷模式 PB.DrawMode = 10 Num = BarString & Format$(Percent, \"###\") + \"%\" PB.CurrentX = 50 - PB.TextWidth(Num) / 2 PB.CurrentY = (PB.ScaleHeight - PB.TextHeight(Num)) / 2 \'显示百分比 PB.Print Num PB.Line (0, 0)-(Percent, PB.ScaleHeight), , BF \'刷新 PB.Refresh
Exit_UpdateProgress:
On Error GoTo 0 Exit Sub Err_UpdateProgress:
Select Case Err Case 0 Resume Next Case Else MsgBox \"Error: \" & Err.Number & vbNewLine & Err.Description, vbInformation, \"错误\" Resume Exit_UpdateProgress End Select
End Sub
\'将Recordset中的数据以Html格式写入文件 Private Sub ToHTML(SN As Recordset, strCaption As String, FileName As String) Open FileName For Output As #1 \'Html的文件头和页面信息 Print #1, \"<HTML>\" Print #1, \"<HEAD>\" Print #1, \"<meta http-equiv=\"\"Content-Type\"\" content=\"\"text/html; charset=gb2312\"\">\" Print #1, \"<meta http-equiv=\"\"Content-Language\"\" content=\"\" zh - cn \"\" > \" Print #1, \"<meta name=\"\"GENERATOR\"\" content = \"\" Visual Basic Access to Html \"\" > \" \'Html标题 Print #1, \"<TITLE>\" & strCaption & \"</TITLE>\" Print #1, \"<STYLE>\" Print #1, \"<!--\" Print #1, \"BODY,td {\" Print #1, \"font-family:\"\"宋体,Arial Black\"\";\" Print #1, \"font-size:9pt;\" Print #1, \"line-height:16px;\" Print #1, \"}\" Print #1, \"-->\" Print #1, \"</STYLE>\" Print #1, \"</Head>\" \'数据开始 Print #1, \"<Body>\" Print #1, \"<Table border=\"\" 1\"\">\" \'自动根据字段数量确定列宽 If SN.Fields.Count > 1 Then lwidth = 100 / SN.Fields.Count - 1 Else lwidth = 100 / SN.Fields.Count End If \'保证列宽大小 If lwidth < 10 Then lwidth = 10 End If \'若要设置固定列宽,将下面一行的注释符号去除即可 lwidth = 1000 \'先输出表头 Print #1, \"<TR>\" For i = 0 To SN.Fields.Count - 1 Print #1, \"<td width=\"\"\" & Str(lwidth) & \"\"\" bgcolor = \"\"#B1CACF\"\" > \" Print #1, SN.Fields(i).Name Print #1, \"</td>\" Next i Print #1, \"</TR>\" \'开始输出数据 Do Until SN.EOF \'实现黑白交替效果 If j Mod 2 = 1 Then Print #1, \"<TR bgcolor = \"\"#EFEFEF\"\">\" Else Print #1, \"<TR bgcolor = \"\"#FFFFFF\"\">\" End If \'输出每个字段数据 For i = 0 To SN.Fields.Count - 1 Print #1, \"<td width=\"\"\" & Str(lwidth) & \"\"\"> \" Print #1, SN.Fields(i).Value Print #1, \"</td>\" Next i Print #1, \"</TR>\" SN.MoveNext UpdateProgress Picture1, j / SN.RecordCount * 100 j = j + 1 Loop \'Html文件结束 Print #1, \"</Table>\" Print #1, \"</Body>\" Print #1, \"</HTML>\" Close #1 End Sub
\'复制Recordset中数据到Excel表格Worksheet Private Sub CopyRecords(RST As Recordset, WS As Worksheet, StartingCell As ExlCell) Dim SomeArray() As Variant Dim Row As Long Dim Col As Long Dim Fd As Field On Error GoTo Err_CopyRecords \'检测Recordset中是否没有数据 If RST.EOF And RST.BOF Then Exit Sub RST.MoveLast ReDim SomeArray(RST.RecordCount + 1, RST.Fields.Count) \'拷贝表头到数组 Col = 0 For Each Fd In RST.Fields SomeArray(0, Col) = Fd.Name Col = Col + 1 Next \'拷贝Recordset到数组 RST.MoveFirst Recs = RST.RecordCount Counter = 0 For Row = 1 To RST.RecordCount - 1 Counter = Counter + 1 If Counter <= Recs Then i = (Counter / Recs) * 100 UpdateProgress Picture1, i For Col = 0 To RST.Fields.Count - 1 SomeArray(Row, Col) = RST.Fields(Col).Value If IsNull(SomeArray(Row, Col)) Then _ SomeArray(Row, Col) = \"\" Next RST.MoveNext Next \'将数组填充到Excel WorkSheet \'Range应该和数组拥有同样的行数和列数 WS.Range(WS.Cells(StartingCell.Row, StartingCell.Col), _ WS.Cells(StartingCell.Row + RST.RecordCount + 1, _ StartingCell.Col + RST.Fields.Count)).Value = SomeArray
Exit_CopyRecords: On Error GoTo 0 Exit Sub Err_CopyRecords: Select Case Err Case 0 Resume Next Case Else MsgBox \"错误: \" & Err.Number & vbNewLine & Err.Description, vbInformation, \"错误\" Resume Exit_CopyRecords End Select End Sub
\'将Recordset数据转换到Excel中 Private Sub ToExcel(SN As Recordset, strCaption As String) Dim oExcel As Object \'OLE自动化对象 Dim objExlSht As Object Dim stCell As ExlCell
On Error GoTo Err_ToExcel DoEvents On Error Resume Next Set oExcel = GetObject(, \"Excel.Application\") \'若Excel没有启动 If Err = 429 Then Err = 0 Set oExcel = CreateObject(\"Excel.Application\") \'无法创建Excel对象 If Err = 429 Then MsgBox Err & \": \" & Error, vbExclamation + vbOKOnly Exit Sub End If End If oExcel.Workbooks.Add oExcel.Worksheets(\"sheet1\").Name = strCaption Set objExlSht = oExcel.ActiveWorkbook.Sheets(1) stCell.Row = 1 stCell.Col = 1 \'填充Excel表格 CopyRecords SN, objExlSht, stCell \'将控制权交给用户 oExcel.Visible = True oExcel.Interactive = True \'测试对象是否活动并释放对象 If Not (objExlSht Is Nothing) Then Set objExlSht = Nothing End If If Not (oExcel Is Nothing) Then Set oExcel = Nothing End If If Not (SN Is Nothing) Then Set SN = Nothing End If UpdateProgress Picture1, 100 Exit_ToExcel: On Error GoTo 0 Exit Sub Err_ToExcel: Select Case Err Case 0 Resume Next Case Else MsgBox \"错误: \" & Err.Number & vbNewLine & Err.Description, vbInformation, \"错误\" Resume Exit_ToExcel End Select
End Sub |