export cross table to excel - error opening in excel


I used this  scripts to export. the data was exported but when I am opening in excel gives me an error. Excel cannot open the file testexport.xlsx because the file format or file extension is not valid. If rename to testexport.xls and open the in excel gives an error fileformat and extension of testexport.xls dont math. if I open it shows the data. and all the records shows.

here is my script that I am using

import System
from System.IO import FileStream, FileMode
from Spotfire.Dxp.Application.Visuals import TablePlot
import clr


from System.Windows.Forms import SaveFileDialog
SaveFile = SaveFileDialog()
SaveFile.FileName = "ExportData.Xlsx"
SaveFile.Filter =  "xlsx Format (*.xlsx)|*.xlsx|*.xls|*.xlsx" # "Excel Workbook (*.xls)|*.xls"

saveFilename = SaveFile.FileName
from Spotfire.Dxp.Application.Visuals import VisualContent,TablePlotColumnSortMode
from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
from System.IO import File, StreamWriter

writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.ExcelXlsxDataWriter) 
stream = StreamWriter(saveFilename)
ExpData = myCrossTable.As[VisualContent]()
ExpData. ExportText(stream)


Much appreciated for your help.



(2) Answers