# dataSetToExcel - corrupt file when attaching file to email

**URL:** <https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [January 31, 2022, 12:49pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547 "2022-01-31T12:49:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![soundwavesct](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@soundwavesct](https://forum.inductiveautomation.com/u/soundwavesct)\
**Post date:** [January 31, 2022, 12:49pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/1 "2022-01-31T12:49:01Z")

</div>

So I have a simple nightly email that sends a HTML report in the body of an email (works great). I am trying to attach the same data as an Excel attachment but when you open it Windows thinks the file is corrupt. I tried to change file extensions, that didn’t work at all and the process seems so straight forward I am not sure what to look for.

```auto
#Run the query 
	data = system.db.runQuery(queryReport, 'xxx')
	lengthDue = data.getRowCount()
		
	if lengthDue > 0:
		#NEW DATASET Ready for email and Excel conversions
		rawData = system.dataset.toDataSet(data)
		
		#Convert DataSet to Excel and get ready to attach to our email
		excelstr = system.dataset.dataSetToExcel(1,[rawData])
		excelbytes = excelstr.encode("UTF8")		
		filename = "Summary.xls"

		#Define Email Distribution List			
		recipients = system.db.runScalarQuery("SELECT EmailList FROM ReportDistList WHERE ReportName='Awaiting Inspection Summary'", 'xxx')
		#Put recipients into a Python list		
		EmailRecipients = [item.strip() for item in recipients.split(',')]	
		
		myEmail = ['xxx@xxx.com']		
		
		#Convert DataSet to HTML For Sending In EMAIL
		EmailHTML = system.dataset.dataSetToHTML(1, rawData, 'Awaiting Inspection Summary')
		Emailbody = '''
		''' + '<h1>Awaiting Inspection Daily Summary</h1>' + '''
		''' + EmailHTML + '<br/>This is an automated email please do not respond'
		system.net.sendEmail(smtpProfile='xxxEmail', fromAddr='xxx@xxx.com',
				subject='Awaiting Inspection Daily Summary', body=Emailbody, html=True, to=EmailRecipients, bcc=myEmail,attachmentNames=[filename],attachmentData=[excelbytes])

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [January 31, 2022, 1:04pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/2 "2022-01-31T13:04:17Z")

</div>

Have you tired it without this line?

> [@soundwavesct](#):
>
> `excelbytes = excelstr.encode("UTF8")	`

---

<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:** [January 31, 2022, 1:04pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/3 "2022-01-31T13:04:39Z")

</div>

I have never used those functions but...

> [@soundwavesct](#):
>
> `attachmentData=[excelbytes]`

> [@soundwavesct](#):
>
> `excelbytes = excelstr.encode("UTF8")`

system.dataset.toExcel returns a byte array.  
and  
attachmentData must be a byte array

So i doubt you should encode it  
and i dont think if you need to but another array around it

---

<div class="post-metadata">

**Author:** ![soundwavesct](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@soundwavesct](https://forum.inductiveautomation.com/u/soundwavesct)\
**Post date:** [January 31, 2022, 1:16pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/4 "2022-01-31T13:16:22Z")

</div>

Yes, makes no difference. Just left it in from an example I found with similar issues.

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [January 31, 2022, 1:24pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/5 "2022-01-31T13:24:14Z")

</div>

If you download the excel file, is it also corrupted? Or is it only corrupt when you email it?

---

<div class="post-metadata">

**Author:** ![soundwavesct](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@soundwavesct](https://forum.inductiveautomation.com/u/soundwavesct)\
**Post date:** [January 31, 2022, 1:27pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/6 "2022-01-31T13:27:54Z")

</div>

Thanks, I removed it but nothing changes.

---

<div class="post-metadata">

**Author:** ![soundwavesct](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@soundwavesct](https://forum.inductiveautomation.com/u/soundwavesct)\
**Post date:** [January 31, 2022, 1:36pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/7 "2022-01-31T13:36:37Z")

</div>

Just tried it, same results. Still corrupt. The data is there, but Excel certainly doesn’t like it.

---

<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:** [January 31, 2022, 2:03pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/8 "2022-01-31T14:03:05Z")

</div>

Maybe convert it to a csv. thats much more readable and excel an also open it

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [January 31, 2022, 2:35pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/9 "2022-01-31T14:35:28Z")

</div>

Try it with system.dataset.toExcel(), rather than:

> [@soundwavesct](#):
>
> `excelstr = system.dataset.dataSetToExcel(1,[rawData])`

According to the docs, the function you are using is depricated, that might be why its not working.  
[https://docs.inductiveautomation.com/display/DOC81/system.dataset.toExcel](https://docs.inductiveautomation.com/display/DOC81/system.dataset.toExcel)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 31, 2022, 2:38pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/10 "2022-01-31T14:38:34Z")

</div>

Does it still open? If so, it’s in the way the xml is constructed by the function, and has always been that way, IIRC.

toExcel() overcame that.

---

<div class="post-metadata">

**Author:** ![william.ivey](https://avatars.discourse-cdn.com/v4/letter/w/aca169/32.png) [@william.ivey](https://forum.inductiveautomation.com/u/william.ivey)\
**Post date:** [January 31, 2022, 2:43pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/11 "2022-01-31T14:43:57Z")

</div>

Not sure if this may help or not…

If you are not formatting the spreadsheet, would a CSV export/file be a better option?

---

<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:** [January 31, 2022, 3:06pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/12 "2022-01-31T15:06:35Z")

</div>

> [@soundwavesct](#):
>
> Still corrupt. The data is there, but Excel certainly doesn’t like it.

What is your indicator that the file is corrupt? If excel is showing data, then I don't think it is truly corrupt.

---

<div class="post-metadata">

**Author:** ![soundwavesct](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@soundwavesct](https://forum.inductiveautomation.com/u/soundwavesct)\
**Post date:** [January 31, 2022, 3:08pm UTC](https://forum.inductiveautomation.com/t/datasettoexcel-corrupt-file-when-attaching-file-to-email/55547/13 "2022-01-31T15:08:33Z")

</div>

Thanks all, using .toExcel immediately cured the issue and allowed me to use .xlsx, a bonus.

I can’t believe I didn’t find that when doing this earlier. It’s to easy to find old info I suppose.
