如何将Excel文件导入DataWindow

时间:2013-07-23 06:53:22

标签: powerbuilder datawindow

我想将.xlsx文件导入PowerBuilder DataWindow,我知道我们可以使用CSV格式,但是用户想要使用xlsx格式,如果有任何导入选项,请告诉我。< / p>

4 个答案:

答案 0 :(得分:3)

你可以像这样导入xls(这是我们的导入功能)。当然,您必须知道从excel到datawindow的每个列值的位置。我认为你不需要这么复杂的解决方案,但主要的指导方针是:

int     li_rtn, li_i
string  ls_range
long    ll_excel_rows, ll_max_rows, ll_max_columns
long    ll_i, ll_j
string  lsa_column_names[], ls_mod
long    lla_column_pos[]
long    ll_row
int     li_rc
string  las_truncated_data[]
string ls_errorMessage

oleobject lole_excel, lole_workbook, lole_worksheet

TRY   
    lole_excel = create oleobject
    li_rtn = lole_excel.ConnectToNewObject("excel.application")
    if li_rtn <> 0 then
            ls_errorMessage = "Error running MS Excel api"
            li_rtn = -1
    else
        li_rtn = 1
        lole_excel.WorkBooks.Open(as_filepath) 

        lole_workbook = lole_excel.application.workbooks(1)

        int li_sheetnum
        IF IsNumber(as_sheet) THEN
            li_sheetnum = Integer(as_sheet)
        ELSE
            li_sheetnum = uof_pub_excel_get_sheet_byname(as_sheet, lole_workbook)
        END IF
        lole_worksheet = lole_workbook.worksheets(li_sheetnum)

        ll_max_rows     = lole_worksheet.UsedRange.Rows.Count
        ll_max_columns  = lole_worksheet.UsedRange.Columns.Count

        string lsa_tmp[]
        lsa_column_names = f_split(f_trim(as_imported_columns,";"), ";")
        FOR ll_i = 1 TO UpperBound(lsa_column_names)
            IF (pos(lsa_column_names[ll_i], ":")>0) THEN
                lsa_tmp = f_split(lsa_column_names[ll_i], ":")
                lla_column_pos[UpperBound(lla_column_pos)+1] = long(lsa_tmp[2])
                lsa_column_names[ll_i] = lsa_tmp[1]
            END IF
        NEXT

        string  ls_cellValue, ls_coltype, ls_value
        int         li_idx_col, li_statrRow
        boolean lb_copyData = false
        int         li_col_offset, li_vlen

        li_statrRow = ai_start_row

        IF (UpperBound(lla_column_pos)=0) THEN
            IF (UpperBound(lsa_column_names)<ll_max_columns) THEN ll_max_columns = UpperBound(lsa_column_names)
            FOR ll_j = li_statrRow TO ll_max_rows
                li_col_offset = 0
                ll_row = adw_dest.insertRow(0) // insert new row
                FOR ll_i = 1 TO (ll_max_columns)
                    ls_cellValue = String(lole_worksheet.cells(ll_j,ll_i).value)
                    IF (lsa_column_names[(ll_i)] = "") THEN 
                        li_col_offset++
                        continue
                    END IF
//                  li_rc = adw_dest.SetItem(ll_row, lsa_column_names[(ll_i)], lole_worksheet.cells(ll_j, (ll_i)).value)

                    ls_value = String(lole_worksheet.cells(ll_j, ll_i).value)
                    ls_coltype = adw_dest.describe(lsa_column_names[(ll_i)]+'.ColType')

                    // Checking length of string data
                    IF (pos(ls_coltype, "char")>0) THEN
                        li_vlen = integer(replace(left(ls_coltype, len(ls_coltype)-1), 1, 5, ""))
                        IF (li_vlen > 0 AND len(ls_value)>li_vlen) THEN
                            li_rtn = -2
                            ls_value = left(ls_value, li_vlen)
                            IF (f_array_indexof(las_truncated_data, lsa_column_names[ll_i])<1) THEN
                                las_truncated_data[UpperBound(las_truncated_data)+1] = lsa_column_names[ll_i]
                            END IF
                        END IF
                    END IF

                    li_rc = guo_common_utilities.uof_pub_set_dw_value(adw_dest, ll_row,  lsa_column_names[(ll_i)], ls_value,  ls_coltype)

                NEXT
            NEXT

        ELSE
            FOR ll_j = li_statrRow TO ll_max_rows
                ll_row = adw_dest.insertRow(0) // insert new row
                FOR ll_i = 1 TO UpperBound(lla_column_pos)
                    ls_cellValue = String(lole_worksheet.cells(ll_j,lla_column_pos[ll_i]).value)
                    adw_dest.SetItem(ll_row, lsa_column_names[ll_i], ls_cellValue)
                NEXT
            NEXT
        END IF
    end if

CATCH ( runtimeerror  lo_rte)
    li_rtn = -1
    ls_errorMessage = "MS Excel api runtime error"
