개~발/Powerbuilder

csv, xls, 탭형식 파일 DW로 import 받기

민서정 2009. 3. 5. 15:46
 

파일을 Import 받을때 TXT(탭형식), CSV(쉼표), XLS(엑셀파일)형식을 아래의 하나의 함수로 받을 수 있습니다.

 

//***************************************************************************************//
//* 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

==============================================================================================
/* 사용예*/
/*import 할  파일의 컬럼수와 동일한 DW가 생성되어 있어야한다.*/

 s_result = GetFileOpenName ("Select File", s_FileName1, s_FileName2, "CSV", &
         + "CSV(쉼표로 분리), *.csv,Excel Files (*.xls), *.xls, ")

 IF s_result <> 1 THEN return 0
 dw_k.SetItem(1, "file_name", s_FileName1)
 
 if not FileExists(s_FileName1) then return 0
 
 dw_excel.Reset()
 
 f_excel_import(s_FileName1, dw_excel, REF ls_err)