Handling Microsoft Excel file format

Handling Microsoft Excel file format

TODO: If you know more about the container format, and whether it really needs a specialized library for processing, please expand this section.
TODO: Tips for displaying Excel documents which were manually parsed using one of the methods described.
TODO: If you know whether Excel provides a "viewer" ActiveX control that can be embedded in a Qt application through ActiveQt, please fill out this section (including links to relevant resources).

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

This page discusses various available options for working with Microsoft Excel documents in your Qt application. Please also read the general considerations outlined on the Handling Document Formats page.

One needs to distinguish between two different formats (this page deals with both of them):

 Legacy "Excel Spreadsheet" format"Office Open XML Workbook" format
classification:binary BIFF-basedXML-based
main filename extension:.xls.xlsx
main internet media type:application/vnd.ms-excelapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet
default format of Excel:until Excel 2003since Excel 2007

Reading / Writing

Using Excel itself

If you are exclusively targeting the Windows platform and Microsoft Excel will be installed on all target machines, then you can use Qt's ActiveX framework to access Excel's spreadsheet processing functionality through OLE automation. For an introductory code example (and a way to list the API provided by the Excel COM object), consult this how-to.

 DLL file nameCOM object nameplatformslicense
Microsoft Excel ?Excel.ApplicationWindowscommercial

Using ODBC

To read an Excel file with ODBC (tested on Windows 7 with QT 4.7.1 and Windows 10 with QT 5.7) :

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "xlsx_connection");
db.setDatabaseName("DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + QString("c:\\path\\to\\your\\file\\file.xlsx"));
 QSqlQuery query("select * from [" + QString("Sheet1") + "$]"); // Select range, place A1:B5 after $
 while (query.next())
 QString column1= query.value(0).toString();
 qDebug() << column1;

The above code print all of column1's values tp the debug output. It works for *.xls and *.xlsx and the other excel file formats.

By default OBDC uses the first row as names for the columns, you are supposed to be able to change this with the 'FirstRowHasNames' option in the connection settings, however there is a bug (see KB288343). Keep in mind that you are using a database and that each column has his own datatype, so if your second row contains text and your third row contains numbers, sql will pick one of these datatypes. If a few rows contain text and the rest of them contain floating numbers, sql will make the text appear and will make the numbers disappear.

NOTE: To use ODBC on Windows, the MS Access Database Engine has to be installed. You can find it here: Microsoft Access Database Engine 2010. The Engine is maybe distributed with a MS Office Access installation, but on this should not be relied on. In Addition, you should regard that a 64 bit application can only use the 64 bit Engine and so for 32 bit accordingly. That’s why you maybe install both versions to avoid problems. Furthermore, the Engine should not be confused with the MS Access Runtime which contains the Engine.

Using independent parser/writer libraries

For a more portable solution, you could take a look at some of the available third-party C/C++ libraries for parsing/writing Excel files:

Qt XlsxC++


noyesyesyesWin, Mac, Linux, …MIT [weak copyleft]
xlsLibC++yesnonoyesWin, Mac, Linux, …LGPL v3 [weak copyleft]
libxlsCyesnoyesnoWin, Mac, Linux, …LGPL [weak copyleft]
LibXLC++yesyesyesyesWin, Mac, Linux, …commercial
qtXLSCyesnoyesyesWin, ?commercial
FreeXLCyesnoyesnoLinux, ?LGPL / MPL [weak copyleft]
BasicExcelC++yesnoyesyes ? ?
Number DuckC++yesnoyesyesWin, Linuxcommercial

Note that these libraries differ in their scope and general approach to the problem.

Using manual XML processing

Files using the XML-based (.xlsx) format could be processed using Qt's XML handling classes (see Handling Document Formats). Third-party libraries can help you in dealing with the container format that wraps the actual XML files:

 APIsupported platformslicense
libopcCWin, Mac, Linux, …permissive

Using batch conversion tools

If all else fails, there is always the option of using an existing tool to automatically convert between Excel files and a more manageable format, and let your Qt application deal with that format instead. The conversion tool could be bundled with your application or specified as a prerequisite, and controlled via Doc:QProcess. Some possibilities are:

 .xls to *.xlsx to **to .xls*to .xlsxplatformslicense
LibreOffice.ods .csv ….ods .csv ….ods .csv ….ods .csv …Win, Mac, Linux, …GPL v3 [strong copyleft]


LibreOffice can be used like this for batch conversion (it's slow, though):
soffice invisible -convert-to xls test.ods

See Also

想对作者说点什么? 我来说一句