Export data to excel or csv

I have put a dataSet on Parameters,
and I would like export then on excel/CSV file.

dataDS = self.view.params.pData #table.data

	system.dataset.exportExcel("test.xls",1, dataDS)

error:

com.inductiveautomation.ignition.common.script.JythonExecException
Traceback (most recent call last):
  File "<function:runAction>", line 3, in runAction
TypeError: can't convert u'value' to com.inductiveautomation.ignition.common.Dataset

	caused by org.python.core.PyException
Traceback (most recent call last):
  File "<function:runAction>", line 3, in runAction
TypeError: can't convert u'value' to com.inductiveautomation.ignition.common.Dataset


Ignition v8.1.38 (b2024030513)
Java: Azul Systems, Inc. 17.0.9

First time I'am tryng to export data. someone can help ? :thinking:

You are trying to export a string value to excel. The exportExcel system function expects a dataset.

headers = ['test']
data = [['value']]
dataDS = system.dataset.toDataSet(headers,data)
...

Thank you @dkhayes117

I do this script but I'am getting another error:

	
	headers = []
	data = []
	DS = self.parent.parent.parent.getChild("CorpEvenement").props.params.Data
	for row in DS:
		if not headers:
			headers = row.keys()
		data.append([val for key,val in row.iteritems()])
		
	dataDS=  system.dataset.toDataSet(headers,data)
	
	
	#dataDS = self.parent.parent.parent.getChild("CorpEvenement").props.params.Data #table.data

	system.dataset.exportExcel("test.xls",1, dataDS)

errror:

com.inductiveautomation.ignition.common.script.JythonExecException
Traceback (most recent call last):
  File "<function:runAction>", line 16, in runAction
AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'exportExcel'

	at org.python.core.Py.AttributeError(Py.java:176)
	at org.python.core.PyObject.noAttributeError(PyObject.java:965)
	at org.python.core.PyObject.__getattr__(PyObject.java:959)
	at org.python.pycode._pyx1905.runAction$1(<function:runAction>:16)
	at org.python.pycode._pyx1905.call_function(<function:runAction>)
	at org.python.core.PyTableCode.call(PyTableCode.java:173)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:306)
	at org.python.core.PyFunction.function___call__(PyFunction.java:474)
	at org.python.core.PyFunction.__call__(PyFunction.java:469)
	at org.python.core.PyFunction.__call__(PyFunction.java:464)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:847)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:829)
	at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runFunction(ProjectScriptLifecycle.java:868)
	at com.inductiveautomation.ignition.common.script.ScriptManager$ScriptFunctionImpl.invoke(ScriptManager.java:1010)
	at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$AutoRecompilingScriptFunction.invoke(ProjectScriptLifecycle.java:950)
	at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:161)
	at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:98)
	at com.inductiveautomation.perspective.gateway.action.ScriptAction.runAction(ScriptAction.java:80)
	at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.lambda$call$0(ActionCollection.java:263)
	at com.inductiveautomation.perspective.gateway.api.LoggingContext.mdc(LoggingContext.java:54)
	at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.call(ActionCollection.java:252)
	at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.call(ActionCollection.java:221)
	at com.inductiveautomation.perspective.gateway.threading.BlockingTaskQueue$TaskWrapper.run(BlockingTaskQueue.java:154)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at com.inductiveautomation.perspective.gateway.threading.BlockingWork$BlockingWorkRunnable.run(BlockingWork.java:58)
	at java.base/java.lang.Thread.run(Unknown Source)
Caused by: org.python.core.PyException
Traceback (most recent call last):
  File "<function:runAction>", line 16, in runAction
AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'exportExcel'

	... 29 more

Ignition v8.1.38 (b2024030513)
Java: Azul Systems, Inc. 17.0.9

exportExcel() will not work in perspective. Use toExcel() instead.

1 Like

I missed the perspective tag :frowning:

I'am gatting the same error

TypeError: can't convert 1 to com.inductiveautomation.ignition.common.Dataset

toExcel has different parameters and return value.

Pause.
Take a second.
Use the autocomplete (Ctrl+Space).
Try to solve your own problems.

4 Likes

WOW.............
WHY WAS THIS HIDDEN FROM ME........

:smiling_face_with_tear:

I will forever use this:

hello @PGriffith
I used what you recomanded me but, it not work. But maybe I forgot something

this my dataset:

scrip:

DS = self.view.getChild("root").custom.DataStatusAlarmes
	headers = system.dataset.getColumnHeaders(DS)
	system.dataset.toExcel(headers, DS, "C:\\TESTE\\results1.xls")

I get this error:

com.inductiveautomation.ignition.common.script.JythonExecException
Traceback (most recent call last):
  File "<function:runAction>", line 6, in runAction
