[Solved] Get errors when exporting data to CSV files



  • I am writing a small application to export data to CSV files. And this is the code that I used

    @
    QString fileName = QFileDialog::getSaveFileName(
    this,
    tr("Save As"),
    "",
    tr("Comma Separated Values (.csv);;All Files (.*)"));

    if (fileName.isEmpty()) return;
    else {
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly)) {
    QMessageBox::information(this, tr("Unable to open file"), file.errorString());
    return;
    }

    // Write content to *.csv file
    QTextStream stream(&file);

    // Print header
    stream << "ID\tUsername\tAddress";

    stream.flush();
    file.close();
    }
    @

    Then I run the application, a dialog appears to save CSV file. But when I open the CSV file that I have just saved, I got 2 following error warnings

    !http://i.stack.imgur.com/pt0vJ.png(Error 1)!

    and

    !http://i.stack.imgur.com/JQF1w.png(Error 2)!

    This is the data in the CSV file

    !http://i.stack.imgur.com/vbHXY.png(The data in CSV file)!

    As you see, I intend to display 3 fields such as ID, Username and Address in 3 columns but all data is in a column.

    Could you explain me that why I got 2 above messages and how can I display each field (ID, Username and Address) in each column?

    Thanks in advance!

    P/S: Currently I am using Microsoft Excel 2013.



  • Step one: Don't be afraid to use a text editor (the really old one) to take a look into the file yourself. You'll see first hand if everything is fine in there, or at least "as intended".

    Step two: When importing a csv-file, you have to give some settings - e.g. the field delimiter, which not necessarily has to be a tab (\t)!

    I think you are just using a bad format here. I would try something like this, because that's how I know it:
    @stream << ""ID";"Username";"Address"";@



  • @thEClaw: Thanks for your help! Now I can solve my issues. I am very happy!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.