Excel導出問題

aichangfeng 2007-08-29 10:56:06
String ls_filename,ls_path,ls_path_new

GetFolder('Select path:',ls_path)
IF ls_path = '' OR ISNULL(ls_path) THEN
MESSAGEBOX("ERROR","PATH IS NULL!")
RETURN 1
END IF

SetPointer(HourGlass!)
//ls_nature是從datawindow動態取值
//filename
ls_filename = ls_nature + '.XLS'

OleObject Loo_Excel
Loo_Excel=Create oleobject
Loo_Excel.ConnectToNewObject("Excel.Application")
Loo_Excel.WorkBooks.Add()

Loo_Excel.Range("A1").Select
Loo_Excel.Selection.Font.ColorIndex = 2
Loo_Excel.Cells[1,2].value= 'Status'
//Choose dir
IF Right(ls_path,1) = '\' then//curr DIR
ls_path_new = ls_path + ls_filename
Else
ls_path_new = ls_path + '\' + ls_filename
End if
//Save as
IF FileExists ( ls_path_new ) THEN
FileDelete ( ls_path_new )
End IF
Loo_Excel.activeworkbook.saveas(ls_path_new)
Loo_Excel.ActiveWorkbook.saved = true
Loo_Excel.activeworkbook.close()
Loo_Excel.application.quit()
Loo_Excel.DisconnectObject()
Destroy Loo_Excel
SetPointer(Arrow!)

Messagebox("OK...","Export to Excel Successful!!")

问题一:
执行结束后,excel进程仍然在,并且执行一遍,多一个进程,只有退出pb后,excel进程才消失。
问题二:
文件名相同,並已打開,再導出時會有錯誤?
...全文
402 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
aichangfeng 2007-09-18
  • 打赏
  • 举报
回复
沒人知道嗎
superdinosaur520 2007-09-11
  • 打赏
  • 举报
回复
帮LZ顶!!!!!!!!!!!!!!!!!!!!!!!!!
aichangfeng 2007-09-10
  • 打赏
  • 举报
回复
人呢
aichangfeng 2007-09-03
  • 打赏
  • 举报
回复
其實上面是兩個進程,一個是打開excel產生一個模板,另一個是另存excel文件,第一個還是沒有關掉,我提供的代碼你試一下
aichangfeng 2007-08-31
  • 打赏
  • 举报
回复
再次謝謝WOFEIYANG(飞扬)
判斷相同文件名是否已經打開這個問題解決了
還有一點就是導出完成后怎麼去掉進程里的excel
WOFEIYANG 2007-08-31
  • 打赏
  • 举报
回复
If ll_handle >0 Then SendMessage(ll_handle,WM_CLOSE,0,0) //关闭
是我从我上面回答中复制过来的,FindWindowA和SendMessage函数的申名,请看我上面的回复
WOFEIYANG 2007-08-31
  • 打赏
  • 举报
回复
比如说你将要导出的EXCEL文件名为A.XLS.
那么在EXCEL中的标题为Microsoft Excel - A.xls,如果系统将扩展设置为隐藏的则EXCEL标题会成为Microsoft Excel - A,因此在你导出之前先用FindWindowA(0,"title")去判断是否已经打开就行了.

string ls_title
long ll_handle
ls_title = "Microsoft Excel - A.xls"

ll_handle = FindWindowA(0,ls_title)
If ll_handle <=0 Then
ls_title = "Microsoft Excel - A"
ll_handle = FindWindowA(0,ls_title)
End IF
If ll_handle >0 Then
MessageBox('提示','该EXCEL文件已经被打开!')
If ll_handle >0 Then SendMessage(ll_handle,WM_CLOSE,0,0) //关闭
End If

以上代码不管你的EXCEL是用什么方式打开的,都可以被找到,且被关闭.
aichangfeng 2007-08-31
  • 打赏
  • 举报
回复
謝謝WOFEIYANG(飞扬)
我說的打開的excel文件不是用PB打開的,而是自己手動打開,要導出時怎麼去判斷要生成的相同的文件名excel是否處天打天狀態
其實退出窗口時excel在進程里就已經沒有了,我是想做到導出完畢就讓進程里沒有excel
WOFEIYANG 2007-08-31
  • 打赏
  • 举报
回复
//以下代码像在任务管理器中结束EXCEL进程一样

//创建非可视对象uf_get_exename

//声明local external function(api)
Function Long CreateToolhelp32Snapshot(Long Flags,Long ProcessId) Library "kernel32.dll"
Function Integer Process32First(uLong Snapshot,ref s_Process Process) Library "kernel32.dll"
Function Integer Process32Next(uLong Snapshot,ref s_Process Process) Library "kernel32.dll"

//声明前先创建结构
unsignedlong structsize
unsignedlong usage
unsignedlong processid
unsignedlong defaultheapid
unsignedlong moduleid
unsignedlong threads
unsignedlong parentprocessid
unsignedlong classbase
unsignedlong flags
character filename[260]
//构造对象uf_get_exename的函数of_getexe()

