返回顶部

Qt处理csv文件

代码如下:

  

bool ReadCSV(const char* filename,QList<QStringList>& content)
{

    QFile file(filename);
    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        std::string s = "read file fail";
        throw s;
    }
    content.clear();
    QTextStream text(&file);
    QStringList listContent;
    while (!text.atEnd()) {
        listContent.push_back(text.readLine());
    }
    for(QString& str:listContent)
    {
        content.push_back(str.split(","));
    }
    file.close();
    return true;
}

bool WriteCSV(const char* filename,const QList<QStringList>& content)
{
    QFile file(filename);
    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        std::string s = "read file fail";
        throw s;
    }
    QTextStream text(&file);
    for(QStringList strlist:content)
    {
        for(QString& str:strlist)
        {
            text << str << ",";
        }
        text << "\r\n";
    }
    file.close();
    return true;
}

  

posted @ 2021-03-26 17:01  Zoya23  阅读(442)  评论(0)    收藏  举报