共用方式為


使用 Visual FoxPro 將具有其他欄位類型的備忘欄位匯出至文字檔

本文將說明如何將包含備忘欄位及其他欄位類型的記錄,動態匯出為逗號分隔的文本檔案。

原始產品版本: Visual FoxPro
原始 KB 編號: 241424

摘要

本文示範如何將包含備忘欄位和其他欄位類型的記錄,以動態方式匯出到逗號分隔的文本檔中。

其他相關資訊

本文中的範例程式代碼遵循下列順序:

  1. 提示輸入源數據表(.dbf 檔案文件)。
  2. 提示輸入目標文字檔(.txt檔案)。
  3. 將數據表的內容匯出至文字檔。

程式代碼會掃描並判斷數據表中的每個欄位類型和每一筆記錄,然後據以數據類型進行處理:

CLOSE ALL
CLEAR ALL

lcFieldString = ''
lcMemo = ''

USE GETFILE('dbf', 'Select DBF') && Prompts for table to be used.

lnFieldCount = AFIELDS(laGetFields) && Builds array of fields from the
&& selected table.

*!* Prompt for Output file and use Low-Level functions
*!* to create it.
lcTextFile = FCREATE(GETFILE('txt', 'Select Text'))

*!* Starts scanning the table and converts the fields
*!* values according to their types **
SCAN
    WAIT WINDOW STR(RECNO()) + ' Of ' + STR(RECCOUNT()) NOWAIT

    FOR lnCount = 1 TO lnFieldCount
        lcType = laGetFields(lnCount, 2)

        IF lcType # 'G' && Don't try to turn a general field into a string
            lcString = EVALUATE(laGetFields(lnCount, 1))
        EndIf

        DO CASE
            CASE lcType = 'M' && Process the Memo Fields
                lnMemoLines = MEMLINES(EVALUATE(laGetFields(lnCount,1)))
                FOR lnLoop = 1 TO lnMemoLines
                    IF lnLoop < lnMemoLines
                        lcMemo = lcMemo + ;
                            ALLTRIM(MLINE(EVALUATE(laGetFields(lnCount, 1)), ;
                    lnLoop)) + ' '
                    ELSE
                        lcMemo = lcMemo + ;
                            ALLTRIM(MLINE(EVALUATE(laGetFields(lnCount, 1)), ;
                    lnLoop))
                    ENDif
                ENDfor

                lcString = lcMemo
                lcMemo = ''
            CASE lcType = 'G' && Process the General Fields
                lcString = 'Gen'
            CASE lcType = 'D' && Process the Date Fields
                lcString = DTOC(lcString)
            CASE lcType = 'T' && Process the DateTime Fields
                lcString = TTOC(lcString)
            CASE lcType = 'N' && Process the Numeric Fields
                lcString = STR(lcString, LEN(STR(lcString)), 2)
            CASE lcType = 'I' && Process the Integer Fields
                lcString = STR(lcString)
            CASE lcType = 'L' && Process the Logical Fields
                IF lcString = .T.
                    lcString = 'T'
                ELSE
                    lcString = 'F'
                ENDif
        ENDcase

        IF lnCount < lnFieldCount && Determines if the last field was
        && processed and sets the closing quote.
        lcFieldString = lcFieldString + '"' + lcString + '"' + ','
        ELSE
        lcFieldString = lcFieldString + '"' + lcString + '"'
        ENDif
    ENDfor

    FPUTS(lcTextFile, lcFieldString) && Writes string to the text file.
    lcFieldString = ''
ENDscan

FCLOSE(lcTextFile)

CLOSE All
CLEAR All
WAIT WINDOW 'Text File Creation Completed' NOWAIT