QT将excel文件转换成txt文件

it2023-08-12  66

QT将excel文件转换成txt文件

代码

本文基于QAxObject,依赖excel程序,优化了读xls文件的效率。 注意pro中需要添加 CONFIG += qaxcontainer

代码

//excel程序连接 QAxObject excel("Excel.Application"); excel.setProperty("Visible", false); //隐藏打开的excel文件界面 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"); //获取sheet所有数据,二维矩阵形式 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); //行list 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); //关闭xls文件 excel.dynamicCall("Quit(void)"); //退出excel
最新回复(0)