# Behavior of system.dataset.exportExcel

**URL:** <https://forum.inductiveautomation.com/t/behavior-of-system-dataset-exportexcel/83093>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [December 13, 2023, 3:36pm UTC](https://forum.inductiveautomation.com/t/behavior-of-system-dataset-exportexcel/83093 "2023-12-13T15:36:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [December 13, 2023, 3:36pm UTC](https://forum.inductiveautomation.com/t/behavior-of-system-dataset-exportexcel/83093/1 "2023-12-13T15:36:04Z")

</div>

I have this code on a simple "Export data to Excel" button for our end users of a certain Vision application:

```auto
try:
	path = system.dataset.exportExcel(fileName,1,data,1)
except FileNotFoundError:
	system.gui.errorBox("File is in use. Please save this file under a different name or close the existing file, then try again.", "Export Failure")

```

But when trying to export over a file that is in use, I can't get it to actually reach the "Except" block. It always throws an error while executing the system.dataset.exportExcel function, shown below:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/b/7be068611787ccfc485a80487a6199ad45f14de4.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/e/de1507be8bcbd803e0763e8f96a53100329f8eef.png)

Is there anything that I can do to make the try/except logic work as intended?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [December 13, 2023, 3:43pm UTC](https://forum.inductiveautomation.com/t/behavior-of-system-dataset-exportexcel/83093/2 "2023-12-13T15:43:09Z")

</div>

You're catching a Python FileNotFoundError, not the Java FileNotFound_Exception_. You can either catch bare java.lang.Exception, or import the specific error and catch it.

Another option is to separate the file choosing from the writing; system.file.saveFile will give you a path that you can then check whether a file exists at, and then manually write out your dataset with system.dataset.toExcel.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 13, 2023, 3:44pm UTC](https://forum.inductiveautomation.com/t/behavior-of-system-dataset-exportexcel/83093/3 "2023-12-13T15:44:47Z")

</div>

Jython's normal exceptions don't cover exceptions generated by Java. Use code construct like this (preferably in a project library):

```auto
from java.lang import Throwable

def someFunction(someDataset, someFile):
	try:
		bytes = system.dataset.toExcel(someDataset)
		system.file.write(someFile, bytes)
	except Exception, e:
		print "Jython Exception", e
	except Throwable, t:
		print "Java Exception", t

```

I normally use loggers, though, and use `PythonAsJavaException()` (from later.py) to convert python exceptions to a java format that is logger-friendly.

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [December 13, 2023, 4:20pm UTC](https://forum.inductiveautomation.com/t/behavior-of-system-dataset-exportexcel/83093/4 "2023-12-13T16:20:11Z")

</div>

Thank you, that makes sense -- I am still learning how Jython works under the hood. I modified the script, and it _should_ be working but it's still throwing the same error message instead of reaching any of these "test" except blocks. You can see I'm trying to import a few things to see what works, but none of them are working... 🤔

```auto
from java.lang import Throwable
from java.lang import Exception as JException1
from java.io import FileNotFoundException as JException2

data = event.source.parent.getComponent('MyTable').viewDataset
fileName = "MyReport.xlsx"

try:
	path = system.dataset.exportExcel(fileName,1,data,1)
except Throwable, a:
	print "A: ", a
except JException1, b:
	print "B: ", b
except JException2, c:
	print "C: ", c

```

If I try overwriting an existing file named MyReport.xlsx, I continue seeing the error that is pictured in the original post. Is it best practice to use the 'bytes' method instead of directly using `system.dataset.exportExcel`?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [December 13, 2023, 4:24pm UTC](https://forum.inductiveautomation.com/t/behavior-of-system-dataset-exportexcel/83093/5 "2023-12-13T16:24:52Z")

</div>

> [@Bushmatic](#):
>
> You can see I'm trying to import a few things to see what works, but none of them are working... 🤔

Where are you executing this script and where are you looking for the output?

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [December 13, 2023, 4:27pm UTC](https://forum.inductiveautomation.com/t/behavior-of-system-dataset-exportexcel/83093/6 "2023-12-13T16:27:05Z")

</div>

It's within an "actionPerformed" event handler script on a button in a Vision window. I'm looking at the output console for the "print" statements, and if all goes well, I will get the error box that I put in the original post.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [December 13, 2023, 4:37pm UTC](https://forum.inductiveautomation.com/t/behavior-of-system-dataset-exportexcel/83093/7 "2023-12-13T16:37:05Z")

</div>

I suppose I wasn't clear. Are you testing in a true client or the designer?

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [December 13, 2023, 4:40pm UTC](https://forum.inductiveautomation.com/t/behavior-of-system-dataset-exportexcel/83093/8 "2023-12-13T16:40:47Z")

</div>

Both! Same behavior happens either way
