【基础功能】c++QT创建.csv文件

来源:互联网 发布:淘宝怎么升级心 编辑:程序博客网 时间:2024/05/17 08:28

转眼本科都毕业了,研究生生涯里决定多写写博文,也是因为码代码多了,基础板块老是忘记放在哪里,以后想起来了就发篇博文吧,方便大家也方便自己。

这个是c++创建.csv文件,我是用QT的界面撒~

void CreatExcel(){QString fileName = QFileDialog::getSaveFileName(widget, QObject::tr("Save File"),"",QObject::tr("EXCEL (*.CSV)"));//对话框if(fileName =="")return;QTextCodec *code;code = QTextCodec::codecForName("gb18030");//编码格式string strbuffer = code->fromUnicode(fileName).data();FILE *fileWrite = fopen( strbuffer.c_str(),"w");//写文件        QFile file;file.open(fileWrite, QIODevice::WriteOnly);int BandCount = 7;QString stempName = NULL;//列名for(int j = 0; j < BandCount; j++){stempName += "Band";stempName += QString().setNum(j + 1);if (j == BandCount - 1)//到最后一个stempName += "\n";elsestempName += ",";}string strNamebuffer = code->fromUnicode(stempName).data();file.write(strNamebuffer.c_str(), qstrlen(strNamebuffer.c_str()));//列名写入        int PixelCount = 10;        string* strCountbuffer = new string[PixelCount];//列值写入for(int i = 0; i < PixelCount; i++){QString stemp;//写一行for(int j = 0; j < BandCount; j++){stemp += code->fromUnicode(QString().setNum(j));if (j == BandCount - 1)    stemp += "\n";else     stemp += ",";}strCountbuffer[i] = code->fromUnicode(stemp).data();file.write(strCountbuffer[i].c_str(), qstrlen(strCountbuffer[i].c_str())); }file.close();//关闭文件}