Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Get errors when exporting data to CSV files
Forum Updated to NodeBB v4.3 + New Features

[Solved] Get errors when exporting data to CSV files

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LeeMinh
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thEClaw
      wrote on last edited by
      #2

      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"";@

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LeeMinh
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved