把Excel导入到VFP数据库中,生成DBF数据库文件,其通用代码如下:
注意如下几个条件:
①EXCEL文件必须保留字段名,不能只有数据行,否则第一行数据将作为字段处理
②不管Excel原文件有多少个列,导入之后,会自动生成对应的列名称。
TmpDbfFile = "F:\school\TmpTestDbf.dbf"
ExcelObject = CREATEOBJECT("excel.application")
ExcelFile = ExcelObject.APPLICATION.Workbooks.OPEN(GETFILE("xls"))
ExcelFile.activesheet.usedrange.columns.AutoFit &&这步不可省略,否则未展开的列数据取不全
ExcelFile.saveas(TmpDbfFile,11)
ExcelFile.saved = .t.
ExcelFile.close
SELECT * FROM &TmpDbfFile IN TO CURSOR MyTmp
USE IN TmpTestDbf
DELETE FILE &TmpDbfFile
brow
select * from MyTmp in to dbf MyResult.dbf
use mydb
APPEND FROM 123 TYPE xl5
假如担心字段不对应的话,可以先建一个临时表与EXCEL的字段一样
这样再利用 IN SERT INTO MYDB () SELECT XXX FROM TMP 就可以了
如下是已测试成功不存在任何问题的代码了
TmpDbfFile = "F:\school\TmpTestDbf.dbf"
ExcelObject = CREATEOBJECT("excel.application")
ExcelFile = ExcelObject.APPLICATION.Workbooks.OPEN(GETFILE("xls"))
ExcelFile.activesheet.usedrange.columns.AutoFit &&这步不可省略,否则未展开的列数据取不全
ExcelFile.saveas(TmpDbfFile,11)
ExcelFile.saved = .t.
ExcelFile.close
SELECT * FROM &TmpDbfFile IN TO CURSOR MyTmp
USE IN TmpTestDbf
DELETE FILE &TmpDbfFile
brow
select * from MyTmp in to dbf MyResult.dbf
use myresult
for i=1 to 1401
go i
if Zk4="0.0"
replace Zk4 with "0"
endif
next
browse
use test
for i=1 to 1383
go i
replace hkkh with 92621020000 + i
next
use test
for i=1 to 1382
go i
if len(alltrim(hk))=4
replace hk with alltrim(hk) + "县"
endif
next
browse