读书人

帮小弟我看看excel导出的有关问题先

发布时间: 2012-02-12 17:16:34 作者: rapoo

帮我看看excel导出的问题,先谢谢
int li_rc,handle,excel_ok
string ls_path,ls_file
li_rc = GetFileSaveName ( "保存 ",ls_path, ls_file, "XLS ", "EXCEL文件(*.xls),*xls " , "C:\ ", 32770)
if li_rc <> 1 then
return
end if
if FileExists(ls_path) then
if MessageBox( "覆盖提示 ", "文件: " + ls_file + "已存在,要替换吗? ",Question!,YesNo!) = 2 then return
end if
handle = FileOpen(ls_path,StreamMode!, Write!, LockWrite!, Replace!)

if handle > = 0 then
//文件创建成功
Pointer oldpointer
setpointer(HourGlass!)
Oleobject ole_xls
ole_xls = create Oleobject
excel_ok = ole_xls.connecttonewobject( "Excel.Application ")
ole_xls.workbooks.open(ls_path,0,false)
ole_xls.Application.Run( "MacroName ")
//没有打开excel文件
if excel_ok <> 0 then
messagebox( "错误 ", "请确认安装了Microsoft Excel ",stopsign!)
return
end if
//往excel中写数据

//先把标题写上
ole_xls.activesheet.cells(1,1).value = dw_1.object.zt_code_t.text
ole_xls.activesheet.cells(1,2).value = dw_1.object.bargain_number_t.text
ole_xls.activesheet.cells(1,3).value = dw_1.object.Collect_plan_number_t.text
ole_xls.activesheet.cells(1,4).value = dw_1.object.material_number_t.text
ole_xls.activesheet.cells(1,5).value = dw_1.object.apply_amount_t.text
ole_xls.activesheet.cells(1,6).value = dw_1.object.Material_plan_unitprice_t.text
ole_xls.activesheet.cells(1,7).value = dw_1.object.Material_act_unitprice_t.text
ole_xls.activesheet.cells(1,8).value = dw_1.object.MC_plan_datetime_t.text
ole_xls.activesheet.cells(1,9).value = dw_1.object.MC_Material_datetime_t.text
ole_xls.activesheet.cells(1,10).value = dw_1.object.MC_plan_original_id_t.text
ole_xls.activesheet.cells(1,11).value = dw_1.object.State_t.text
ole_xls.activesheet.cells(1,12).value = dw_1.object.remark_t.text
for i = 1 to rows
for j = 1 to columns
choose case j
case 1,2,3,4,10,12
ole_xls.activesheet.cells(i+1,j).value = dw_1.getitemstring(i,j)
case 5,6,7,11
ole_xls.activesheet.cells(i+1,j).value = dw_1.getitemnumber(i,j)
case 8,9
ole_xls.activesheet.cells(i+1,j).value = dw_1.getitemdatetime(i,j)
end choose
next
next
ole_xls.application.quit()
ole_xls.disconnectobject()
destroy ole_xls
setpointer(oldpointer)
if FileClose(handle) <> 1 then
messagebox( " ", "文件关闭失败 ")
halt
end if

else
messagebox( " ", "创建文件失败 ")
return
end if
最后文件也创建成功了,可excel弹出一个窗口,问是否保存文件,如果选择“否”或“取消”,打开创建的文件,里边就什么都没有,如果选“是”,再保存一个文件,这个文件就有正确的数据,到底怎么回事啊?

[解决办法]
FileOpen去掉
ole_xls.application.quit()
的前边加上
ole_xls.application.ActiveWorkbook.SaveAs( 'c:\xxx.xls ')
------解决方案--------------------


在释放用户对象前保存一下,ole_xls.ActiveWorkbook.Save

读书人网 >PB

热点推荐