Page 1 of 1

The format CSV converter in DBF or XLS

Posted: Tue Aug 21, 2012 6:27 am
by Eugene Lutsenko
Whether there is in Alaska or eXpress ++ a function converting a CSV format in DBF or XLS (it is better in DBF)? If yes, that as it to use?

Re: The format CSV converter in DBF or XLS

Posted: Tue Aug 21, 2012 7:55 am
by Auge_Ohr
hi,

you can use SDFDBE or DELIMITED to create a CSV.

Re: The format CSV converter in DBF or XLS

Posted: Tue Aug 21, 2012 8:53 am
by Eugene Lutsenko
And if it is already created (CSV), how it will load into DBF?

Re: The format CSV converter in DBF or XLS

Posted: Tue Aug 21, 2012 11:11 am
by rdonnay

Code: Select all

// This function loads a CSV file into a work area.
// The top line of the CSV file must contain the field names that match the work area.

FUNCTION DC_CSV2WorkArea( cCsvFileName )

LOCAL nHandle, cLine, aTokens, aFields, lStatus, cFieldName, aStru, ;
      oRecord, cValue, i, nFound, nFieldType

nHandle := DC_TxtOpen( cCsvFileName )

IF  nHandle <= 0
  RETURN .f.
ENDIF

oRecord := DC_DbRecord():new()

cLine := DC_TxtLine( nHandle )
aFields := DC_TokenArray( cLine, ',' )
aStru := dbStruct()

DC_TxtSkip(nHandle,1)
DO WHILE !DC_TxtEof(nHandle)
  cLine := DC_TxtLine(nHandle)
  aTokens := DC_TokenArray(cLine,',')
  dbGoTo(0)
  DC_DbScatter(oRecord)
  FOR i := 1 TO Len(aFields)
    IF !Empty(cFieldName := Upper(Alltrim(aFields[i]))) .AND. Len(aTokens) == Len(aFields)
      IF IsFieldVar(cFieldName)
        nFound := AScan(aStru,{|a|Upper(Alltrim(a[1]))==cFieldName})
        cValue := Alltrim(aTokens[i])
        IF nFound > 0
          nFieldType := aStru[nFound,2]
          IF nFieldType $ 'CM'
            oRecord:&(cFieldName) := cValue
          ELSEIF nFieldType == 'N'
            oRecord:&(cFieldName) := Val(cValue)
          ELSEIF nFieldType == 'L'
            oRecord:&(cFieldName) := ' ' + Upper(Alltrim(cValue)) + ' ' $ ' Y YES .T. TRUE T '
          ELSEIF nFieldType == 'D'
            oRecord:&(cFieldName) := CtoD(cValue)
          ENDIF
        ENDIF
      ENDIF
    ENDIF
  NEXT
  DC_DbGather(oRecord,.t.)
  DC_TxtSkip(nHandle,1)
ENDDO

DC_TxtClose( nHandle )

RETURN .t.

Re: The format CSV converter in DBF or XLS

Posted: Tue Aug 21, 2012 12:40 pm
by Eugene Lutsenko
I understand that I have a bad balance meanwhile that I receive at a forum and that I give on it: I receive much, and I give practically a zero. But nevertheless you couldn't provide the program which would address to the given function. I had a question: the database in which information from the CSV file will register, should be created to the appeal to this function? Whether if yes, that is possible to define automatically its structure and how it to make?