转换.csv文件为.xlsx文件

qt操作.csv文件转换为.xlsx文件 qt操作.csv文件 #includevoid MainWindow::csvToXlsx(QString theF

qt操作.csv文件转换为.xlsx文件

qt操作.csv文件


#includevoid MainWindow::csvToXlsx(QString theFilePath)
{QFile file(theFilePath);QDir dir;dir.setPath(theFilePath);//检查文件的类型是否为.csvif(!theFilePath.contains(".Csv")){qDebug()<<"传入的文件类型不是.Csv文件"<<endl;QMessageBox::warning(this,"警告","传入的文件类型不是.Csv文件",QMessageBox::Ok);return;}QString absPath = dir.absolutePath();qDebug()<<"绝对路径:"<<absPath<<endl;QStringList pathStrList = absPath.split("/");//检查文件存在性QString tempPath;for(int index=0;index <pathStrList.count()-1;index++){tempPath += QString("%1/").arg(pathStrList.at(index));}qDebug()<<"传进来的文件路径为"<<tempPath<<endl;dir.setPath(tempPath);if(!dir.exists(pathStrList.at(pathStrList.count()-1))){qDebug()<<(pathStrList.at(pathStrList.count()-1))+"此文件不存在,请自行检查";QMessageBox::warning(this,"警告",(pathStrList.at(pathStrList.count()-1))+"此文件不存在,请自行检查",QMessageBox::Ok);return;}QString aimPath;for(int index =0;index <pathStrList.count(); index++){//将传进来的文件名转换为excel的.xlsx需要的形式,仅仅修改后缀if(index< pathStrList.count() -1){aimPath += QString("%1\\").arg(pathStrList.at(index));}else{QString aimName = pathStrList.at(index).split(".").at(0);aimPath += QString("%1.xlsx").arg(aimName);}}qDebug()<<"aimPath = "<<aimPath<<endl;//路径拼接好了,现在就开始进行转换if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){qDebug()<<"此.Csv文件打开失败"<<endl;return;}int countRow=0,colCount=0;QTextStream stream(&file);QString aLine = stream.readLine();QStringList strSplitList = aLine.split(",");colCount = strSplitList.count();qDebug()<<"aLine = "<<aLine<<endl;qDebug()<<"strSplitList = "<<strSplitList<<endl;qDebug()<<"colCount ="<<colCount<<endl;QString rangeStart = QString("A");QString rangeEnd = QString("%1").arg((char)('A'+colCount-1));//rangeEnd = rangeEnd+countRow;//   QString range = QString("%1:%2").arg(rangeStart).arg(rangeEnd);QString range;/*将数据写入xlsx文件*/QAxObject* excel = new QAxObject("Excel.Application");excel->setControl("Excel.Application");//excel文件(需要激活)excel->setProperty("Visible",false);//不显示窗体excel->setProperty("DisplayAlerts",false);//不显示任何警告信息QAxObject* workbooks = excel->querySubObject(<