/**********************************************************/ /* 函数名称:uf_dwsaveas_excel 功能 :将数据窗口数据导出EXCEL文件,并将EXCEL文件默认英文标题替换成中文。 参数 :datawindow datawin,为用户要导出数据窗口的数据窗口控件名 返回值:integer 1,success;-1,error 流程描述:先用saveasAscii()倒出为excel文件,再替换表头为中文名 原因:为提高程序的可读性作了少量的修改(如:增加注释、改变排版风格等)*//**********************************************************/
/***************以下程序将导出为EXCEL文档******************/integer li_rtn,ii,li_ascstring ls_name,ls_pathnameboolean lb_existif datawin.RowCount()<1 then MessageBox("提示信息","请先检索数据再导出至Excel!") return -1 //errorend ifli_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 ifelse return -1end if
/**********以下程序将导出的EXCEL英文标题替换为汉字*********/long numcols , numrows , c, rOLEObject xlapp , xlsubint retnumcols = 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 -1end 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_textNEXT
xlApp.DisConnectObject()Destroy xlappMessageBox("提示信息","导出数据成功!") return 1 //success