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

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

这个是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";
		else
			stempName += ",";
	}
	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();//关闭文件
}


你可能感兴趣的:(【基础功能】c++QT创建.csv文件)