개발업무를 하는 직장인이라면 프로그램에서 엑셀파일을 다루는 것은 필수과제가 아닐까 생각됩니다.
엑셀의 자료를 프로그램에 업로드하고 다운로드하는 방법은 꼭 알아야하죠
아래는 인터넷 서칭을 통해 구할 수 있는 엑셀 파일 업로드 함수의 소스 입니다.
엑셀 파일을 지정하면 그 파일을 데이터윈도우에 넣어주는 소스이죠
일단 엑셀파일을 데이터윈도우에 들어가기 쉽게 텍스트 파일로 변환한 후 import 하게 됩니다.
그런데 문제가 있습니다.
엑셀 파일 A를 지정했는데
프로그램을 실핼 전에 엑셀 창 B가 띄워져 있었다면 이 소스는 기존 엑셀창 B의 데이터를 업로드 하려고 한다는 것입니다.
심지어 기존 엑셀창 B를 닫아 버립니다.
왜 문제가 심각하냐면 기존 엑셀창에 열심히 데이터를 입력해 놨는데 엑셀창을 닫아 버리면 심호흡이 급격히 상승하면서 분노게이지가 올라가게 하기 때문이죠
문제 해결 방법은 간단합니다. 모르면 어렵고요
li_connect = ole_excel.ConnectToObject("","excel.application")
connecttoobject를 connecttonewobject로 교정하는 것입니다.
connecttoobject는 엑셀이 이미 띄워져 있다면 기존 엑셀을 재사용하는 것이고
connecttonewobject는 새로운 엑셀창을 만드는 것입니다.
//***************************************************************************************//
//* Import file type : TXT(탭형식), CSV(쉼표), XLS(엑셀파일)
//* Function : f_excel_import *//
//* 용 도 : 선택한 파일을 DataWindow에 ImportFile 하기 *//
//* Argument : as_path (처리할 Excel File 경로, 파일명 포함) *//
//* adw (IMPORT할 DataWindow) *//
//* as_error ( Reference, Error Msg) *//
//* ReTurn값 : Long( 1 : Success, 0에서 -9 : ImportFile Fail, -10 : FileOpen Fail, *//
//* -11 : FileDelete Fail ) *//
//* 사 용 예 : f_excel_import('C:\TEMP\TEMP.XLS', dw_1, REF ls_err) *//
//***************************************************************************************//
oleobject ole_excel
Boolean lb_select, lb_delete
Integer li_connect, li_open
Long ll_xls, ll_row, ll_import
String ls_open_file, ls_save_file , ls_msg
SetNull(as_error)
ls_open_file = trim(as_path)
IF Len(ls_open_file) = 0 THEN
as_error = "엑셀 파일의 경로를 입력하세요."
RETURN -10
END IF
IF Not FileExists(ls_open_file) Then
as_error = "지정한 파일이 존재하지 않습니다."
RETURN -10
END IF
if pos(ls_open_file,'CSV') + pos(ls_open_file,'csv') > 0 then
ll_xls = pos(ls_open_file,'CSV') //쉼표분리파일
if IsNull(ll_xls) or ll_xls = 0 then ll_xls = pos(ls_open_file,'csv')
else
ll_xls = pos(ls_open_file,'xls') //엑셀파일
if IsNull(ll_xls) or ll_xls = 0 then ll_xls = pos(ls_open_file,'XLS')
end if
if IsNull(ll_xls) or ll_xls = 0 then //* Excel File이 아니면 Text File인지 체크
ll_xls = pos(ls_open_file,'txt') // txt 탭 분리 파일
if IsNull(ll_xls) or ll_xls = 0 then ll_xls = pos(ls_open_file,'TXT')
If Not IsNull(ll_xls) or ll_xls > 0 then
ls_save_file = ls_open_file
goto excel_import
END IF
as_error = "Excel 파일이 아닙니다."
Return -10
end if
ole_excel = CREATE OLEobject
li_connect = ole_excel.ConnectToObject("","excel.application")
IF li_connect = -5 THEN
// -5 Can't connect to the currently active object
li_connect = ole_excel.ConnectToNewObject("excel.application")
END IF
IF li_connect <> 0 THEN
SetPointer(Arrow!)
CHOOSE CASE li_connect
CASE -1
ls_msg = "Invalid Call: the argument is the Object property of a control~r~n"
CASE -2
ls_msg = "Class name not found~r~n"
CASE -3
ls_msg = "Object could not be created~r~n"
CASE -4
ls_msg = "ould not connect to object~r~n"
CASE -9
ls_msg = "Other error~r~n"
CASE -15
ls_msg = "MTS is not loaded on this computer~r~n"
CASE -16
ls_msg = "Invalid Call: this function not applicable~r~n"
CASE ELSE
ls_msg = "If any argument's value is NULL, ConnectToNewObject returns NULL.~r~n"
END CHOOSE
DESTROY ole_excel
as_error = '엑셀 프로그램을 실행할 수 없습니다. ~r~n'+ls_msg
RETURN -10
END IF
SetPointer(HourGlass!)
ole_excel.WorkBooks.Open(ls_open_file)
ole_excel.Application.Visible = FALSE
lb_select = ole_excel.WorkSheets(1).Activate
ls_save_file = mid(ls_open_file, 1, ll_xls -2) + string(now(),'hhmmss') + ".txt"
ole_excel.Application.Workbooks(1).Saveas(ls_save_file, -4158)
ole_excel.WorkBooks(1).Saved = TRUE
ole_excel.WorkBooks.Close() //파일삭제
ole_excel.Application.Quit
ole_excel.DisConnectObject()
DESTROY ole_excel
excel_import:
ll_import = adw.importfile(ls_save_file) // 1번라인부터 입력
//ll_import = adw.importfile(ls_save_file,2)
SetPointer(Arrow!)
IF ll_import < 1 Then
MessageBox("ERROR", "파일 처리에 실패 하였습니다.(" + ls_save_file + ")", StopSign!)
Return ll_import
END IF
adw.accepttext()
IF NOT FileDelete(ls_save_file) THEN
MessageBox("ERROR", "파일 삭제에 실패 하였습니다.(" + ls_save_file + ")", StopSign!)
Return -11
END IF
Return 1
다음엔 요즘에 많이 프로그래밍 되고 있는 스크랩핑에 대한 팁을 기술하겠습니다.
'직장인 자기계발 > IT 코칭' 카테고리의 다른 글
갤럭시 안드로이드폰 밀어서 잠금 해제 없애기 방법 (0) | 2023.01.16 |
---|---|
파이썬 웹스크랩핑 오류 해결 방법 ModuleNotFoundError : No Module named (0) | 2022.11.23 |
로그인시 보이는 이름 바꾸기 와 사용자 계정 변경 (0) | 2022.09.08 |
PC 이름 바꾸기 와 장치 이름 바꾸기 (0) | 2022.09.08 |
mariadb database c0038: SQLSTATE = S1000 count does not exists check the function name 메세지 나올 때 조치 방법 (0) | 2022.08.16 |