Create .csv file in C++ in qt

Kamesh picture Kamesh · Apr 2, 2014 · Viewed 11.7k times · Source

I want to create an csv file using c++, using Qt for application and UI framework. Is there's library for csv file.

Answer

iamantony picture iamantony · May 22, 2015

Try qtcsv library for reading and writing csv-files. Example:

#include <QList>
#include <QStringList>
#include <QDir>
#include <QDebug>

#include "qtcsv/stringdata.h"
#include "qtcsv/reader.h"
#include "qtcsv/writer.h"

int main()
{
    // prepare data that you want to save to csv-file
    QStringList strList;
    strList << "one" << "two" << "three";

    QtCSV::StringData strData;
    strData.addRow(strList);
    strData.addEmptyRow();
    strData << strList << "this is the last row";

    // write to file
    QString filePath = QDir::currentPath() + "/test.csv";
    QtCSV::Writer::write(filePath, strData);

    // read data from file
    QList<QStringList> readData = QtCSV::Reader::readToList(filePath);
    for ( int i = 0; i < readData.size(); ++i )
    {
        qDebug() << readData.at(i).join(",");
    }

    return 0;
}

I tried to make it small and easy-to-use. See Readme file for library documentation and other code examples.