Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Excel API for C/C++



  • Does anyone know of a platform independent C or C++ library/API for reading and writing Excel™ files?

    The native API from Microsoft is not an option, as it might not be installed on every machine we target nor is it available on Macs.

    I know of jexcelapi for Java, but never heard of a similar lib for our beloved C++ :)



  • KSpread (part of KOffice) reads/writes Excel files. KSpread uses Qt (and KDE libs).

    Have you checked to see how they do it?



  • Do you really need Excel output, or do you just need to output data that Excel can read?

    If the latter is enough, considder a simpler format like csv. Otherwise, you may want to investigate either the .ods or the .xlsx format. Both are packed structures with XML files, but they are not simple I have heard. I never worked with them directly.

    A last option that may be workable is to go through ODBC and QtSql. You can read and write Excel files that way, but of course there are limits to what you can do there.



  • I've found some infos on the Excel 2003 XML format on "this page":http://blogs.msdn.com/b/brian_jones/archive/2005/06/27/433152.aspx. Could be a good start.

    CSV ist not an option, it's very cumbersome to read and write correctly. And the users receiving the CSV must take care of the correct settings (charset, delimiter, enclosing quotes and all that). That's error prone and the support for it is a nightmare - we all are developers, aren't we? :-) Also we need it in both directions (import Excel sheets of our users into our database and generate spread sheets for the users filled with our data).

    Unfortunately we also must support opening Excel files in the native (i.e. non-XML) format, otherwise we would had to convert them by hand. Giving xlsx to our users would be ok.



  • We can have Jexcelapi on the server using java. No problem with that. This way we have to put everything to the server, but I want do do some things in the Qt/C++ based client.



  • [quote author="Volker" date="1291507360"]Unfortunately we also must support opening Excel files in the native (i.e. non-XML) format, otherwise we would had to convert them by hand. Giving xlsx to our users would be ok.[/quote]

    I believe Microsoft has a tool for converting non-XML to XML. You may be able to use it in a batch process.

    So you only need to get info out? You don't need to be able to edit it and save it? There are quite a number of tools to convert XML files in to other formats, I believe.



  • You did read of that "platform independent" thingy, didn't you? :) To be able to work on a Mac I cannot use the Microsoft tools. And of course that tool exists, it's called Excel :)



  • I think it's a downloadable tool for people with old versions of Excel actually. Standalone. But yes, it was a Windows app.

    You have Excel on "Mac":http://www.ehow.co.uk/how_6316841_convert-xls-xlsx-mac.html too :P

    There may be a Mac app for this. Or you could try wine (or similar).

    Probably make life much easier.



  • Come on... you cannot force your commercial customers to install wine just to convert some crappy xls files :-)

    Everthing other than a lib I can incorporate to my app is not acceptable; it's more pleasent for the user if we upload the files to the business logic server then.



  • Alright, I guess since it is for paying customers it is best to treat them nice.

    Good luck with your Excel API. I'm sure someone has written something like it for Qt somewhere. You checked Qt-Apps already and similar sites?

    This can search Excel files: http://qt-apps.org/content/show.php/recoll?content=66138
    But might not have the full API.



  • I've searched around with google, got some things that might be interesting, but nothing with using Qt in the first search results. It's not on a very high priority at the moment, so no need to worry. I thought it's worth a try in this forum - if you don't ask, you won't get an answer...



  • There are 2 C++ libaries to create excel.
    One is free xlslib : http://xlslib.sourceforge.net/
    Other is commercial libXL : http://www.libxl.com/

    Sorry for late response and I don't know whether you need this or not. But for others I share...



  • Thanks for the links. libxl I will have a look into. xlslib unfortunately cannot read XLS files. This is the crucial part. Writing the files could be quite easily approached with the new XML based format (thats seems not too complicated as long as there are no formulas or cross references, which we do not need).



  • Another available option is to attempt to extract the code from an open source excel reading competitor.

    Open Office - http://www.openoffice.org/ - is likely to be used,

    Gnumeric is probably a better option.
    Excel code is here - http://git.gnome.org/browse/gnumeric/tree/plugins/excel



  • Libxls
    libxls is a C library which can read Excel (xls) files. It has been tested on Linux x86 and Cygwin but may work on any little-endian system. ...
    "libxls.sourceforge.net":http://libxls.sourceforge.net

    [EDIT: clickable link, Volker]



  • Thanks for the link Yash, it looks promising. It could be a good start and maybe I find some time to make a Qt version.



  • What's about KOffice? As far as I know they use MS -> OOo -> KOffice, but they might have a cuter API to the OOo routines since they are Qt-based.



  • KOffice falls out due to license issues - unfortunately.



  • [quote author="Yash" date="1293597766"]Libxls
    [EDIT: clickable link, Volker][/quote]
    Not yet....

    "libxls.sourceforge.net":http://libxls.sourceforge.net



  • Thanks - corrected * 2 :-)



  • Has anybody figured out what kind of license is? GPL, LGPL, ... ?

    I would like to include all the "open source" tools/libraries we talk about into wikis so when needed one can find them in one place.

    EDIT: What are the KOffice license issues?



  • From http://www.koffice.org/contribute/:

    bq. KOffice is released under the LGPL v2+ (and GPL v2+ for some parts).



  • [quote author="Volker" date="1294273445"]From http://www.koffice.org/contribute/:

    bq. KOffice is released under the LGPL v2+ (and GPL v2+ for some parts).[/quote]

    Why was that a problem then? Can't get much more flexible than that (unles you are looking for the one part that is only GPL and you want to release a closed source app).

    EDIT: before I get screamed at: warning: I don't have any idea of the details about "v2+"



  • I'd have to extract the parts I need, remove the KDE stuff (not available on Windows and Mac) and publish it as a new LGPL project. We do not have the time for that at the moment and it's not on top of the want-have list of the customer.



  • I see.... so the issue is not exactly with the license...



  • The license is one of the issues. Nevermind, I have some hints I will dig into further if I ever have some spare time.



  • Volker, as your successes with excel?
    I use libxls, but some excel's files in russian language not read correctly (I realized because of the inability to convert data from a file in UTF-16). If you have any achievements, can you tell us about them?



  • I did not look into libxls yet, so sorry, no advice from my side.



  • is there also something for word documents (*.doc or *.docx)?



  • You could maybe take a look at "http://www.libxl.com/":http://www.libxl.com/



  • [quote author="mogz" date="1309159483"]You could maybe take a look at "http://www.libxl.com/":http://www.libxl.com/[/quote]
    That lib was already mentioned on the first page of this topic.



  • well in its documentation it says it works for excel files only. i need word documents. or didnt i see the word classes?



  • oops! the post was meant for the main title. guess i'll have to take my time next time. sorry for that



  • Hi,

    a lot of development has been done during the last year on the Calligra Suite filters. Calligra Suite is the fork of KOffice. You can use the koconverter to do batch conversions to ODF. However the excel filter does not follow the koconverter approach at moment. Please discuss at the #calligra channel at
    freenode.

    Consider that any of the binary files in {.doc, .ppt, .xls} use the Compound File Binary File Format.
    On top of it any graphics are stored in containers described by the MS-ODRAW spec. You can reuse libmso from Calligra Suite to parse and convert MS-ODRAW content to ODF. libmso makes use of msoscheme (http://gitorious.org/msoscheme) to parse the MS-ODRAW content. Also MS-PPT containers are parsed by msoscheme.

    Office Open XML support has been also improved a lot. Use the #calligra channel to discuss the details.



  • Does anyone try using QSqlDatabase to do the work?



  • [quote author="flight9" date="1333679679"]Does anyone try using QSqlDatabase to do the work?[/quote]

    You would need an SQL driver for that. I don't know of any one - do you do?



  • [quote author="flight9" date="1333679679"]Does anyone try using QSqlDatabase to do the work?[/quote]
    What are you refering to now exactly? The original question (Excel), or something else?

    Because for Excel, you can actually use the QODBC drivers if you want.



  • You migt to try use OpenDocument writer supported by Qt and than convert it to Excel file with OpenOffice from command line (bacground process). Open/LibreOffice is widley present on main platforms ... unless it is mobile layer.

    I have not used OpenDocument but I have sucesfully converted many OO files to Excel.
    This method was not very fast - maybe now it will be ok .. but results were good. At least there is not much work to be done to test it.

    There is lot of resources how to do it in many ways using O/LO by use of extra macros or python but good statring point is soffice --help. There is --convert command that should work for this task.



  • I have just find it and checked:

    @soffice --invisible -convert-to xls filename.xlsm -> filename.xls
    soffice --invisible -convert-to xls test.ods -> test.xls
    @

    Works like a charm ... I do not have any complicated files but it take about 1s to convert simple one page document.



  • Hi Volker,

    Have you figure out the problem to Execl API for QT/C/C++?
    

    How did/will you to get it done? I met the same problem with you. It had
    trappedd me for days . I'm looking for your sharing or tips.
    Of course, if someone have any idea or process, sharing or tips is apreciated.


Log in to reply