Handling microsoft excel file format

From Qt Wiki
Revision as of 15:57, 5 March 2015 by AutoSpider (talk | contribs) (Convert ExpressionEngine section headers)
Jump to navigation Jump to search
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.

[toc align_right="yes" depth="2"]

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.

p{width:60%;border:solid 1px #99a;background:#eef;color:#335;padding:2pt 4pt;font-size:0.9em;line-height:150%;font-style:italic}. 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):

table{width:95%;margin-left:2.5%}. | |. Legacy "Excel Spreadsheet" format |. "Office Open XML Workbook" format | | classification: | binary BIFF-based | XML-based | | main filename extension: | {font:1em monospace}.xls | {font-family:monospace}.xlsx | | main internet media type: | {font:0.9em monospace}application/vnd.ms-excel | {font:0.9em monospace}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.

table{width:95%;margin-left:2.5%}. | |. DLL file name |. COM object name |. platforms |. license | | Microsoft Excel | ? | {font-family:monospace}Excel.Application | Windows | {color:#458}commercial |

Using ODBC

p{border:dashed 1px #a94;background:#fbf3dd;color:#530;padding:2pt 4pt;margin-left:2pt;margin-right:2pt;font-size:0.9em;line-height:150%;font-style:italic}. 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 {color:#580}.xls and {color:#580}.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:

table{width:95%;margin-left:2.5%}. | |. API |. {font-family:monospace}.xls |. {font-family:monospace}.xlsx |. reading |. writing |. platforms |. license | | Qt Xlsx | C++ Qt| {color:#920}no | {color:#580}yes | {color:#580}yes| {color:#580}yes | Win, Mac, Linux, … | MIT {color:#458}[weak copyleft]| | xlsLib | C++ | {color:#580}yes | {color:#920}no | {color:#920}no | {color:#580}yes | Win, Mac, Linux, … | LGPL v3 {color:#458}[weak copyleft]| | libxls | C | {color:#580}yes | {color:#920}no | {color:#580}yes | {color:#920}no | Win, Mac, Linux, … | LGPL {color:#458}[weak copyleft]| | LibXL | C++ | {color:#580}yes | {color:#580}yes | {color:#580}yes | {color:#580}yes | Win, Mac, Linux, … | {color:#458}commercial | | qtXLS | C | {color:#580}yes | {color:#920}no | {color:#580}yes | {color:#580}yes | Win, ? | {color:#458}commercial | | FreeXL | C | {color:#580}yes | {color:#920}no | {color:#580}yes | {color:#920}no | Linux, ? | LGPL / MPL {color:#458}[weak copyleft]| | BasicExcel | C++ | {color:#580}yes | {color:#920}no | {color:#580}yes | {color:#580}yes | ? | ? | | Number Duck | C++ | {color:#580}yes | {color:#920}no | {color:#580}yes | {color:#580}yes | Win, Linux | {color:#458}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:

table{width:95%;margin-left:2.5%}. | |. API |. supported platforms |. license | | libopc | C | Win, Mac, Linux, … | {color:#458}permissive |

p{border:dashed 1px #a94;background:#fbf3dd;color:#530;padding:2pt 4pt;margin-left:2pt;margin-right:2pt;font-size:0.9em;line-height:150%;font-style:italic}. 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:

table{width:95%;margin-left:2.5%}. |. |. {font-family:monospace}.xls to * |. {font-family:monospace}.xlsx to * |. &#42; to {font-family:monospace}.xls |. &#42; to {font-family:monospace}.xlsx |. platforms |_. license | | LibreOffice | {font-family:monospace}.ods .csv … | {font-family:monospace}.ods .csv … | {font-family:monospace}.ods .csv … | {font-family:monospace}.ods .csv … | Win, Mac, Linux, … | GPL v3 {color:#458}[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

p{border:dashed 1px #a94;background:#fbf3dd;color:#530;padding:2pt 4pt;margin-left:2pt;margin-right:2pt;font-size:0.9em;line-height:150%;font-style:italic}. 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

p{border:dashed 1px #a94;background:#fbf3dd;color:#530;padding:2pt 4pt;margin-left:2pt;margin-right:2pt;font-size:0.9em;line-height:150%;font-style:italic}. TODO: Tips for displaying Excel documents which were manually parsed using one of the methods described above.

p{color:#fff;border-bottom:solid 1px #ccc}. .

See Also