在处理数据时,经常需要在不同的文件格式之间进行转换。DBF文件是一种常见的数据库文件格式,但随着时间的推移,一些现代应用程序,如Excel 2007,已经停止了对DBF文件的直接支持。尽管如此,仍然可以通过编写VBScript脚本来实现Excel与DBF文件之间的数据互转。本文将介绍如何编写这样的脚本,以及如何使用它们来读取和写入DBF文件。
要读取DBF文件,可以使用VBScript编写一个简单的脚本。这个脚本将打开DBF文件,并将其内容加载到Excel的新工作簿中。请注意,这个脚本是一个初步版本,它强制Excel不进行数据转换,这意味着它适合于查看文件,但可能需要进一步的改进才能完美。
要使用这个脚本,可以将其放置在桌面上,然后将DBF文件拖放到脚本上。脚本将打开Excel并加载DBF文件中的数据到一个新的工作表中。
如果对拖放脚本、宏脚本编程感兴趣,并希望成为Excel高手,可以查看书《Baby Steps - 如何不费吹灰之力成为Excel高手》。页面底部有更多信息的链接。
Option Explicit
Dim inputFile, path, fileName, tableName
Dim rs, fieldVals, i, myExcel, myWorkBook, mySheet, row, column
Const adOpenDynamic = 2
Const adLockPessimistic = 2
Const adCmdTable = 2
Const adOpenForwardOnly = 0
inputFile = WScript.Arguments.Item(0)
path = Split(inputFile, "\")
fileName = path(Ubound(path))
path(Ubound(path)) = ""
path = Join(path, "\")
tableName = Left(fileName, Len(fileName) - 4)
Dim dBConn
Set dBConn = OpenDBFConn(path)
Set rs = CreateObject("ADODB.Recordset")
rs.Open tableName, dbConn, adOpenForwardOnly, adLockPessimistic, adCmdTable
Set myExcel = CreateObject("Excel.Application")
Set myWorkBook = myExcel.Workbooks.Add()
Set mySheet = myWorkBook.Sheets(1)
myExcel.Visible = TRUE
rs.MoveFirst
Dim field
row = 1
column = 1
For Each field In rs.Fields
mySheet.Cells(row, column).Value = field.Name
WScript.Echo field.type
column = column + 1
Next
row = 2
Redim fieldVals(rs.Fields.Count - 1)
While Not rs.EOF
column = 0
For Each field In rs.Fields
fieldVals(column) = "=" & field.Value & ""
column = column + 1
Next
mySheet.Range(mySheet.Cells(row, 1), mySheet.Cells(row, column)).Formula = fieldVals
row = row + 1
rs.MoveNext
Wend
rs.Close
WScript.Echo "Loading Finished"
Excel 2007似乎也不再支持DBF格式。这里有一个简单的脚本可以将Excel文件(.xls、.xlsx、.csv等)转换为DBF格式。这个脚本还有很长的路要走才能变得完美。最糟糕的是,这个实现只存储VARCHAR(64)类型的数据。可能会尝试制作一个稍微更友好的版本。
这个脚本使用拖放脚本技术,正如在书《Baby Steps - 如何不费吹灰之力成为Excel高手》中所解释的。如果想了解如何用Excel征服世界,请查看博客页面底部的链接。
Option Explicit
Dim inputFile, path, fileName, tableName, createTable
inputFile = WScript.Arguments.Item(0)
path = Split(inputFile, "\")
fileName = path(Ubound(path))
path(Ubound(path)) = ""
path = Join(path, "\")
Dim dBConn
Set dBConn = OpenDBFConn(path)
tableName = Split(fileName, ".")
tableName(Ubound(tableName)) = ""
tableName = Join(tableName, ".")
tableName = Left(tableName, Len(tableName) - 1)
Open Excel and scan each spreadsheet
Dim myExcel, myWorkbook, mySheet, nColumns, column
Dim fields, row, scan, thisTableName, sheetCount
Dim createString, i
Set myExcel = CreateObject("Excel.Application")
myExcel.Visible = TRUE
Set myWorkbook = myExcel.Workbooks.Open(inputFile)
sheetCount = 1
For Each mySheet In myWorkbook.Sheets
'Get number of fields from column headers
scan = mySheet.Rows(1).Value
For nColumns = 1 To UBound(scan, 2)
If IsEmpty(scan(1, nColumns)) Then Exit For
Next
nColumns = nColumns - 1
If nColumns > 0 Then
thisTableName = tableName & "_" & sheetCount
createString = "CREATE TABLE " & thisTableName & "("
For i = 1 To nColumns
createString = createString & "["
& Replace(scan(1, i), " ", "_") & "] VARCHAR(64) "
If Not i = nColumns Then createString = createString & ", "
Next
createString = createString & ")"
On Error Resume Next
dbConn.Execute "Drop Table " & thisTableName
On Error Goto 0
WScript.Echo createString
dBConn.Execute createString
'Now we have the table, let us write to it
Dim rs, fieldPos, fieldVals
Redim fieldPos(nColumns - 1)
Redim fieldVals(nColumns - 1)
For i = 0 To nColumns - 1
fieldPos(i) = i
Next
Set rs = CreateObject("ADODB.Recordset")
Const adOpenDynamic = 2
Const adLockPessimistic = 2
Const adCmdTable = 2
rs.Open thisTableName, dbConn, adOpenDynamic, adLockPessimistic, adCmdTable
For row = 2 To 1048576
scan = mySheet.Rows(row).Value
For i = 1 To nColumns
If Not IsEmpty(scan(1, i)) Then Exit For
Next
'Blank row found
If i > nColumns Then Exit For
For i = 0 To nColumns - 1
fieldVals(i) = scan(1, i + 1)
Next
rs.AddNew fieldPos, fieldVals
Next
rs.Close
End If
sheetCount = sheetCount + 1
Next