PowerBuilder Datawindow Save as - Save Datawindow data in different formats
Published on 17 January 17
1
3
// I have created a window and I placed a datawindow dw_1 on it
// I put a Button on the window and I am writing the below code in clicked event to save the data window in different formats
// If the data is more than 1048576 rows(latest xlsx will support only thismuch rows), we will save the data in text/csv format else we can save data in xlsx format
string sFileName, sFileName2, sFileNameCSV, sExt, ls_path
int li_res
IF dw_1.RowCount() > 1048575 THEN
// 1,048,576 --New xlsx supports
// old - xls - support only 65535 rows
li_res = GetFileSaveName ("Save Data As...", sFileName, sFileName2, "CSV", "Comma Seperated File (*.CSV), *.CSV," + &
"Text File (*.TXT), *.TXT," )
ELSE
li_res = GetFileSaveName ("Save Data As...", sFileName, sFileName2, "XLSX", "Microsoft Excel (*.XLSX), *.XLSX," +"Comma Seperated File (*.CSV), *.CSV," + &
"Text File (*.TXT), *.TXT," )
END IF
sExt = Right (sFileName, 3)
if li_res = 1 then
Choose Case Upper(sExt)
Case "TXT"
li_res = dw_1.SaveAs (sFileName, Text!, true)
Case "CSV"
li_res = dw_1.SaveAs (sFileName, CSV!, true)
Case "LSX"
ls_path ='D:\Users\Test\Downloads\'
IF Not( FileExists(ls_path)) THEN
CreateDirectory(ls_path)
END IF
sFileNameCSV = ls_path + Mid(sFileName2, 1, Len(sFileName) -5) + 'xtemp_tempx.CSV'
FileDelete(sFileNameCSV)
li_res = dw_1.SaveAs (sFileNameCSV, CSV!, true)
CASE ELSE
MessageBox("Invalid File Format",sExt + ' is an invalid file format~nPlease try again')
End Choose
if li_res = -1 then
MessageBox("Failed","Failed to save report")
end if
elseif li_res = 0 then
MessageBox("Cancelled","Cancelled")
else
MessageBox("Failed","Failed to save report")
end if
IF Upper(sExt) = "LSX" THEN
oleobject lole_Object
lole_Object = CREATE oleobject
If lole_Object.ConnectToNewObject("excel.application") <> 0 Then
Messagebox("File Conversion", "Cannot convert the file from CVS. Unable to connect to Excel.", Exclamation!)
DESTROY(lole_Object)
End If
lole_Object.WorkBooks.Open(sFileNameCSV)
lole_Object.Application.DisplayAlerts = False
lole_Object.ActiveWorkbook.SaveAs(sFileName, 51)
lole_Object.Application.DisplayAlerts = True
lole_Object.activeworkbook.saved=true
lole_Object.application.quit()
lole_Object.disconnectobject()
DESTROY(lole_Object)
FileDelete(sFileNameCSV)
END IF
This blog is listed under
Development & Implementations
and Data & Information Management
Community
Related Posts:
You may also be interested in
Share your perspective

Share your achievement or new finding or bring a new tech idea to life. Your IT community is waiting!
Thank you for sharing. Great to see you back.