Exporting to Excel (from client only)

# Script that exports a file from client (not webplayer)

import System

from System.IO import FileStream, FileMode

from Spotfire.Dxp.Application.Visuals import TablePlot
from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import SaveFileDialog

# GETS THE FILE PATH FROM THE USER THROUGH A FILE DIALOG
SaveFile = SaveFileDialog()
SaveFile.Filter = "Spotfire Text Data Format (*.xlsx)|*.xls"
SaveFile.ShowDialog()

for vis in Application.Document.ActivePageReference.Visuals: 
if  vis.Title == "ExportKPI" : 
vizTable = vis.As[TablePlot]()


if SaveFile.FileName=="":
    # user does not select a file (cancel button)
    saveFilename = ""
else:
    saveFilename = SaveFile.FileName
    print "saveFilename=", saveFilename

    # Export Table data to the file
    try:
        stream = FileStream(saveFilename, FileMode.Create)
        vizTable.ExportData(DataWriterTypeIdentifiers.ExcelXlsxDataWriter, stream)
        #vizTable.As[TablePlot]().ExportData(DataWriterTypeIdentifiers.StdfDataWriter, stream)
        print "stream.Length = ", stream.Length
    finally:
        stream.Dispose()




Previous
Next Post »