TypeError: can't convert [u'62d04597-0f49-4930-80f5-2d302efa155f', u'prov:default:/tag:Sensor/Sensor7/F8:/alm: High Alarm', u'Sensor 7  High Alarm', Wed May 08 15:13:42 CEST 2024, 0, 3] to com.inductiveautomation.ignition.common.Dataset

	at org.python.core.Py.TypeError(Py.java:236)
	at org.python.core.Py.tojava(Py.java:568)
	at com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities.lambda$toExcel$1(DatasetUtilities.java:1022)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
	at java.base/java.util.Iterator.forEachRemaining(Unknown Source)
	at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
	at java.base/java.util.stream.AbstractPipeline.copyInto(Unknown Source)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
	at java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source)
	at java.base/java.util.stream.AbstractPipeline.evaluateToArrayNode(Unknown Source)
	at java.base/java.util.stream.ReferencePipeline.toArray(Unknown Source)
	at com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities.toExcel(DatasetUtilities.java:1022)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
	at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:190)
	at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:208)
	at org.python.core.PyObject.__call__(PyObject.java:494)
	at org.python.core.PyObject.__call__(PyObject.java:498)
	at org.python.pycode._pyx1823.runAction$1(<function:runAction>:27)
	at org.python.pycode._pyx1823.call_function(<function:runAction>)
	at org.python.core.PyTableCode.call(PyTableCode.java:173)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:306)
	at org.python.core.PyFunction.function___call__(PyFunction.java:474)
	at org.python.core.PyFunction.__call__(PyFunction.java:469)
	at org.python.core.PyFunction.__call__(PyFunction.java:464)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:846)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:828)
	at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runFunction(ProjectScriptLifecycle.java:832)
	at com.inductiveautomation.ignition.common.script.ScriptManager$ScriptFunctionImpl.invoke(ScriptManager.java:1009)
	at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$AutoRecompilingScriptFunction.invoke(ProjectScriptLifecycle.java:897)
	at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:158)
	at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:97)
	at com.inductiveautomation.perspective.gateway.action.ScriptAction.runAction(ScriptAction.java:74)
	at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.lambda$call$0(ActionCollection.java:263)
	at com.inductiveautomation.perspective.gateway.api.LoggingContext.mdc(LoggingContext.java:54)
	at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.call(ActionCollection.java:252)
	at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.call(ActionCollection.java:221)
	at com.inductiveautomation.perspective.gateway.threading.BlockingTaskQueue$TaskWrapper.run(BlockingTaskQueue.java:154)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at com.inductiveautomation.perspective.gateway.threading.BlockingWork$BlockingWorkRunnable.run(BlockingWork.java:58)
	at java.base/java.lang.Thread.run(Unknown Source)
Caused by: org.python.core.PyException
Traceback (most recent call last):
  File "<function:runAction>", line 6, in runAction
TypeError: can't convert [u'62d04597-0f49-4930-80f5-2d302efa155f', u'prov:default:/tag:Sensor/Sensor7/F8:/alm: High Alarm', u'Sensor 7  High Alarm', Wed May 08 15:13:42 CEST 2024, 0, 3] to com.inductiveautomation.ignition.common.Dataset

	... 46 more

Ignition v8.1.27 (b2023042509)
Java: Azul Systems, Inc. 11.0.18

I found solution using: spreadsheet = system.dataset.dataSetToExcel(1, [DS]) and system.file.writeFile(filePath, spreadsheet)
It work.

For a simple export to.csv and download this works onClick Event

def runAction(self, event):

	
	component = self.getSibling("tblWorkRequest")

	# Convert the dataset to CSV
	csv = system.dataset.toCSV(component.props.data)
	
	# Initiate a file download
	system.perspective.download("solicitudesOT.csv", csv)

tblWorkRequest is the table name, use the hierarchy u need.

Hi Alejandro,

Yes this works.
but when I open the CSV
all the data points are concentrated in a 1 cell.

is there a way to fomat the csv before exporting to csv?

Hello!

You may try:

def runAction(self, event):
    component = self.getSibling("tblWorkRequest")
    
    # Convert the dataset to CSV using commas as delimiter
    csv = system.dataset.toCSV(component.props.data, 1)
    

    system.perspective.download("solicitudesOT.csv", csv)

Thanks Alejandro,

Do you have the link for the manual for this script function?

No, I don´t have a Manual for this, you may research online.

it fixes the 1 concentratrated cell issue but it puts all table data in 1 row

You may research online or in this forum.

try:

def runAction(self, event):
    component = self.getSibling("tblWorkRequest")
    
 
    csv = system.dataset.toCSV(component.props.data, 1)
    
    # Encode as UTF-8 with proper line breaks
    csvBytes = csv.encode('utf-8')
    
 
    system.perspective.download("solicitudesOT.csv", csvBytes)

Cheers!

def runAction(self, event):
    component = self.getSibling("tblWorkRequest")
    
 
    csv = system.dataset.toCSV(component.props.data, 1)
    
    # Encode as UTF-8 with proper line breaks
    csvBytes = csv.encode('utf-8')
    
 
    system.perspective.download("solicitudesOT.csv", csvBytes)
1 Like
2 Likes