how to get data from an excel file?

Just starting out? Need help? Post your questions and find answers here.
gabriel
Enthusiast
Enthusiast
Posts: 137
Joined: Sat Aug 01, 2009 4:49 pm
Location: Beirut, Lebanon

how to get data from an excel file?

Post by gabriel »

Hello:

what's the easiest way to get or read data (some specific cells) from an excel worksheet?

thanks in advance
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: how to get data from an excel file?

Post by srod »

If the target system has Excel installed then you might use COMatePLUS (there are a few Excel demos in the package). Alternatively, use ODBC via PB's database library. You will need to set up a DSN connection to the Excel workbook in question (either through the control panel or through code which you can find in these forums) and then you just use PB's database functions as per normal.
I may look like a mule, but I'm not a complete ass.
gabriel
Enthusiast
Enthusiast
Posts: 137
Joined: Sat Aug 01, 2009 4:49 pm
Location: Beirut, Lebanon

Re: how to get data from an excel file?

Post by gabriel »

Yes , Excel is installed. I looked at examples coming with COMATEplus, it seems that all are for writing/creating excel files. I need example for reading it, is there such one?

Thank you
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: how to get data from an excel file?

Post by srod »

The basic demo also reads a couple of cells, albeit from a newly created worksheet. Altering it to load an existing workbook is simple enough.

I will add that the ODBC path is likely to lead to a solution which runs faster, much faster!
I may look like a mule, but I'm not a complete ass.
gabriel
Enthusiast
Enthusiast
Posts: 137
Joined: Sat Aug 01, 2009 4:49 pm
Location: Beirut, Lebanon

Re: how to get data from an excel file?

Post by gabriel »

I didn't succeed to find that demo, could you just indicate it's name.
Otherwise, I just need to open an existing excel file, and read some cells, if it's small and easy to you to do it with COMAte, I'll be very thankfull.
On the other hand, momentarily I'm saving the excel file as .csv file, then accessing the cells by reading it as text file, but this is not quite straightforward

Thanks anyway, your COMATE is fantastic
User avatar
PureLeo
Enthusiast
Enthusiast
Posts: 221
Joined: Fri Jan 29, 2010 1:05 pm
Location: Brazil

Re: how to get data from an excel file?

Post by PureLeo »

Hey srod, I'm trying your COMatePLUS and I'm getting the error when running the excel demo...

I don't have Excel installed, only OpenOffice... is that ok, or I need Excel to run it?
KIKI
Enthusiast
Enthusiast
Posts: 145
Joined: Thu Dec 28, 2006 11:49 am
Location: FRANCE

Re: how to get data from an excel file?

Post by KIKI »

PureLeo wrote:Hey srod, I'm trying your COMatePLUS and I'm getting the error when running the excel demo...

I don't have Excel installed, only OpenOffice... is that ok, or I need Excel to run it?
Hi Leo,
There's an example on this forum on how to use Open Office spreadsheets with comate Plus.
User avatar
Kiffi
Addict
Addict
Posts: 1362
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: how to get data from an excel file?

Post by Kiffi »

PureLeo wrote:or I need Excel to run it?
yes, you need an installed Excel to access Excel-Objects.

Greetings ... Kiffi
Hygge
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4326
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: how to get data from an excel file?

Post by Rook Zimbabwe »

Flype had PURExls
http://www.purebasic.fr/english/viewtop ... it=purexls

And you CAN read and write to xls sheets if you have them already made... ABBKlaus had something too as I recall but I think he abandoned it... Use the SEARCH on the forum or GOOGLE the forum for WRITE TO EXCEL and see what pops!

8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
gabriel
Enthusiast
Enthusiast
Posts: 137
Joined: Sat Aug 01, 2009 4:49 pm
Location: Beirut, Lebanon

Re: how to get data from an excel file?

Post by gabriel »

the old link for flype' PureXLS is dead, is there a new one?
User avatar
Kiffi
Addict
Addict
Posts: 1362
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: how to get data from an excel file?

Post by Kiffi »

gabriel wrote:the old link for flype' PureXLS is dead, is there a new one?
"Use the search, Luke"

http://www.purebasic.fr/english/viewtop ... LS#p330302

;-)

Greetings ... Kiffi
Hygge
gabriel
Enthusiast
Enthusiast
Posts: 137
Joined: Sat Aug 01, 2009 4:49 pm
Location: Beirut, Lebanon

Re: how to get data from an excel file?

Post by gabriel »

examples show always how to create an excel file, no one to get data from (just reading some cells)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: how to get data from an excel file?

Post by RASHAD »

Removed
Last edited by RASHAD on Mon Aug 23, 2010 5:04 pm, edited 1 time in total.
Egypt my love
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: how to get data from an excel file?

Post by Paul »

You can download some sample code and a sample excel sheet here, which uses PureBasic's built in database commands...
http://www.reelmedia.org/pureproject/FILES/readxls.zip

This is the code in the ZIP file:

Code: Select all

#ODBC_ADD_DSN =1 
#Database     =1
#Library      =1


Procedure MakeConnection(Driver.s,strAttributes.s)
  If OpenLibrary(#Library,"ODBCCP32.DLL")
    Buffer=AllocateMemory(Len(strAttributes))
    CopyMemory(@strAttributes,Buffer,Len(strAttributes))
    For x=0 To Len(strAttributes)-1
      If PeekB(Buffer+x)=Asc(";"):PokeB(Buffer+x,0):  EndIf
    Next
    result=CallFunction(#Library,"SQLConfigDataSource",0,#ODBC_ADD_DSN,@Driver,Buffer)
    FreeMemory(Buffer)
    CloseLibrary(#Library)
  EndIf
  ProcedureReturn result
EndProcedure



file$="my stuff.xls"

If MakeConnection("Microsoft Excel Driver (*.xls)","DSN=ConnectXLS;Description=Description For Purebasic Excel;FileType=Excel97;DBQ="+File$+";")
  If UseODBCDatabase()
    If OpenDatabase(#Database,"ConnectXLS","","",#PB_Database_ODBC)
      If DatabaseQuery(#Database,"select * from [my data$]")
        While NextDatabaseRow(#Database)
          id=0
          col1 =GetDatabaseLong(#Database,id):id+1
          col2$=GetDatabaseString(#Database,id):id+1
          Debug Str(col1)+"..."+col2$
        Wend
      EndIf    
      CloseDatabase(#Database)
    EndIf
  EndIf
EndIf

Image Image
gabriel
Enthusiast
Enthusiast
Posts: 137
Joined: Sat Aug 01, 2009 4:49 pm
Location: Beirut, Lebanon

Re: how to get data from an excel file?

Post by gabriel »

Thank you very much Paul:D
that's what I was searching, something really simple, compact and fast.

everything OK, except the first line of "my stuff.xls" is not read, any trick?
Post Reply