Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting any Excel sheet to .DBF file

234 views
Skip to first unread message

arpan

unread,
Apr 14, 2010, 12:13:20 PM4/14/10
to
Hi,

I have a small project where database file needed and the records
comes from Excel sheet
Can any one help me to convert excel sheet to database file. I am now
using xharbour 1.1.0

Thanks

Ella

unread,
Apr 14, 2010, 2:27:48 PM4/14/10
to
Hello,

Below is a solution to read in an .xls sheet in an array - in case of
huge sheets I think preallocating the array is also necessary.

Ella


function main()

local a, i, j, cItem
local cFile:="C:\CNVS\excel\cp.xls", cTemp:="C:\CNVS"
local cSheetName:="Sheet1"

a:=adoxls2array( cFile, cTemp, cSheetName )

for i:=1 to len( a )
cItem:=""
for j:=1 to len( a[i] )
cItem+=" "+xstr( a[i][j] )
next
? cItem
next

return NIL

function adoxls2array( cFile, cTemp, cSheetName )

local oConn, oComm, oRs, oErr
local i:=0, nCols, cConn, aRs:=array(0), aRows

if .t. // the header ( HDR ) option can be set to yes or no
cConn:="Provider=Microsoft.Jet.OLEDB.4.0;"
cConn+="Data Source="+cFile+";"
cConn+='Extended Properties="Excel 8.0;HDR=No;IMEX=1";'
else // this driver can't identify a "header" record
cConn :="Driver={Microsoft Excel Driver (*.xls)};"
cConn+="DriverId=790;"
cConn+="Dbq="+cFile+";"
cConn+="DefaultDir="+cTemp+";"
endif

try

oConn:=CreateObject( "ADODB.Connection" )
with object oConn
:ConnectionString:=cConn
:Open()
end
oRs:=oConn:Execute( "select * from ["+cSheetName+"$]" )

with object oRs
nCols:=:Fields:Count
aRows:=array( nCols )
do while !:EOF
for i:=0 to nCols-1
aRows[i+1]:=:Fields[i]:Value
next
aadd( aRs, aclone( aRows ) )
:MoveNext()
enddo
end

oRs:Close()
oConn:Close()
oRs:=NIL
oConn:=NIL

catch oErr
alert( "Error: " + oErr:Operation + " " + oErr:Description )
end

return ( aRs )


Robert Campsmith

unread,
Apr 15, 2010, 10:43:13 AM4/15/10
to
Thanks Ella,

Do you have a definition for Function xStr( uVar ) ??

Thanks,
-- Robert

Robert Campsmith

unread,
Apr 15, 2010, 10:50:51 AM4/15/10
to
Hello Ella,

I tried your magic black box.

Microsoft Excel sent me this message...
'TEXTX.DBF' cannot be accessed. The file may be read-only, or you
may be trying to access a read-only location. Or, the server the
document is stored on may not be responding.
Retry Cancel

The full path of the output filename is
s:\sturec\Adrian\Import\testx.dbf

Thanks,
-- Robert

Robert Campsmith

unread,
Apr 15, 2010, 11:02:30 AM4/15/10
to
Hello Ella,

I got your routine to work, by cleverly passing it the correct
parameters, but my source spreadsheet which consisted of 189 rows
by 8 columns came out as 3 rows by 3 columns.

What part may I be missing?

Thanks,
-- Rwc

Robert Campsmith

unread,
Apr 15, 2010, 11:50:58 AM4/15/10
to
Ella,

Kindly disregard previous three posts from me.

Your blackbox pops up an Alert box instantly with the message
"Error: EXECUTE S_OK" and the button Ok I took this to mean that
it executed without errors (and very quickly).

However, no exported .dbf file is created anywhere.

The function "adoxls2array()" is supposed to return an array, but
the array that is returned is zero length.

Maybe I do not understand the comment


// the header ( HDR ) option can be set to yes or no

Since .t. is always True, the first half of the IF statement
probably always executes. So I tried setting the .t. to .f. and
got the same result - no exported .dbf file.

Thanks,
-- Robert

Ella

unread,
Apr 15, 2010, 12:55:59 PM4/15/10
to
Robert,

I'm learned with xstr(...) function in VxH, but you can use cstr(...)
instead.
The idea is to convert whatever cell content to a string, even if it's
undefined ( NIL )

Here cFile should be set as complete file name with absolute path of
an existing Excel sheet.

> >    local cFile:="C:\CNVS\excel\cp.xls", cTemp:="C:\CNVS"


And here cSheetName should be set to an existing sheet ib cFile ( see
the tabs in the Excel workbook )

> >    local cSheetName:="Sheet1"
>

Here the content of the sheet is read in a 2D array, and is the
programmer's option, what to do with the data ( maybe he'she wants to
filter the content, and to save certain columns, each converted to a
predefined datatype )

> >    a:=adoxls2array( cFile, cTemp, cSheetName )

>
> >    for i:=1 to len( a )
> >       cItem:=""
> >       for j:=1 to len( a[i] )
> >          cItem+="  "+xstr( a[i][j] )
> >       next
> >       ? cItem
> >    next
>
> > return NIL
>
> > function adoxls2array( cFile, cTemp, cSheetName )
>
> >    local oConn, oComm, oRs, oErr
> >    local i:=0, nCols, cConn, aRs:=array(0), aRows
>

Here are two possible alternatives for a connection string:
- in the IF branch there is a connection string via OLEDB
- in the ELSE branch there is a connection string via ODBC

Some Excel sheets do have "header line", others not.

After deciding which is more appropiate, the options might need to be
adjusted to the concrete environment.


> >    if .t.   // the header ( HDR ) option can be set to yes or no
> >       cConn:="Provider=Microsoft.Jet.OLEDB.4.0;"
> >       cConn+="Data Source="+cFile+";"
> >       cConn+='Extended Properties="Excel 8.0;HDR=No;IMEX=1";'
> >    else    // this driver can't identify a "header" record
> >       cConn :="Driver={Microsoft Excel Driver (*.xls)};"
> >       cConn+="DriverId=790;"
> >       cConn+="Dbq="+cFile+";"
> >       cConn+="DefaultDir="+cTemp+";"
> >    endif
>

Personally I hate Office, because there are always problems with
extracting the data, when migrating to a normal database.


Ella

seandc

unread,
Apr 17, 2010, 2:09:48 PM4/17/10
to

Arpan:
Why not export the data from Excel in CSV format and read into dBase
format using APPEND FROM...DELIMITED WITH ","
Sean

0 new messages