Exporting a Cross Table to an Excel and sending that excel over an email across.

Hi All,

The below code inserts the data in the mail body and sends it. I am trying to export the cross table of a visualization to excel and then send the same as an attachment over an email.

Could you please assist me in acheiving this.

from Spotfire.Dxp.Data import DataTable from cStringIO import StringIO from System.Net.Mail import MailMessage, SmtpClient def sendMail( toStr, fromStr, subjectStr, bodyStr ): mailObj = MailMessage( toStr, fromStr, subjectStr, bodyStr ) smtpServer = SmtpClient("smtp.XXXX.com") smtpServer.Send( mailObj ) def cleanField( aString ): if aString == "(Empty)": return "" if aString.find( "\"" ) >= 0: aString = aString.replace( "\"", "'" ) if aString.find( "\n" ) >= 0: aString = "\"" + aString + "\"" return aString def convertTableToCSV( dataTable ): rows = dataTable.Select( "1=1" ).AsIndexSet() cols = dataTable.Columns result = StringIO() data=[] for rowNum, aRow in enumerate( rows ): if rowNum == 0: for colNum, aCol in enumerate( cols ): if colNum > 0: result.write( "," ) result.write( aCol.Name ) result.write( "\n" ) for colNum, aCol in enumerate( cols ): if colNum > 0: result.write( "," ) result.write( cleanField( aCol.RowValues.GetFormattedValue( aRow ) ) ) result.write( "\n" ) return result.getvalue() aDataTable = Document.Data.Tables[ "XXXX" ] csvData = convertTableToCSV( aDataTable ) sendMail( "from@email.com", "to@email.com", "Spotfire-Data", csvData )

 

Thanks

San

(1) Answer

Login