Home > other >  QT C All datas shown on same column when export csv?
QT C All datas shown on same column when export csv?

Time:08-05

I want to save a set of data in csv file. I have no problem with registration. My problem is in the form of saving. All data are collected in column A. I have 2 graphs in total, graph1 and graph2. Graph1 should be in column A and Graph2 should be in column B. How can I set this up.

Here is code for export csv

void MainWindow::exportArraysToCSV(QStringList labelList, QList < QList < double >> dataColums, QChar sep) {
  QString filters("CSV files (*.csv);;All files (*.*)");
  QString defaultFilter("CSVi files (*.csv)");
  QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(),
    filters, & defaultFilter);
  QFile file(fileName);

  if (file.open(QFile::WriteOnly | QFile::Append)) {
    QTextStream data( & file);
    QStringList strList;

    foreach(auto label, labelList) {
      if (label.length() > 0)
        strList.append("\""   label   "\"");
      else
        strList.append("");
    }

    data << strList.join(",") << "\n";

    int maxRowCount = 0;
    foreach(auto column, dataColums)
    maxRowCount = qMax(maxRowCount, column.count());

    for (int i = 5; i < maxRowCount;   i) // rows
    {
      strList.clear();
      for (int j = 0; j < 2;   j) // columns
      {
        if (i < dataColums[j].count())
          strList.append(QString::number(dataColums[j][i], 'f'));


        else
          strList.append("\"\" ");
      }

      data << strList.join(";")   "\n";


    }
    file.close();
  }
}

I think this code for join all data on same column. How can I split it?

 data << strList.join(",") << "\n";

Here is write log to csv function

void Logger::writeLogCSV(QStringList labelList, QList<double> dataList, bool addTime)
{
    QTextStream out(logFile);

    if (addTime)
    {
        if (csvLabelsBuffer.contains("time") == false)
            csvLabelsBuffer.insert(0, "time");
    }

    if (labelList.count() == 0)
    {
        qDebug() << "Empty label list - abort csv write";
        return;
    }

    bool canAddLabel = false;
    for (auto i = 2; i < labelList.count();   i)
    {
        if (csvLabelsBuffer.count() == 2 || csvLabelsBuffer.contains(labelList[i]) == false)
        {
            canAddLabel = true;
            csvLabelsBuffer.append(labelList[i]);
        }
    }

    if (canAddLabel)
    {
        canAddLabel = false;

        QStringList origFile = out.readAll().split(QRegExp("[\r\n]"), QString::SplitBehavior::SkipEmptyParts);

        for (auto i = 2; i < csvLabelsBuffer.count();   i)
            out << "\""   csvLabelsBuffer[i]   "\t";
        out << "";
       // out << "\n";

        if (origFile.length() > 0)
        {
            while (origFile.first().contains("\""))
                origFile.removeFirst();
        }

        logFile->resize(0); // delete contents !

        for (auto i = 2; i < origFile.count();   i) // Start from second line (data without first line which contains labels)
            out << origFile[i]   ",";

        return;
    }

    // add Data !
    for (auto i = 2; i < csvLabelsBuffer.count();   i)
    {
        out.atEnd(); // ???

        int index = labelList.indexOf(csvLabelsBuffer[i]); 
        if (index >= 0 && index < dataList.count())
            out << QString::number(dataList[index], 'f')   ",";
        else if (csvLabelsBuffer[i] == "time")
            out << QDateTime::currentDateTimeUtc().toString("yyyy-MM-ddTHH:MM:ss.zzzZ")   ',';
    }

    out << "\n \n";

}

Here is csv file. I need move graph2 and all datas to column B

CodePudding user response:

you should open the generated file with a text editor and post some lines of it here to your answer. By Opening the File with excel you need to set the seperator it will use to split the the row into columns. Usually it will use ',' but sometimes it will default to ';'.

this will seperate them by comma, which is the standard for csv:

void MainWindow::exportArraysToCSV(QStringList labelList, QList < QList < double >> dataColums, QChar sep) {
      QString filters("CSV files (*.csv);;All files (*.*)");
      QString defaultFilter("CSVi files (*.csv)");
      QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(),
        filters, & defaultFilter);
      QFile file(fileName);
    
      if (file.open(QFile::WriteOnly | QFile::Append)) {
        QTextStream data( & file);
        QStringList strList;
    
        foreach(auto label, labelList) {
          if (label.length() > 0)
            strList.append("\""   label   "\"");
          else
            strList.append("");
        }
    
        data << strList.join(",") << "\n";
    
        int maxRowCount = 0;
        foreach(auto column, dataColums)
        maxRowCount = qMax(maxRowCount, column.count());
    
        for (int i = 5; i < maxRowCount;   i) // rows
        {
          strList.clear();
          for (int j = 0; j < 2;   j) // columns
          {
            if (i < dataColums[j].count())
              strList.append(QString::number(dataColums[j][i], 'f'));
    
    
            else
              strList.append("\"\" ");
          }
    
          data << strList.join(",")   "\n";
    
    
        }
        file.close();
      }
    }

If that does not fix your problem, then you need to open your csv with a texteditor and see whats wrong with it. Post some lines of it here then.

  • Related