PB的datawindow導出到excel文件(使用saveasascii)


**********************************************************/
/*函數名稱:uf_dwsaveas_excel
功能:將數據窗口數據導出EXCEL文件,並將EXCEL文件默認英文標題替換成中文。
參數:datawindow datawin,為用戶要導出數據窗口的數據窗口控件名
返回值:integer 1,success;-1,error
流程描述:先用saveasAscii()倒出為excel文件,再替換表頭為中文名
 
        原因:為提高程序的可讀性作了少量的修改(如:增加注釋、改變排版風格等)*/
/**********************************************************/

/***************以下程序將導出為EXCEL文檔******************/
integer li_rtn,ii,li_asc
string ls_name,ls_pathname
boolean lb_exist
if datawin.RowCount()<1 then
MessageBox("提示信息","請先檢索數據再導出至Excel!")
return -1//error
end if
li_rtn=GetFileSaveName("保存文件",ls_pathname,ls_name,"xls","Excel文件(*.xls),*.xls")

if li_rtn=1 then
lb_exist = FileExists(ls_pathname)
IF lb_exist THEN
li_rtn = MessageBox("保存", ls_pathname+"已經存在,是否覆蓋?",Exclamation!, YesNo!)
end if
if li_rtn=1 then
//當文件存在用戶選擇覆蓋,或是文件本就不存在時。注意變量li_rtn
li_rtn=datawin.SaveAsAscii(ls_pathname)
if li_rtn=1 then
//MessageBox("提示信息","導出數據成功!")
else
MessageBox("錯誤信息","導出數據失敗!")
return -1//error
end if
else
return -1//error
end if
else
return -1
end if

/**********以下程序將導出的EXCEL英文標題替換為漢字*********/
long numcols , numrows , c, r
OLEObject xlapp , xlsub
int ret
numcols = long(datawin.Object.DataWindow.Column.Count)
numrows = datawin.RowCount()

// 產生oleobject的實例
xlApp = Create OLEObject

//連接ole對象
ret = xlApp.ConnectToNewObject( "Excel.Sheet" )
if ret < 0  then
MessageBox("連接失敗!","連接到EXCEL失敗,請確認您的系統是否已經安裝EXCEL!~r~n"&
+"錯誤代碼:"+string(ret))
    return -1
end if
// 打開EXCEL文件
xlApp.Application.Workbooks.Open(ls_pathname)
////使文件可見
//xlApp.Application.Visible = true

// 得到活動工作表的引用,改善程序性能
xlsub = xlapp.Application.ActiveWorkbook.Worksheets[1]
string ls_colname,ls_text,ls_modistr,ls_col
//取字段名更改為對應的文本text值
FOR c=1 to numcols
ls_col="#"+string(c)+".name"
ls_colname=datawin.describe(ls_col)
ls_modistr=ls_colname+"_t.text"
ls_text=datawin.describe(ls_modistr)
xlsub.cells[1,c]=ls_text
NEXT

xlApp.DisConnectObject()
Destroy xlapp
MessageBox("提示信息","導出數據成功!")
return 1//success


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM