以下是引用TonyDeng在2012-8-18 12:38:16的发言:
直接从.TXT文件中创建表并把数据转录过去就可以了
我现在就是想知道转录过去的方法,谢谢
直接从.TXT文件中创建表并把数据转录过去就可以了
CLEAR ALL CLOSE DATABASES ALL CLEAR SET DECIMALS TO 4 * CreateDataDictionary() Import_Table("53.TXT", "53") CLOSE DATABASES ALL CLEAR ALL RETURN * 导入数据 FUNCTION Import_Table(tcDataFileName, tcTableName) LOCAL lnHandle, lnLine, lcFieldName, lnIndex, lnValue LOCAL lnFields, lnRecords LOCAL llSuccess OPEN DATABASE DATA1 EXCLUSIVE * 打开数据文件 lnHandle = FOPEN(tcDataFileName) IF lnHandle < 0 MESSAGEBOX("数据文件" + tcDataFileName + "打开失败!", 16) RETURN ENDIF * 跳过前面9行 FOR lnLine = 1 TO 9 FGETS(lnHandle) NEXT * 获取字段数 lnFields = INT(VAL(FGETS(lnHandle))) * 创建数据表结构 SET SAFETY OFF USE Dictionary EXCLUSIVE IN 0 IF INDBC("_" + tcTableName, "TABLE") REMOVE TABLE ("_" + tcTableName) ENDIF COPY STRUCTURE EXTENDED TO ("_" + tcTableName) DATABASE Data1 USE ("_" + tcTableName) ALIAS _Structure EXCLUSIVE IN 0 ZAP IN _Structure llSuccess = .T. FOR lnLine = 1 TO lnFields lcFieldName = ALLTRIM(FGETS(lnHandle)) SELECT Dictionary LOCATE ALL FOR ALLTRIM(Dictionary.Field_Name) == lcFieldName IF !FOUND("Dictionary") MESSAGEBOX("发现数据字典中没有的字段:" + lcFieldName, 16) llSuccess = .F. EXIT ENDIF SELECT _Structure APPEND BLANK REPLACE _Structure.Field_Name WITH Dictionary.Field_Name REPLACE _Structure.Field_Type WITH Dictionary.Field_Type REPLACE _Structure.Field_Len WITH Dictionary.Field_Len REPLACE _Structure.Field_Dec WITH Dictionary.Field_Dec NEXT USE IN _Structure USE IN Dictionary * 创建导入数据表 SET SAFETY OFF IF INDBC(tcTableName, "TABLE") REMOVE TABLE (tcTableName) ENDIF SELECT 0 CREATE (tcTableName) DATABASE Data1 FROM ("_" + tcTableName) USE * 导入数据 USE (tcTableName) ALIAS _Data EXCLUSIVE IN 0 USE ("_" + tcTableName) ALIAS _Structure EXCLUSIVE IN 0 * 获取记录数 lnRecords = INT(VAL(FGETS(lnHandle))) FOR lnLine = 1 TO lnRecords SELECT _Data APPEND BLANK lcLineContent = FGETS(lnHandle, RECSIZE("_Data")) FOR lnIndex = 1 TO lnFields lcField = FIELD(lnIndex, "_Data") SELECT _Structure LOCATE ALL FOR UPPER(ALLTRIM(_Structure.Field_Name)) == lcField SELECT _Data DO CASE CASE _Structure.Field_Type == 'C' REPLACE (lcField) WITH LEFT(lcLineContent, _Structure.Field_Len) CASE _Structure.Field_Type == 'N' lnValue = VAL(LEFT(lcLineContent, _Structure.Field_Len)) / (10 ^ _Structure.Field_Dec) REPLACE (lcField) WITH lnValue ENDCASE lcLineContent = SUBSTR(lcLineContent, _Structure.Field_Len + 1) NEXT NEXT USE IN _Structure SELECT _Data BROWSE * 关闭数据文件句柄 FCLOSE(lnHandle) RETURN llSuccess ENDFUNC * 创建数据字典 PROCEDURE CreateDataDictionary() LOCAL lnHandle, lcLineContent LOCAL lcDelimiter lcDelimiter = ' ' && 界定符是空格 USE Dictionary_Structure EXCLUSIVE IN 0 SET SAFETY OFF ZAP IN Dictionary_Structure lnHandle = FOPEN("格式.TXT") DO WHILE !FEOF(lnHandle) lcLineContent = CHRTRAN(FGETS(lnHandle), CHR(9), lcDelimiter) IF !EMPTY(lcLineContent) APPEND BLANK IN Dictionary_Structure REPLACE Dictionary_Structure.Field_Name WITH GETWORDNUM(lcLineContent, 1, lcDelimiter) IN Dictionary_Structure REPLACE Dictionary_Structure.Field_Type WITH GETWORDNUM(lcLineContent, 2, lcDelimiter) IN Dictionary_Structure REPLACE Dictionary_Structure.Field_Len WITH INT(VAL(GETWORDNUM(lcLineContent, 3, lcDelimiter))) IN Dictionary_Structure REPLACE Dictionary_Structure.Table_Cmt WITH GETWORDNUM(lcLineContent, 4, lcDelimiter) IN Dictionary_Structure IF Dictionary_Structure.Field_Type == 'A' REPLACE Dictionary_Structure.Field_Type WITH 'C' ENDIF ENDIF ENDDO FCLOSE(lnHandle) USE IN Dictionary_Structure ENDPROC