# Button that exports dataset to new excel file each time it is pressed

**URL:** <https://forum.inductiveautomation.com/t/button-that-exports-dataset-to-new-excel-file-each-time-it-is-pressed/62390>\
**Category:** Ignition\
**Tags:** scripting, vision\
**Created:** [July 28, 2022, 7:09am UTC](https://forum.inductiveautomation.com/t/button-that-exports-dataset-to-new-excel-file-each-time-it-is-pressed/62390 "2022-07-28T07:09:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brennan\_Joles](https://avatars.discourse-cdn.com/v4/letter/b/d2c977/32.png) [@Brennan\_Joles](https://forum.inductiveautomation.com/u/Brennan_Joles)\
**Post date:** [July 28, 2022, 7:09am UTC](https://forum.inductiveautomation.com/t/button-that-exports-dataset-to-new-excel-file-each-time-it-is-pressed/62390/1 "2022-07-28T07:09:29Z")

</div>

I have made a button to write the last week of data from a dataset to an excel file. It works great except each time I press it it overwrites the last time I pressed it in the excel file. I would like a button that creates a new file or a new sheet in the file each time I export the data. How would I do this? Here is my code below:

> params = {"StartDate":system.date.now(),"EndDate":system.date.addHours(system.date.now(), 7)}  
> results = system.db.runNamedQuery("Downtime/FEXW/WestExitDTTrueOEEExcel", params)  
> spreadsheet = system.dataset.toExcel(True, [results])  
> filePath = "C:\Users\pjolesbr\OneDrive - Clarios\Projects\Downtime Project\OEE MTTR MTBF.xlsx"  
> system.file.writeFile(filePath, spreadsheet)

---

<div class="post-metadata">

**Author:** ![jeroen\_smits](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jeroen\_smits](https://forum.inductiveautomation.com/u/jeroen_smits)\
**Post date:** [July 28, 2022, 7:44am UTC](https://forum.inductiveautomation.com/t/button-that-exports-dataset-to-new-excel-file-each-time-it-is-pressed/62390/2 "2022-07-28T07:44:33Z")

</div>

you can put a timestamp into the filepath. this way your filepath is always unique and it will not overwrite the same file.

example:

filePath = “C:\Users\pjolesbr\OneDrive - Clarios\Projects\Downtime Project\OEE MTTR MTBF”+ str(system.date.now()) + “.xlsx”

result: ‘C:\Users\pjolesbr\OneDrive - Clarios\Projects\Downtime Project\OEE MTTR MTBFThu Jul 28 09:43:12 CEST 2022.xlsx’

in addition you can format system.date.now() to whatever format works for you.

---

<div class="post-metadata">

**Author:** ![Brennan\_Joles](https://avatars.discourse-cdn.com/v4/letter/b/d2c977/32.png) [@Brennan\_Joles](https://forum.inductiveautomation.com/u/Brennan_Joles)\
**Post date:** [July 28, 2022, 7:57am UTC](https://forum.inductiveautomation.com/t/button-that-exports-dataset-to-new-excel-file-each-time-it-is-pressed/62390/3 "2022-07-28T07:57:55Z")

</div>

Thank you I figured this out but I kept getting errors. Turns out I didn’t think about how you cannot have colons in filenames. I reformatted the date using periods instead of colons and dashes instead of slashes and it worked!
