MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.


I'm not curious

PowerBuilder Datawindow Save as - Save Datawindow data in different formats

Published on 17 January 17
3392
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
// 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:
View Comment (1)
Post a Comment

Please notify me the replies via email.

Important:
  • We hope the conversations that take place on MyTechLogy.com will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
  1. 18 January 17
    1

    Thank you for sharing. Great to see you back.

You may also be interested in
 
Awards & Accolades for MyTechLogy
Winner of
REDHERRING
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url

Back to Top