Handling microsoft excel file format: Difference between revisions
AutoSpider (talk | contribs) (Convert ExpressionEngine section headers) |
Henri Vikki (talk | contribs) (Redirect to Handling Microsoft Excel file format) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Handling Microsoft Excel file format]] | |||
{{Cleanup | reason=Auto-imported from ExpressionEngine.}} | {{Cleanup | reason=Auto-imported from ExpressionEngine.}} | ||
[[Category:Developing_with_Qt]] | [[Category:Developing_with_Qt]] | ||
= Handling Microsoft Excel (file format) = | = Handling Microsoft Excel (file format) = | ||
Line 8: | Line 10: | ||
This page discusses various available options for working with [http://en.wikipedia.org/wiki/Microsoft_Excel#File_formats Microsoft Excel] documents in your Qt application. Please also read the general considerations outlined on the [[Handling_Document_Formats | Handling Document Formats]] page. | This page discusses various available options for working with [http://en.wikipedia.org/wiki/Microsoft_Excel#File_formats Microsoft Excel] documents in your Qt application. Please also read the general considerations outlined on the [[Handling_Document_Formats | Handling Document Formats]] page. | ||
<pre style="background-color: #E6E6FA">Note that this information is collaboratively collected by the community, with no promise | |||
of completeness or correctness. In particular, use your own research and judgment | |||
when evaluating third-party libraries or tools!</pre> | |||
One needs to distinguish between two different formats (this page deals with both of them): | One needs to distinguish between two different formats (this page deals with both of them): | ||
| | {| class="wikitable" | ||
| | | | ||
| | ! Legacy "Excel Spreadsheet" format | ||
| | ! "Office Open XML Workbook" format | ||
| | |- | ||
| classification: | |||
| binary BIFF-based | |||
| XML-based | |||
|- | |||
| main filename extension: | |||
| .xls | |||
| .xlsx | |||
|- | |||
| main internet media type: | |||
| application/vnd.ms-excel | |||
| application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | |||
|- | |||
| default format of Excel: | |||
| until Excel 2003 | |||
| since Excel 2007 | |||
|} | |||
== Reading / Writing == | == Reading / Writing == | ||
Line 25: | Line 45: | ||
If you are exclusively targeting the Windows platform and Microsoft Excel will be installed on all target machines, then you can use [http://doc.qt.io/qt-4.8/activeqt.html 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 [[Using_ActiveX_Object_in_QT | this how-to]]. | If you are exclusively targeting the Windows platform and Microsoft Excel will be installed on all target machines, then you can use [http://doc.qt.io/qt-4.8/activeqt.html 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 [[Using_ActiveX_Object_in_QT | this how-to]]. | ||
{| class="wikitable" | |||
| [http://office.microsoft.com/excel/ '''Microsoft Excel'''] | ? | | ! DLL file name | ||
! COM object name | |||
! platforms | |||
! license | |||
|- | |||
| [http://office.microsoft.com/excel/ '''Microsoft Excel'''] | |||
| ? | |||
| Excel.Application | |||
| Windows | |||
| commercial | |||
|} | |||
=== Using ODBC === | === Using ODBC === | ||
<pre style="background-color: Moccasin"> TODO: Info on using ODBC drivers (via QSqlDatabase) for accessing Excel spreadsheets - please fill out this section if you know more. (What | |||
is the ODBC driver called? Where does it come from? Windows only or also Mac/Linux? Link to sample code snippet?) </pre> | |||
To read an Excel file with ODBC (tested on Windows 7 with QT 4.7.1) : | To read an Excel file with ODBC (tested on Windows 7 with QT 4.7.1) : | ||
Line 49: | Line 80: | ||
</code> | </code> | ||
This sample print in the console all column1's values. It works for | This sample print in the console all column1's values. It works for <span style="color:#009000">xls</span> and <span style="color:#009000">.xlsx </span> | ||
By default OBDC uses the first row as names for the columns, you can change this whith the 'FirstRowHasNames' option in the connection settings. 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 wil pick one of these datatypes. If a few rows contain text and the rest of them contains floating numbers, sql wil make the text appear and will make the numbers disappear. | By default OBDC uses the first row as names for the columns, you can change this whith the 'FirstRowHasNames' option in the connection settings. 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 wil pick one of these datatypes. If a few rows contain text and the rest of them contains floating numbers, sql wil make the text appear and will make the numbers disappear. | ||
Line 57: | Line 88: | ||
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: | 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: | ||
{| class="wikitable" | |||
! API | |||
| [https://github.com/dbzhang800/QtXlsxWriter '''Qt Xlsx'''] | C++ Qt| | ! .xls | ||
| [http://xlslib.sourceforge.net/ '''xlsLib'''] | C++ | | ! .xlsx | ||
| [http://libxls.sourceforge.net '''libxls'''] | C | | ! reading | ||
| [http://www.libxl.com/ '''LibXL'''] | C++ | | ! writing | ||
| [http://www.qtsoftware.de/vertrieb/db/qtxls_e.htm '''qtXLS'''] | C | | ! platforms | ||
| [https://www.gaia-gis.it/fossil/freexl '''FreeXL'''] | C | | ! license | ||
| [http://www.codeproject.com/Articles/13852/BasicExcel-A-Class-to-Read-and-Write-to-Microsoft '''BasicExcel'''] | C++ | | |- | ||
| [https://numberduck.com/ '''Number Duck'''] | C++ | | | [https://github.com/dbzhang800/QtXlsxWriter '''Qt Xlsx'''] | ||
| C++ Qt | |||
| no | |||
| yes | |||
| yes | |||
| yes | |||
| Win, Mac, Linux, … | |||
| MIT [weak copyleft] | |||
|- | |||
| [http://xlslib.sourceforge.net/ '''xlsLib'''] | |||
| C++ | |||
| yes | |||
| no | |||
| no | |||
| yes | |||
| Win, Mac, Linux, … | |||
| LGPL v3 [weak copyleft] | |||
|- | |||
| [http://libxls.sourceforge.net '''libxls'''] | |||
| C | |||
| yes | |||
| no | |||
| yes | |||
| no | |||
| Win, Mac, Linux, … | |||
| LGPL [weak copyleft] | |||
|- | |||
| [http://www.libxl.com/ '''LibXL'''] | |||
| C++ | |||
| yes | |||
| yes | |||
| yes | |||
| yes | |||
| Win, Mac, Linux, … | |||
| commercial | |||
|- | |||
| [http://www.qtsoftware.de/vertrieb/db/qtxls_e.htm '''qtXLS'''] | |||
| C | |||
| yes | |||
| no | |||
| yes | |||
| yes | |||
| Win, ? | |||
| commercial | |||
|- | |||
| [https://www.gaia-gis.it/fossil/freexl '''FreeXL'''] | |||
| C | |||
| yes | |||
| no | |||
| yes | |||
| no | |||
| Linux, ? | |||
| LGPL / MPL [weak copyleft] | |||
|- | |||
| [http://www.codeproject.com/Articles/13852/BasicExcel-A-Class-to-Read-and-Write-to-Microsoft '''BasicExcel'''] | |||
| C++ | |||
| yes | |||
| no | |||
| yes | |||
| yes | |||
| ? | |||
| ? | |||
|- | |||
| [https://numberduck.com/ '''Number Duck'''] | |||
| C++ | |||
| yes | |||
| no | |||
| yes | |||
| yes | |||
| Win, Linux | |||
| commercial | |||
|} | |||
Note that these libraries differ in their scope and general approach to the problem. | Note that these libraries differ in their scope and general approach to the problem. | ||
Line 73: | Line 175: | ||
Files using the XML-based (.xlsx) format could be processed using Qt's XML handling classes (see [[Handling_Document_Formats | Handling Document Formats]]). Third-party libraries can help you in dealing with the container format that wraps the actual XML files: | Files using the XML-based (.xlsx) format could be processed using Qt's XML handling classes (see [[Handling_Document_Formats | Handling Document Formats]]). Third-party libraries can help you in dealing with the container format that wraps the actual XML files: | ||
{| class="wikitable" | |||
! API | |||
! supported platforms | |||
! license | |||
|- | |||
| [http://libopc.codeplex.com '''libopc'''] | |||
| C | |||
| Win, Mac, Linux, … | |||
| permissive | |||
|} | |||
<pre style="background-color: Moccasin"> TODO: If you know more about the container format, and whether it really needs a specialized library for processing, please expand this section. </pre> | |||
=== Using batch conversion tools === | === Using batch conversion tools === | ||
Line 83: | Line 194: | ||
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: | 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: | ||
{| class="wikitable" | |||
| | | | ||
| [http://www.libreoffice.org/ '''LibreOffice'''] | | ! .xls to * | ||
| [http://… '''…'''] | … | … | … | … | … | … | | ! .xlsx to * | ||
! * to .xls | |||
! * to .xlsx | |||
! platforms | |||
! license | |||
|- | |||
| [http://www.libreoffice.org/ '''LibreOffice'''] | |||
| .ods .csv | |||
… | |||
| .ods .csv | |||
… | |||
| .ods .csv | |||
… | |||
| .ods .csv | |||
… | |||
| Win, Mac, Linux, | |||
… | |||
| GPL v3 [strong copyleft] | |||
|- | |||
| [http://… '''…'''] | |||
| … | |||
| … | |||
| … | |||
| … | |||
| … | |||
| … | |||
|} | |||
''Notes:'' | ''Notes:'' | ||
LibreOffice can be used like this for batch conversion (it's slow, though): <code>soffice —invisible -convert-to xls test.ods<code> | LibreOffice can be used like this for batch conversion (it's slow, though): <code>soffice —invisible -convert-to xls test.ods</code> | ||
== Displaying / User Interaction == | == Displaying / User Interaction == | ||
Line 95: | Line 232: | ||
=== Using Excel itself === | === Using Excel itself === | ||
<pre style="background-color: Moccasin"> 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). </pre> | |||
=== Manual solution === | === Manual solution === | ||
<pre style="background-color: Moccasin"> TODO: Tips for displaying Excel documents which were manually parsed using one of the methods described above. </pre> | |||
== See Also == | == See Also == |
Latest revision as of 06:48, 31 March 2015
Redirect to:
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine. Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean. |
Handling Microsoft Excel (file format)
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.
Note that this information is collaboratively collected by the community, with no promise of completeness or correctness. In particular, use your own research and judgment when evaluating third-party libraries or tools!
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-based | XML-based |
main filename extension: | .xls | .xlsx |
main internet media type: | application/vnd.ms-excel | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
default format of Excel: | until Excel 2003 | since 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 name | COM object name | platforms | license | |
---|---|---|---|---|
Microsoft Excel | ? | Excel.Application | Windows | commercial |
Using ODBC
TODO: Info on using ODBC drivers (via QSqlDatabase) for accessing Excel spreadsheets - please fill out this section if you know more. (What is the ODBC driver called? Where does it come from? Windows only or also Mac/Linux? Link to sample code snippet?)
To read an Excel file with ODBC (tested on Windows 7 with QT 4.7.1) :
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={Microsoft Excel Driver ('''.xls)};DBQ=" + QString("c:file.xlsx"));
if(db.open())
{
QSqlQuery query("select''' from [" + QString("Sheet1") + "$]"); // Select range, place A1:B5 after $
while (query.next())
{
QString column1= query.value(0).toString();
qDebug() << column1;
}
}
This sample print in the console all column1's values. It works for xls and .xlsx
By default OBDC uses the first row as names for the columns, you can change this whith the 'FirstRowHasNames' option in the connection settings. 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 wil pick one of these datatypes. If a few rows contain text and the rest of them contains floating numbers, sql wil make the text appear and will make the numbers disappear.
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:
API | .xls | .xlsx | reading | writing | platforms | license | |
---|---|---|---|---|---|---|---|
Qt Xlsx | C++ Qt | no | yes | yes | yes | Win, Mac, Linux, … | MIT [weak copyleft] |
xlsLib | C++ | yes | no | no | yes | Win, Mac, Linux, … | LGPL v3 [weak copyleft] |
libxls | C | yes | no | yes | no | Win, Mac, Linux, … | LGPL [weak copyleft] |
LibXL | C++ | yes | yes | yes | yes | Win, Mac, Linux, … | commercial |
qtXLS | C | yes | no | yes | yes | Win, ? | commercial |
FreeXL | C | yes | no | yes | no | Linux, ? | LGPL / MPL [weak copyleft] |
BasicExcel | C++ | yes | no | yes | yes | ? | ? |
Number Duck | C++ | yes | no | yes | yes | Win, Linux | commercial |
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:
API | supported platforms | license | |
---|---|---|---|
libopc | C | Win, Mac, Linux, … | permissive |
TODO: If you know more about the container format, and whether it really needs a specialized library for processing, please expand this section.
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 .xlsx | platforms | license | |
---|---|---|---|---|---|---|
LibreOffice | .ods .csv
… |
.ods .csv
… |
.ods .csv
… |
.ods .csv
… |
Win, Mac, Linux,
… |
GPL v3 [strong copyleft] |
… | … | … | … | … | … | … |
Notes:
LibreOffice can be used like this for batch conversion (it's slow, though):
soffice —invisible -convert-to xls test.ods
Displaying / User Interaction
Using Excel itself
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).
Manual solution
TODO: Tips for displaying Excel documents which were manually parsed using one of the methods described above.
See Also
- Handling Document Formats
- other Microsoft Office formats:
- other “spreadsheet” formats: