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-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
Contents
[hide]
1Reading/Writing
1.1Using Excel itself
1.2Using ODBC
1.3Using independent parser/writer libraries
1.4Using manual XML processing
1.5Using batch conversion tools
2See Also
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
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"));
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;
}
db.close();
QSqlDatabase::removeDatabase("xlsx_connection");
}
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:
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
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
See Also
Handling Document Formats
Handling Microsoft Word file format
Handling Microsoft PowerPoint file format
Handling HTML
Handling PDF
Category:
Developing with Qt
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSON
JSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다.
그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다.
저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
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"));
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;
}
db.close();
QSqlDatabase::removeDatabase("xlsx_connection");
}
soffice —invisible -convert-to xls test.ods
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.