ehxz 发表于 2007-6-13 19:14:08

一个简单的DW导出XLS的函数,不错的了

&nbsp;/**********************************************************/ &nbsp; <br/>&nbsp; /* 函数名称:uf_dwsaveas_excel &nbsp; <br/>&nbsp; 功能 :将数据窗口数据导出EXCEL文件,并将EXCEL文件默认英文标题替换成中文。 &nbsp; <br/>&nbsp; 参数 :datawindow &nbsp; datawin,为用户要导出数据窗口的数据窗口控件名 &nbsp; <br/>&nbsp; 返回值:integer &nbsp; 1,success;-1,error &nbsp; <br/>&nbsp; 流程描述:先用saveasAscii()倒出为excel文件,再替换表头为中文名 &nbsp; <br/>&nbsp; 设计人:yanhui 2003年11月 &nbsp; <br/>&nbsp; 修改人:叶文林 &nbsp; 2004.4.8 &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 原因:为提高程序的可读性作了少量的修改(如:增加注释、改变排版风格等)*/ &nbsp; <br/>&nbsp; /**********************************************************/ &nbsp; <br/>&nbsp; &nbsp; <br/>&nbsp; /***************以下程序将导出为EXCEL文档******************/ &nbsp; <br/>&nbsp; integer &nbsp; li_rtn,ii,li_asc &nbsp; <br/>&nbsp; string &nbsp; ls_name,ls_pathname &nbsp; <br/>&nbsp; boolean &nbsp; lb_exist &nbsp; <br/>&nbsp; if &nbsp; datawin.RowCount()&lt;1 &nbsp; then &nbsp; <br/>&nbsp; MessageBox("提示信息","请先检索数据再导出至Excel!") &nbsp; <br/>&nbsp; return &nbsp; -1 //error &nbsp; <br/>&nbsp; end &nbsp; if &nbsp; <br/>&nbsp; li_rtn=GetFileSaveName("保存文件",ls_pathname,ls_name,"xls","Excel文件(*.xls),*.xls") &nbsp; <br/>&nbsp; &nbsp; <br/>&nbsp; if &nbsp; li_rtn=1 &nbsp; then &nbsp; <br/>&nbsp; lb_exist &nbsp; = &nbsp; FileExists(ls_pathname) &nbsp; <br/>&nbsp; IF &nbsp; lb_exist &nbsp; THEN &nbsp; &nbsp; <br/>&nbsp; li_rtn &nbsp; = &nbsp; MessageBox("保存", &nbsp; ls_pathname+"已经存在,是否覆盖?",Exclamation!, &nbsp; YesNo!) &nbsp; <br/>&nbsp; end &nbsp; if &nbsp; <br/>&nbsp; if &nbsp; li_rtn=1 &nbsp; then &nbsp; <br/>&nbsp; //当文件存在用户选择覆盖,或是文件本就不存在时。注意变量li_rtn &nbsp; <br/>&nbsp; li_rtn=datawin.SaveAsAscii(ls_pathname) &nbsp; <br/>&nbsp; if &nbsp; li_rtn=1 &nbsp; then &nbsp; <br/>&nbsp; // MessageBox("提示信息","导出数据成功!") &nbsp; <br/>&nbsp; else &nbsp; <br/>&nbsp; MessageBox("错误信息","导出数据失败!") &nbsp; <br/>&nbsp; return &nbsp; -1 //error &nbsp; <br/>&nbsp; end &nbsp; if &nbsp; <br/>&nbsp; else &nbsp; <br/>&nbsp; return &nbsp; -1 //error &nbsp; <br/>&nbsp; end &nbsp; if &nbsp; <br/>&nbsp; else &nbsp; <br/>&nbsp; return &nbsp; -1 &nbsp; <br/>&nbsp; end &nbsp; if &nbsp; <br/>&nbsp; &nbsp; <br/>&nbsp; /**********以下程序将导出的EXCEL英文标题替换为汉字*********/ &nbsp; <br/>&nbsp; long &nbsp; numcols &nbsp; , &nbsp; numrows &nbsp; , &nbsp; c, &nbsp; r &nbsp; <br/>&nbsp; OLEObject &nbsp; xlapp &nbsp; , &nbsp; xlsub &nbsp; <br/>&nbsp; int &nbsp; ret &nbsp; <br/>&nbsp; numcols &nbsp; = &nbsp; long(datawin.Object.DataWindow.Column.Count) &nbsp; <br/>&nbsp; numrows &nbsp; = &nbsp; datawin.RowCount() &nbsp; <br/>&nbsp; &nbsp; <br/>&nbsp; // &nbsp; 产生oleobject的实例 &nbsp; <br/>&nbsp; xlApp &nbsp; = &nbsp; Create &nbsp; OLEObject &nbsp; <br/>&nbsp; &nbsp; <br/>&nbsp; //连接ole对象 &nbsp; <br/>&nbsp; ret &nbsp; = &nbsp; xlApp.ConnectToNewObject( &nbsp; "Excel.Sheet" &nbsp; ) &nbsp; <br/>&nbsp; if &nbsp; ret &nbsp; &lt; &nbsp; 0 &nbsp; &nbsp; then &nbsp; <br/>&nbsp; MessageBox("连接失败!","连接到EXCEL失败,请确认您的系统是否已经安装EXCEL!~r~n"&amp; &nbsp; <br/>&nbsp; +"错误代码:"+string(ret)) &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &nbsp; -1 &nbsp; <br/>&nbsp; end &nbsp; if &nbsp; <br/>&nbsp; // &nbsp; 打开EXCEL文件 &nbsp; <br/>&nbsp; xlApp.Application.Workbooks.Open(ls_pathname) &nbsp; &nbsp; <br/>&nbsp; ////使文件可见 &nbsp; <br/>&nbsp; //xlApp.Application.Visible &nbsp; = &nbsp; true &nbsp; <br/>&nbsp; &nbsp; <br/>&nbsp; // &nbsp; 得到活动工作表的引用,改善程序性能 &nbsp; <br/>&nbsp; xlsub &nbsp; = &nbsp; xlapp.Application.ActiveWorkbook.Worksheets &nbsp; <br/>&nbsp; string &nbsp; ls_colname,ls_text,ls_modistr,ls_col &nbsp; <br/>&nbsp; //取字段名更改为对应的文本text值 &nbsp; <br/>&nbsp; FOR &nbsp; c=1 &nbsp; to &nbsp; numcols &nbsp; <br/>&nbsp; ls_col="#"+string(c)+".name" &nbsp; <br/>&nbsp; ls_colname=datawin.describe(ls_col) &nbsp; <br/>&nbsp; ls_modistr=ls_colname+"_t.text" &nbsp; <br/>&nbsp; ls_text=datawin.describe(ls_modistr) &nbsp; <br/>&nbsp; xlsub.cells=ls_text &nbsp; <br/>&nbsp; NEXT &nbsp; <br/>&nbsp; &nbsp; <br/>&nbsp; xlApp.DisConnectObject() &nbsp; <br/>&nbsp; Destroy &nbsp; xlapp &nbsp; <br/>&nbsp; MessageBox("提示信息","导出数据成功!") &nbsp; <br/>&nbsp; return &nbsp; 1 //success&nbsp;&nbsp;

yeguozhi 发表于 2009-9-28 15:55:35

好的,谢谢分享!试试

OKCJ1910 发表于 2009-9-30 12:40:02

受用了,谢谢~~

suiyuan2008 发表于 2009-10-19 23:05:17

学习学习,谢谢

qianhuhui 发表于 2009-11-21 13:27:38

今天看了测试程序发现有问题。还需要改善!
页: [1]
查看完整版本: 一个简单的DW导出XLS的函数,不错的了

免责声明:
本站所发布的一切破解补丁、注册机和注册信息及软件的解密分析文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:Admin@SybaseBbs.com