///////////////////////////of_getexe()////////////////////////
功能:枚举进程并返回指定进程号PID
传入:无
返回:Long
/////////////////////////////////////////////////////////////

s_Process lst_Process
string ls_filename[100] ,ls_curexename
ulong ln_ProcessID,ln_SameCount,ln_Snapshot,ln_Circle,ln_Count,ul_PID

ul_PID = 0
ln_Snapshot = CreateToolhelp32Snapshot(2,0)
if (ln_Snapshot<1) then return 0
lst_Process.StructSize = 296

if Process32First(ln_Snapshot,lst_Process)=0 then return 0

//枚举当前权限下的进程
debugbreak()
do while true

if Process32Next(ln_Snapshot,lst_Process)=0 then exit
ln_Count = ln_Count + 1
ls_FileName[ln_Count] = lst_Process.FileName
If Lower(ls_FileName[ln_Count]) = 'EXCEL.exe' Then
//取得进程号
ul_PID = lst_Process.ProcessID
//messagebox(string(ul_PID),ls_FileName[ln_Count])
End If

loop

return ul_PID

//接下来
//创建一个窗体,上面放一按钮cb_click

//在窗体声明local external function(api)
FUNCTION ulong TerminateProcess(ulong hProcess,ulong uExitCode) LIBRARY "kernel32.dll"
FUNCTION ulong OpenProcess(ulong dwDesiredAccess,ulong bInheritHandle,ulong dwProcessId) LIBRARY "kernel32.dll"

//cb_click的clicked事件

uf_get_exename luf_get_exename


INTEGER li_rc
ULONG ul_PID
ULONG PROCESS_TERMINATE = 0001
ULONG hwdprocess

If messagebox('结束进程','确定吗?Kill?',question!,yesno!,1) = 2 Then return

//创建实例变量
luf_get_exename = create uf_get_exename
//获取指定进程号
ul_PID = luf_get_exename.of_getexe()

If ul_PID = 0 Then
Messagebox('结束进程','没有发现EXCEL进程!')
return
End If

If ul_PID <> 0 Then
//获取指定进程号的进程句柄
hwdprocess = OpenProcess(PROCESS_TERMINATE,1,ul_PID)
//messagebox('',string(hwdprocess))
//结束进程,成功返回非零
li_rc = TerminateProcess(hwdprocess,0)
If li_rc <> 0 Then Messagebox('结束进程','成功结束进程!')
End If


Destroy luf_get_exename;
WOFEIYANG 2007-08-31
  • 打赏
  • 举报
回复
要关闭所有被打开的EXCEL吗?
aichangfeng 2007-08-30
  • 打赏
  • 举报
回复
怎麼沒人頂啊?
WOFEIYANG 2007-08-30
  • 打赏
  • 举报
回复
通过SendMessage(ll_handle,WM_CLOSE,0,0) 函数,可以解决以上两个问题.

一\在每打开一个EXCEL文件时,记录标题名称所对应的ll_handle放入数组中

二\每打开一个文件时,先用ll_handle = FINDWINDOWA()查找正要打开的文件,有没已经被打开了,若已经被打开.则通过SendMessage(ll_handle,WM_CLOSE,0,0)关闭.

三\在退出窗口时,循环handle数组,使用SendMessage(ll_handle,WM_CLOSE,0,0)进行关闭
WOFEIYANG 2007-08-30
  • 打赏
  • 举报
回复
//关闭标题所对应的窗口

FUNCTION LONG FindWindowA (ulong Winhandle, string wintitle) LIBRARY "user32"

FUNCTION long SendMessage (long hwnd ,long wMsg , long wParam,long lParam) Library "user32.dll" Alias for "SendMessageA"

constant integer WM_CLOSE = 16

ls_title_text = "Microsoft Excel - a.xls"

ls_title_text = Trim(ls_title_text)

//以下代码是查找某EXCEL文件是否已被打开,用FINDWINDOWA()函数时要考虑,你的系统是否设置了隐藏护展名,如"Microsoft Excel - a.xls" 当你的系统设置为隐藏护展名时,A.XLS文档标题会变为"Microsoft Excel - a"

long ll_handle,ll_pos
string ls_old_text
ls_old_text = ls_title_text
ll_handle = FindWindowA(0,ls_title_text) //先查找全名
if ll_handle <=0 THen
ll_pos = Pos(ls_title_text,'.')
if ll_pos >0 Then
ls_title_text = Replace(ls_title_text,ll_pos,4,'')
ll_handle = FindWindowA(0,ls_title_text) //查找隐藏了扩展名
End If
End If

If ll_handle >0 Then SendMessage(ll_handle,WM_CLOSE,0,0) //关闭


609

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder DataWindow
社区管理员
  • DataWindow社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