LCFILENAME=GETFILE("XLS,XLSX","文件名","确定",0,"请选择EXCEL文件")
IF EMPTY(LCFILENAME)
MESSAGEBOX("未选择(未找到需要的) EXCEL 文件",64,"系统信息")
QUIT
ENDIF
DBFNAME = PUTFILE("保存为(&N):",JUSTSTEM(LCFILENAME)+".DBF","DBF")
IF EMPTY(DBFNAME)
MESSAGEBOX("DBF文件名不能为空",64,"系统信息")
QUIT
ENDIF
OEXCEL=CREATEOBJECT('Excel.application')
OEXCEL.WORKBOOKS.OPEN('&lcFileName') &&打开文件
I=1
***************获得表的可用SHEET*************************************
FOR EACH OMYVAR IN OEXCEL.SHEETS
DIMENSION A(I)
A(I)=OMYVAR.NAME
I=I+1
NEXT OMYVAR
*********检查D:\ZHAOAI.XLS是否存在,若存在,删除***********
IF FILE('D:\ZHAOAI.xls')
DELETE FILE D:\ZHAOAI.XLS
ENDIF
R=OEXCEL.SHEETS(A(1)).USEDRANGE.ROWS.COUNT &&有数据的总行数
C=OEXCEL.SHEETS(A(1)).USEDRANGE.COLUMNS.COUNT &&有数据的总列数
OEXCEL.CELLS(R,C).SELECT &&选中数据
OEXCEL.SELECTION.NUMBERFORMATLOCAL = "@" &&把被选定的单元格设为文本格式
OEXCEL.COLUMNS.AUTOFIT &&让所有的列宽都自动调整
OEXCEL.DISPLAYALERTS=.F.
OEXCEL.ACTIVEWORKBOOK.SAVEAS('D:\ZHAOAI',39) &&另存为5.0的Excel
OEXCEL.ACTIVEWORKBOOK.SAVED=.T. &&不保存当前EXCEL表
OEXCEL.WORKBOOKS.CLOSE &&关闭表
OEXCEL.QUIT &&退出EXCEL
RELEASE OEXCEL &&释放变量
*********************************************************
IMPORT FROM D:\ZHAOAI.XLS TYPE XL5 &&导入到默认目录下的ZHAOAI.DBF表中
DELETE FILE D:\ZHAOAI.XLS&&删除表
*********************************************************
USE ZHAOAI
MA=FCOUNT()
DIMENSION B(MA),D(MA),E(MA),F(MA),C(MA)
BADCHARS = '/,-=:;!@#$%&*.?<>()[]\'+'+'+CHR(34)+CHR(39)+" "
FOR LA=1 TO MA
B(LA)=FIELD(LA)
SCATTER TO D(LA)
D(LA)=CHRTRANC(SUBSTR(ALLTRIM(D(LA)),1,10),BADCHARS,REPLICATE("_",LEN(BADCHARS)-1))
IF LEN(RIGHTC(D(LA),1)) = 1 AND ISLEADBYTE(RIGHTC(D(LA),1))
&& last byte is Double byte
D(LA) = SUBSTR(D(LA),1,9)
ENDIF
IF !ISALPHA(LEFT(D(LA),1))
D(LA) = 'C_'+D(LA)
C(LA)=D(LA)
ALTER TABLE ZHAOAI RENAME &B(LA) TO &C(LA)
ELSE
FOR LB=1 TO LA-1
IF C(LB)==D(LA)
IF LEN(D(LA))<7
D(LA)=ALLTRIM(D(LA)) +"_"+ALLTRIM(STR(LA))
ELSE
D(LA)="C_"+ALLTRIM(STR(LA))
ENDIF
ENDIF
NEXT
C(LA)=D(LA)
ALTER TABLE ZHAOAI RENAME &B(LA) TO &C(LA)
ENDIF
NEXT
GOTO RECNO(1)
DELETE
PACK
MESSAGEBOX('已成功转换'+(LCFILENAME)+'表','提示')
CLOSE ALL
IF FILE(DBFNAME)
DROP TABLE (DBFNAME)
ENDIF
RENAME ZHAOAI.DBF TO (DBFNAME)
QUIT