FINALLY   
    // Quit
    IF (IsValid(lole_excel)) THEN 
        lole_excel.application.quit()
        lole_excel.DisconnectObject()
    END IF

    destroy lole_Excel
    destroy lole_workbook
    destroy lole_worksheet 
END TRY

uo_nv_response luo_return 
luo_return = create uo_nv_response 

luo_return.ii_errorCode = li_rtn
IF (UpperBound(las_truncated_data)>0) THEN
    luo_return.is_errorMessage = "Zeichenkette von "
    FOR li_i = 1 TO UpperBound(las_truncated_data)
        luo_return.is_errorMessage += las_truncated_data[li_i] + ", "
    NEXT
    luo_return.is_errorMessage = Left(luo_return.is_errorMessage, Len(luo_return.is_errorMessage)-2)
    luo_return.is_errorMessage += " wurde abgeschnitten"
ELSE
    luo_return.is_errorMessage = ls_errorMessage
END IF

return luo_return

以下是一些缺失的功能:

global type f_array_indexof from function_object end type forward prototypes global function integer f_array_indexof (any aa_array[], any aa_element) end prototypes global function integer f_array_indexof (any aa_array[], any aa_element);int li_count FOR li_count = 1 TO UpperBound(aa_array) IF (lower(aa_array[li_count]) = lower(aa_element)) THEN return li_count END IF NEXT return -1 end function

f_split:

来自function_object的全局类型f_split 结束类型

前进原型 全局函数boolean f_split(string as_str) 全局函数任何f_split(字符串as_str,字符串as_delimiter) 最终原型

全局函数任何f_split(字符串as_str,字符串as_delimiter); long ll_pos1,ll_pos2,i String lsa_split []

ll_pos1 = Pos(as_str,as_delimiter) ll_pos2 = 0 i = 1

如果(ll_pos1 = 0且Len(as_str)> 0)则lsa_split [1] = as_str

做ll_pos1&gt; 0     lsa_split [i] =''     if(i = 1)然后         lsa_split [i] = mid(as_str,1,ll_pos1 - 1)         ll_pos2 = ll_pos1     其他         ll_pos2 = Pos(as_str,as_delimiter,ll_pos1 + 1)         if(ll_pos2 = 0)然后//它结束了:)             lsa_split [i] = mid(as_str,ll_pos1 + 1,Len(as_str) - ll_pos1)         其他             lsa_split [i] = mid(as_str,ll_pos1 + 1,ll_pos2 - ll_pos1 - 1)         万一     万一     ll_pos1 = ll_pos2
    i = i + 1 环 // lsa_split [i] =右(as_str,Len(as_str) - ll_pos1)

返回lsa_split 结束功能


f_trim:

来自function_object的全局类型f_trim 结束类型

前进原型 全局函数字符串f_trim(字符串as_string,字符ac_remove) 最终原型

全局函数字符串f_trim(字符串as_string,字符ac_remove); if(Isnull(as_string)或Isnull(ac_remove))然后返回as_string

while while(left(as_string,1)= ac_remove)     as_string = right(as_string,Len(as_string)-1) 环

while while(right(as_string,1)= ac_remove)     as_string = left(as_string,Len(as_string)-1) 环

返回as_string 结束功能


public function integer uof_pub_excel_get_sheet_byname(string as_sheetname,oleobject aole_workbook); int li_sheets_count,li_i

li_sheets_count = aole_workbook.worksheets.count FOR li_i = 1 TO li_sheets_count     IF(aole_workbook.worksheets(li_i).name = as_sheetname)然后返回li_i NEXT

返回0 结束功能

答案 1 :(得分:2)

如果正确定义了电子表格(标题行包含列名,行2-n包含数据),则可以定义与其的ODBC连接,并将其视为数据库。

您直接在电子表格上绘制数据窗口 - 完全没有编码! (当然,除了设置SQLCA和调用dw.Retrieve()之外)

答案 2 :(得分:0)

我会用OLE打开工作簿,将其保存为带有dbf扩展名的格式8(DBase3),然后将其导入DataWindow。

答案 3 :(得分:0)

使用OLE将其保存到文本文件,然后将其导入。下面的代码执行此操作

li_return = GetFileOpenName('Select a spreadsheet to upload - XLS or XLSX formats only', ls_path, ls_file, 'xl*', "All Excel Files (*.xl*),*.xl*", "%userprofile%\desktop")

//Save the file as Text using OLE and then import
MyExcelFile = Create OLEObject
If MyExcelFile.ConnectToObject(ls_path) < 0 then
    MessageBox('Import Error', 'Unable to load spreadsheet. Please check if the file is open')
    Return
End If
ls_txt_path = Mid(ls_path, 1, Pos(ls_path, '.xl') - 1) + '.txt'
FileDelete(ls_txt_path)
MyExcelFile.Application.WorkBooks(1).SaveAs(ls_txt_path, 21);
MyExcelFile.Application.DisplayAlerts = False
MyExcelFile.Application.WorkBooks(1).Close();
Destroy MyExcelFile;

现在将名称为ls_txt_path的文件导入数据窗口