# Send data from db to excel

**URL:** <https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713>\
**Category:** Ignition\
**Created:** [March 2, 2022, 12:28pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713 "2022-03-02T12:28:17Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![eren.uyanik](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/eren.uyanik/32/30923_2.png) [@eren.uyanik](https://forum.inductiveautomation.com/u/eren.uyanik)\
**Post date:** [March 2, 2022, 12:28pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/1 "2022-03-02T12:28:17Z")

</div>

Hello all,

I have some recorded data in Mssql, I’m sending these data over ignition to mssql. I need get a table of sql from ignition to excel now but there isn’t any output when I run the script as manual.

```auto
if str(currentValue.quality).upper() == 'GOOD' and str(previousValue.quality).upper() == 'GOOD':
	if (currentValue.value > 0):
		results = system.db.runQuery("SELECT * FROM MyTable", 'MyServer')			
		spreadsheet = system.dataset.toExcel(True, [results])
		filePath = "C:\\Users\\test1\\Desktop\\results.xls"
		system.file.writeFile(filePath, spreadsheet)

```

I set memory tag as 1 and check the diagnostic result, I can’t not any problem. But there isn’t any created file when I check the destination path. What could be the problem? What am I doing wrong?

Thanks

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 2, 2022, 1:07pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/2 "2022-03-02T13:07:54Z")

</div>

Context is important. Are you running this on a gateway script? Then you should be checking this on the gateway server at that file location. If you’re running it on a client script, then check the client computer. So first make sure you are checking the right place. Always a good idea to think about your execution context when debugging.

If this isn’t working at all you should be getting some error somewhere. This looks like you’re running it in on the gateway so you should check your server logs and see what error message you are getting since nothing is happening.

My guess would be that it’s a type issue. `system.db.runNamedQuery` returns a basic dataset while `system.db.runQuery` like you’re using returns a PyDataSet.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/8/b841d80664633664b5803531c4338f4a53b80424.png)  
In the documentation for `system.dataset.toExcel` they use a named query so they feed it a basic dataset. So you may want to try turning your runQuery into a namedQuery or do a `system.dataset.toDataSet()` around your runQuery statement - toExcel might be able to handle PyDataSets, but I can’t tell that from the documentation, and this would eliminate one possibility.

---

<div class="post-metadata">

**Author:** ![eren.uyanik](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/eren.uyanik/32/30923_2.png) [@eren.uyanik](https://forum.inductiveautomation.com/u/eren.uyanik)\
**Post date:** [March 3, 2022, 5:39am UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/3 "2022-03-03T05:39:57Z")

</div>

Thanks a lot for your detail answer. I don’t use gateway or specific client scripts. I just created a memory tag named “export\_excel” in “Tag Browser” and wrote the script in my previous message into it. While trying the script, I manually set the following memory tag to 1.

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

But when I check the manual and Ignition University, all excel examples are running on the perspective. I’m not sure this method running just perspective side.

Thanks

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 3, 2022, 1:35pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/4 "2022-03-03T13:35:28Z")

</div>

You may not be writing these scripts with a context in mind but they are executing in a specific context always.

Since you’re dealing with files/folders its actually quite important as well as with queries. Here’s the differences -

Gateway:  
Files/Folder Operations - You are creating/reading/updating files/folders on the computer that the gateway is hosted on.  
Database queries - You must specify which database connection you are using (or if using a named query which project it comes from)

Vision Client:  
Files/Folder Operations - You are creating/reading/updating files/folders on the computer that the client is using the project on. Computer A is hosting the gateway and computer B opens a Vision project where a button does creates an excel file in a script, that folder is being created on computer B’s file system.  
Database queries - You can set the default database connection for the project and omit these.

All jython code in perspective is run in the gateway. So if on a perspective button you say "make this excel sheet on `"C:\\Users\\test1\\Desktop\\results.xls"`" - this is being made on the computer that is hosting the Igniton gateway, not the computer of the person visiting the perspective application.

From you’re reply I am assuming that you are running this script from a perspective project is that correct? If so I would expect the excel file to be on the gateway computer.

Not quite sure what you mean with that memory tag - are you just using it to debug? Post your updated script.

