Recent

Author Topic: how to Import/export Data from/to excel or calc ?  (Read 17215 times)

lkhalid

  • Newbie
  • Posts: 5
how to Import/export Data from/to excel or calc ?
« on: September 04, 2013, 12:30:21 am »
hi ,

i want to create a database application using lazarus .
the condition is the ability to export some tables from that database to excel or calc manually /or automatically(by code)
and to import some data from excel or calc to my database application (manually /or automatically(by code))

i dont know wish database to choose :
1-firebird
2-sqlite
3-mysql
4-...

any help will be very appreciated for a beginner  :)
Thanks.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: how to Import/export Data from/to excel or calc ?
« Reply #1 on: September 04, 2013, 12:51:56 am »
The easy way out is to use .csv files to export and import the data you need. Excel can open or import those directly and I think calc supports the format too. the server you are going to use plays no part on the job described so choose any one that fulfills all your other technical specifications.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: how to Import/export Data from/to excel or calc ?
« Reply #2 on: September 04, 2013, 04:26:01 am »
:-)   Are you a beginner to databases, or just a beginner to Lazarus?

As @taazz has said, CSV is a good place to start.  You can use the SDFLaz dataset to connect to a CSV file, then use a TDataSource and TDBGrid to display the contents of your CSV file.  Exporting to Excel comes cheap for you this way as well, Excel can open CSV files directly :-)

A CSV file though is a only a Table.  You want multiple tables, you're going to need a relational database, and here you need to work out what your requirements. 
  • Something small with minimal install issues?  SQLLite. 
  • Something scalable and needing to deployed on a client servers and supported by client IT?  MSSQL or Oracle. 
  • Cross platform?  MySQL (MariaDB). 
  • Programming for fun?  MySQL (MariaDB) (I'm not saying this engine is only for hobbyists, I'm saying I genuinely find programming with MySQL fun, working with MSSQL and Oracle can be like banging your head against a wall sometimes).

And these are only my opinions, others will give you different answers to the same questions.  You'll also spot I've never used Firebird or Interbase, so a bit clueless there :-)

Exporting from the database to Excel is done in code.  I've never used it, but FPSpreadsheet seems to be the way to go.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: how to Import/export Data from/to excel or calc ?
« Reply #3 on: September 04, 2013, 06:23:39 am »
« Last Edit: September 04, 2013, 06:25:51 am by exdatis »

lkhalid

  • Newbie
  • Posts: 5
Re: how to Import/export Data from/to excel or calc ?
« Reply #4 on: September 04, 2013, 11:51:57 am »
thank you guys
csv will be my last resort as it is only a table !

i was coding in vb.net using access database so i was able to open my .mdb and paste all ready data in a table or vice versa .

Let's say that the program is a school application that contain at least 30 tables(students,teachers,school absence,notes,courses ...) .and some times we need to have one of them exported to a simple format (excel)
sometimes we also receive data in that simple format (excel) and we must import them to our database (a new students list for Ex)

i am beginner , coding for fun but i can learn all what's necessary to the job .

All what i need is to choose a database that can be edited easily
i'am under ubuntu but if we didnt find a cross platform solution i can work also under windows .


taaz,Mike.Cornflake,exdatis
thanks you for your answers

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: how to Import/export Data from/to excel or calc ?
« Reply #5 on: September 04, 2013, 12:20:39 pm »
Quote
i am beginner , coding for fun but i can learn all what's necessary to the job .

Excellent :-)    Welcome to Lazarus/FreePascal!

If you want to stick with Access, then I'm uncertain what to recommend.  ODBC driver probably.

So, I recommend MySQL :-)  You're on Ubuntu, so installing the engine will be easy enough, should be in software centre I think it's called.  I also recommend a database viewer for you to test with.  You may need to look around, not sure what to recommend under Ubuntu, been too long.  Under Windows I use XAMPP, which includes Apache Web Server, and a MySQL administration plugin.

I'd then suggest you use the Database Admin tool to add a database and a few tables, get some test data in there.

Good write up in the wiki (see link below) on how to use Lazarus to connect to MySQL.  Read the bit immediately below the heading, it's important :-)  Lots of people miss the fact that they need to add the dlls (in your case .so files) to both the Lazarus exe dir and your project exe dir.

http://wiki.freepascal.org/mysql#Simple_MySQL_Demo_Using_the_TMySQL5xConnection_Component

Don't read the stuff above the heading I've linked to, it's scary :-)  Or feel free to read it, but be assured you don't need to do that low level programming if you don't want to :-) It's all hidden behind the TMySQL5xConnection components.

Finally, to export data out to Excel, FPSpreadsheet is the place to go.

http://wiki.freepascal.org/FPSpreadsheet

Conceptual code has already been written for you, you'll just need to modify it to suit :-)

http://wiki.freepascal.org/FPSpreadsheet#Converting_a_database_to_a_spreadsheet

Have fun :-)
« Last Edit: September 04, 2013, 12:30:24 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: how to Import/export Data from/to excel or calc ?
« Reply #6 on: September 04, 2013, 12:31:10 pm »
Personaly I would go for firebird database and its embedded server, you need nothing more to use it as a single user database or to install the server on a machine and let every one connect to it from any OS that is supported by firebird. as for the excel support there are a number of components that can be used most of them are written for delphi but since none of them have any visual components they probably can be used from lazarus as is there is also the fpspreadsheet component pack which you can find at https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/fpspreadsheet that is a lazarus specific implementation.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: how to Import/export Data from/to excel or calc ?
« Reply #7 on: September 04, 2013, 01:51:51 pm »
Another vote for Firebird (embedded), or perhaps sqlite. Flamerobin is a nice database viewer/management tool for Firebird.

I'd stay away from mysql, if only due to the need to match FPC connection component version with mysql .so/.dll client version.

Advantage of Firebird is that you can use it in single user mode (embedded) or multiple user (client/server) without any code changes.

Data/import export could be done with fpspreadsheet, yes.
For export, there's also http://wiki.lazarus.freepascal.org/fpXMLXSDExport (e.g. export to XML that can be imported with MS Access) but exporting to .xls format is probably easiest in practice.

Re mysql:
Quote
Lots of people miss the fact that they need to add the dlls (in your case .so files) to both the Lazarus exe dir and your project exe dir.
I thought copying dlls was a Windows-only phenomenon? Shouldn't .so's be in the library search path (typically different from the executable directory)?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: how to Import/export Data from/to excel or calc ?
« Reply #8 on: September 04, 2013, 02:03:25 pm »
Quote
I thought copying dlls was a Windows-only phenomenon? Shouldn't .so's be in the library search path (typically different from the executable directory)?

You know what they say, Assumption makes an ASS out of U and MPTION :-)  I'm very probably wrong, haven't connected to MySQL under Linux...
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: how to Import/export Data from/to excel or calc ?
« Reply #9 on: September 07, 2013, 09:53:03 am »
Oh, by the way, I'm working on something a bit similar: reading in data, converting it, and exporting it to Excel, CSV, MS Access, Firebird embedded etc.

Currently the program uses in-memory recordsets, but the export code surely could be usable. The relevant code is in outputparserunit.pas

See https://bitbucket.org/reiniero/db2securityscript/ (directory outputparers; the top directory contains a script for the DB2 database; the outputparser program can process the results from the script).

See also
http://wiki.lazarus.freepascal.org/Projects_using_Lazarus#DB2_Security_Script_importer

Thanks,
BigChimp
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018