QT将excel文件转换成txt文件
代码
本文基于QAxObject,依赖excel程序,优化了读xls文件的效率。 注意pro中需要添加 CONFIG += qaxcontainer
代码
QAxObject
excel("Excel.Application");
excel
.setProperty("Visible", false);
QAxObject
* workbooks
= excel
.querySubObject("WorkBooks");
QAxObject
* book
= workbooks
->querySubObject("Open(QString, QVariant)", filename
);
QAxObject
* sheet
= book
->querySubObject("WorkSheets(int)", 1);
QAxObject
* usedrange
= sheet
->querySubObject("UsedRange");
QVariant cell
= usedrange
->dynamicCall("Value");
filename
= path
+year
+"_"+month
+"_"+date
+".txt";
QFile
textfile(filename
);
textfile
.open(QIODevice
::WriteOnly
);
textfile
.close();
textfile
.open(QIODevice
::WriteOnly
|QIODevice
::Text
);
QTextStream
stream(&textfile
);
QVariantList cell_rowList
=cell
.toList();
unsigned int rowCount
= cell_rowList
.size();
unsigned int columnCount
;
for(i
=0;i
<rowCount
;i
++)
{
QVariantList cell_columnList
=cell_rowList
[i
].toList();
columnCount
= cell_columnList
.size();
for(j
=0;j
<columnCount
;j
++)
{
stream
<<cell_columnList
[j
].toString()<<",";
}
stream
<<"\n";
}
textfile
.close();
book
->dynamicCall("Close(Boolen)", false);
excel
.dynamicCall("Quit(void)");