1、* * * VB 将数据导出到 EXCEL,没有安装 EXCEL 的一样也可以导出. * * 调用方式: s_Export2Excel(Ado.Recordset) 或 s_Export2Excel(Rds.RecordSet) * 支持 Rds 与 Ado 的记录导出 * * 得到所有数据类型,有些数据类型 EXCEL 不支持,已经替换掉 Public Function f_FieldType$(ByVal sTypeDSN= ;FIRSTROWHASNAMES=1;READONLY=FALSE; “ DBQ= “ & sExcelFileName创建 Excel 文件,并创建表 Set i
2、DB = New ADODB.Connection iDB.Open iConcStr iDB.Execute iSql 插入数据 With sRecordSet .MoveFirst While .EOF = False iSql = “ “ iFdlist = “ “ For iI = 0 To .Fields.Count - 1 iFdType = f_FieldType(.Fields(iI).Type) If iFdType “image “ And IsNull(.Fields(iI).Value) = False Then iFdlist = iFdlist & “, “ & .
3、Fields(iI).Name & “ “ Select Case iFdType Case “char “, “varchar “, “nchar “, “nvarchar “, “text “ iSql = iSql & “, “ & .Fields(iI).Value & “ “ Case “datetime “ iSql = iSql & “,# “ & .Fields(iI).Value & “# “ Case “image “ Case Else iSql = iSql & “, “ & .Fields(iI).Value End Select End If Next iSql =
4、 “insert into “ & sTableName & “( “ & _ Mid(iFdlist, 2) & “) values( “ & Mid(iSql, 2) & “) “iDB.Execute iSql .MoveNext Wend End With 处理完毕,关闭数据库 iDB.Close Set iDB = Nothing MsgBox “已经将数据保存到 “ & sExcelFileName & “ “, 64 iRe = True GoTo lbexit lbErr: MsgBox “发生错误: “ & Err.Description & vbCrLf & _ “错误代码: “ & Err.Number, 64, “错误 “ lbexit: f_Export2Excel = iRe End Function