Read the sheets in the Excel with QT
2010-02-10 21:10
567 查看
#include <QAxObject>
//open file
QString fileName = QFileDialog::getOpenFileName(this,tr("Open Spreadsheet"), ".", tr("Spreadsheet files (*.xlsx)"));
if (!fileName.isEmpty())
{
int r = QMessageBox::information(this, fileName.toLatin1().mid(fileName.toLatin1().lastIndexOf("/")+1,fileName.toLatin1().length()),
tr(fileName.toLatin1().mid(fileName.toLatin1().lastIndexOf("/")+1,fileName.toLatin1().length())+" "+"has been found./n"
"Do you want to open this file?"),
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::Cancel | QMessageBox::Escape);
if(r==QMessageBox::Yes)
{
// load file into the excel ojbect
QAxObject* excel = new QAxObject( "Excel.Application", 0 );
QAxObject* workbooks = excel->querySubObject( "Workbooks" );
QAxObject* workbook = workbooks->querySubObject( "Open(QString&)", fileName );
QAxObject* sheets = workbook->querySubObject( "Worksheets" );
QList<QVariantList> data; //Data list from excel, each QVariantList is worksheet row
//worksheets count
int count=sheets->dynamicCall("Count()").toInt();
for(int i=1;i<count;i++)
{
//sheet pointer
QAxObject* sheet = sheets->querySubObject( "Item( int )", i );
QAxObject* rows = sheet->querySubObject( "Rows" );
int rowCount = rows->dynamicCall( "Count()" ).toInt(); //unfortunately, always returns 255, so you have to check somehow validity of cell values
QAxObject* columns = sheet->querySubObject( "Columns" );
int columnCount = columns->dynamicCall( "Count()" ).toInt(); //similarly, always returns 65535
//One of possible ways to get column count
int currentColumnCount = 0;
for (int col=1; col<columnCount; col++)
{
QAxObject* cell = sheet->querySubObject( "Cells( int, int )", 1, col );
QVariant value = cell->dynamicCall( "Value()" );
if (value.toString().isEmpty())
break;
else
currentColumnCount = col;
}
columnCount = currentColumnCount;
//sheet->dynamicCall( "Calculate()" ); //maybe somewhen it's necessary, but i've found out that cell values are calculated without calling this function. maybe it must be called just to recalculate
for (int row=1; row <= rowCount; row++)
{
QVariantList dataRow;
bool isEmpty = true; //when all the cells of row are empty, it means that file is at end (of course, it maybe not right for different excel files. it's just criteria to calculate somehow row count for my file)
for (int column=1; column <= columnCount; column++)
{
QAxObject* cell = sheet->querySubObject( "Cells( int, int )", row, column );
QVariant value = cell->dynamicCall( "Value()" );
if (!value.toString().isEmpty() && isEmpty)
isEmpty = false;
dataRow.append(value);
}
if (isEmpty) //criteria to get out of cycle
break;
data.append(dataRow);
}
}
QMessageBox::information(this,"test",data[0].value(0).toString(),QMessageBox::Yes | QMessageBox::Default);
//QTableView *view=new QTableView();
// QFile file("c:/test.txt");
// char *msg="hello";
// file.open(QIODevice::WriteOnly);
//file.write(msg); // write to stderr
//file.close();
workbook->dynamicCall("Close()");
excel->dynamicCall("Quit()");
}
else
{
this->close();
}
}
//open file
QString fileName = QFileDialog::getOpenFileName(this,tr("Open Spreadsheet"), ".", tr("Spreadsheet files (*.xlsx)"));
if (!fileName.isEmpty())
{
int r = QMessageBox::information(this, fileName.toLatin1().mid(fileName.toLatin1().lastIndexOf("/")+1,fileName.toLatin1().length()),
tr(fileName.toLatin1().mid(fileName.toLatin1().lastIndexOf("/")+1,fileName.toLatin1().length())+" "+"has been found./n"
"Do you want to open this file?"),
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::Cancel | QMessageBox::Escape);
if(r==QMessageBox::Yes)
{
// load file into the excel ojbect
QAxObject* excel = new QAxObject( "Excel.Application", 0 );
QAxObject* workbooks = excel->querySubObject( "Workbooks" );
QAxObject* workbook = workbooks->querySubObject( "Open(QString&)", fileName );
QAxObject* sheets = workbook->querySubObject( "Worksheets" );
QList<QVariantList> data; //Data list from excel, each QVariantList is worksheet row
//worksheets count
int count=sheets->dynamicCall("Count()").toInt();
for(int i=1;i<count;i++)
{
//sheet pointer
QAxObject* sheet = sheets->querySubObject( "Item( int )", i );
QAxObject* rows = sheet->querySubObject( "Rows" );
int rowCount = rows->dynamicCall( "Count()" ).toInt(); //unfortunately, always returns 255, so you have to check somehow validity of cell values
QAxObject* columns = sheet->querySubObject( "Columns" );
int columnCount = columns->dynamicCall( "Count()" ).toInt(); //similarly, always returns 65535
//One of possible ways to get column count
int currentColumnCount = 0;
for (int col=1; col<columnCount; col++)
{
QAxObject* cell = sheet->querySubObject( "Cells( int, int )", 1, col );
QVariant value = cell->dynamicCall( "Value()" );
if (value.toString().isEmpty())
break;
else
currentColumnCount = col;
}
columnCount = currentColumnCount;
//sheet->dynamicCall( "Calculate()" ); //maybe somewhen it's necessary, but i've found out that cell values are calculated without calling this function. maybe it must be called just to recalculate
for (int row=1; row <= rowCount; row++)
{
QVariantList dataRow;
bool isEmpty = true; //when all the cells of row are empty, it means that file is at end (of course, it maybe not right for different excel files. it's just criteria to calculate somehow row count for my file)
for (int column=1; column <= columnCount; column++)
{
QAxObject* cell = sheet->querySubObject( "Cells( int, int )", row, column );
QVariant value = cell->dynamicCall( "Value()" );
if (!value.toString().isEmpty() && isEmpty)
isEmpty = false;
dataRow.append(value);
}
if (isEmpty) //criteria to get out of cycle
break;
data.append(dataRow);
}
}
QMessageBox::information(this,"test",data[0].value(0).toString(),QMessageBox::Yes | QMessageBox::Default);
//QTableView *view=new QTableView();
// QFile file("c:/test.txt");
// char *msg="hello";
// file.open(QIODevice::WriteOnly);
//file.write(msg); // write to stderr
//file.close();
workbook->dynamicCall("Close()");
excel->dynamicCall("Quit()");
}
else
{
this->close();
}
}
相关文章推荐
- Read a Text File with VBA in Excel, and Write the Text to a Spreadsheet
- ADODB使用excel的"Cannot modify the design of table 'xxxx'. It is in a read-only database"的问题
- Error with hilo in NHibernate - “could not read a hi value - you need to populate the table”
- Excel导入遇到的问题An object with the same key already exists in the ObjectStateManager……
- To find the differences between 2 Excel sheets in the same file
- the basic approach to read dataset(TFRecord) with iterator in Tensorflow
- Windows SDK Registry: How can I read in data from the registry?
- No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
- struts2警告: The first TextProvider in the ValueStack could not locate the message resource with key
- The user types in Qt on D-Bus
- Read / Write Excel file in Java using Apache POI
- jstl学习常见错误一: The absolute uri: http://java.sun.com/jst/core cannot be resolved in either web.xml or the jar files deployed with
- 论文阅读:Reading Text in the Wild with Convolutional Neural Networks
- SharePoint 2007 Edit in Datasheet时遇到"The selected cells are read-only."问题
- The Excel Connection Manager is not supported in the 64-bit version of SSIS, as no OLE DB provider i
- Error:No toolchains found in the NDK toolchains folder for ABI with prefix: x86_64
- iOS "The sandbox is not in sync with the Podfile.lock"解决方式
- This version does not support documents saved in the Xcode 8 format. Open this document with Xcode 8
- How to identify all the Corrupted Objects in the Database reported with RMAN
- How To Check Whether Physical Standby is in Sync with the Primary or Not?