Anibody has solve or has an idea on how to export a .csv in Ignition with formated columns so everithing doesn´t appear on just 1st column of the .csv. I would need this so my client can make reports on the table data.
At the moment I got:
# Get table
table = self.parent.getChild("TabContainer").getChild("ColumnContainer_1").getChild("TablaDNF_3")
# Use raw dataset
ds = table.props.data
# Convert dataset to CSV (semicolon for Excel)
csv_string = system.dataset.toCSV(ds, showHeaders=True, delimiter=';')
# Force Windows line endings (\r\n)
csv_string = csv_string.replace("\n", "\r\n")
# Add UTF-8 BOM for Excel compatibility
csv_bytes = u'\ufeff'.encode('utf-8') + csv_string.encode('utf-8')
# Download file with static name
system.perspective.download("TablaDNF.csv", csv_bytes)
But still showing all in same 1st column.
Any ideas our workarounds welcome. I´ve got many tables to export. for reports
What part doesn't work? When you open the file in excel everything is in a single line?
If you open the file in a text editor, are the fields delimited with a comma?
I would honestly take out all the encoding stuff to make sure there isn't another issue happening.
# Get table
table = self.parent.getChild("TabContainer").getChild("ColumnContainer_1").getChild("TablaDNF_3")
# Use raw dataset
ds = table.props.data
# Convert dataset to CSV
csv_string = system.dataset.toCSV(ds)
# Download file with static name
system.perspective.download("TablaDNF.csv", csv_string )
I've never had an issue importing a CSV using a comma as the delimeter into excel. I've also never needed to do anything extra to make the file "compatible" such as converting new line characters.
Can you provide a sample of what your table looks like and what the resulting CSV looks like?
component = self.parent.getChild("TabContainer").getChild("TablaOT")
csv = system.dataset.toCSV(component.props.data)
# Get the current date
current_time = system.date.now()
# Format the date into a string for the file name (e.g., "YYYYMMDD")
formatted_date = system.date.format(current_time, "yyyyMMdd")
file_name = "TablaOT_" + formatted_date + ".csv"
system.perspective.download(file_name, csv)
with all the data for each row compacted in first column
You get the data well formated in different columns?
If can use excel directly that would be great, let me know if this code looks good for you, and if not please provide a simple version to export a simple table, that would be enough for me to work with.
You can see from the CSV file that there is no formatting information embedded in the file. It supplies each row of the table on a separate line and defaults to comma separatation (CSV = Comma-Separated Variable). Usually strings are "quoted" and numbers are not. Your sample seems to show everything quoted.
This, really. Don't use CSV if you need formatting in Excel. CSV is fundamentally unformatted.
If system.dataset.toExcel() doesn't produce acceptable formatting, you can use the Apache POI libraries (included with Ignition) to implement your own custom export. (Multiple examples here on the forum.)