Description

Learn how to export a dataset from a query or table to a CSV file.

Video recorded using: Ignition 7.7

Transcript

(open in window)

[00:00] In this lesson we're going to look at how to export a dataset to a CSV file. It doesn't take much scripting to do this. So in our example script here, we grab our table component, and then we grab it's dataset out of it. Now, this example would work equally well for a dataset that came from anywhere. Datasets are all the same kind of thing, so if you had a system.db.run query, and it returned a dataset, this example would work just as well for that. All we need to do to export it to a CSV file, is first convert it to CSV, which we do via system.dataset.toCSV, which takes the dataset, and returns a string, and that string represents the comma-separated values. And then we use system.file.savefile, and this is just a way to open up a save file prompt box for the user, so that they can pick the location of saving the file. All this does is return a file location, or if they hit cancel, it returns none. So that's what this if block is for. It's for protecting against the case where they hit cancel, so that we don't do anything. But in case they hit okay, we go ahead and use a system.file.writefile, which takes a path to a file, and a string, in this case, our CSV. So let's see this in action. Run this guy. I'll go ahead and save it right there in my documents. And now we can see the file's there, and if we open it up, we'll see the same information that came from our table, right here in Excel.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.