---

<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:** [March 3, 2022, 2:39pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/5 "2022-03-03T14:39:04Z")

</div>

@bkarabinchak.psi is right on top of it. Tag Event scripts execute in the Gateway Context. So assuming the script produces no errors the file will be located on the computer hosting the Gateway Server.

---

<div class="post-metadata">

**Author:** ![eren.uyanik](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/eren.uyanik/32/30923_2.png) [@eren.uyanik](https://forum.inductiveautomation.com/u/eren.uyanik)\
**Post date:** [March 8, 2022, 4:58am UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/6 "2022-03-08T04:58:42Z")

</div>

Such a great answer!  
I checked the gateway server and found the my files. Thanks!

But I’m wondering one thing, can’t I send this excel to my local computer where Ignition Designer is running?

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [March 8, 2022, 10:23am UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/7 "2022-03-08T10:23:04Z")

</div>

perspective has no acces to your local computer, you can trigger a download of the file though.  
In which case you dont even need to create a file on your gateway.

you should be able to download the excel from [system.dataset.toExcel - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/system.dataset.toExcel)  
directly with  
[system.perspective.download - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/system.perspective.download)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 8, 2022, 2:58pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/8 "2022-03-08T14:58:31Z")

</div>

Nothing super important, but this:

```python
if str(currentValue.quality).upper() == 'GOOD' and str(previousValue.quality).upper() == 'GOOD':

```

could be written like this:

```python
if currentValue.quality.isGood() and previousValue.quality.isGood():

```

Might as well use the built-in methods.

---

<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:** [March 8, 2022, 3:15pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/9 "2022-03-08T15:15:40Z")

</div>

{ Pedant Mode }  
Might as well use jython’s automatic NetBeans optimizations consistently, too…

```auto
if currentValue.quality.good and previousValue.quality.good:

```

{ / Pedant Mode }

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 8, 2022, 3:18pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/10 "2022-03-08T15:18:58Z")

</div>

Didn’t know it was any different. Thought it was just a ‘shortcut’.  
Thanks for the info, I’ll use this now. Maybe. I kind of prefer the meaning `isGood` brings, even if I dislike the case.

---

<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:** [March 8, 2022, 3:24pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/11 "2022-03-08T15:24:16Z")

</div>

It _ **is** _ a shortcut. Like `.quality` above is short for `.getQuality()`. Jython automatically recognizes Java getters and setters that follow the NetBeans pattern and treats them as jython object attributes.

- `object.getSomething()` can be replaced with `object.something`.
- `object.setSomething(newValue)` can become `object.something = newValue`.
- For booleans, `object.isSomething()` becomes `object.something`.

This is a standard across the Java ecosystem (JSP, Struts, et cetera).

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [March 8, 2022, 3:36pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/12 "2022-03-08T15:36:04Z")

</div>

@pturmel Just for clarification. Are these syntactical shortcuts, or are they actual code shortcuts?

IE Do they actually change the code that is executed, or do they eventually map to the same code execution?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 8, 2022, 3:45pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/13 "2022-03-08T15:45:53Z")

</div>

If it’s just a shortcut, then I’d rather stick with `.isGood`. Even if using `.quality`.  
It doesn’t get any clearer than `.quality.isGood()`, that’s basically an english sentence !

---

<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:** [March 8, 2022, 4:10pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/14 "2022-03-08T16:10:36Z")

</div>

Actual executable code difference.

- `.isGood()` is attribute lookup (java) to obtain the `isGood` method, followed by the parentheses operator (jython interpreter) to invoke the method (back into java).
- `.good` is attribute lookup that includes the method call, entirely in java.

Always use the Netbeans shortcuts if performance matters to you.

---

<div class="post-metadata">

**Author:** ![eren.uyanik](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/eren.uyanik/32/30923_2.png) [@eren.uyanik](https://forum.inductiveautomation.com/u/eren.uyanik)\
**Post date:** [March 11, 2022, 12:32pm UTC](https://forum.inductiveautomation.com/t/send-data-from-db-to-excel/56713/15 "2022-03-11T12:32:44Z")

</div>

I found my exported excel file in the pc of gateway server. Thanks a lot for answers!
