原文地址:http://blog.chinaunix.net/uid-20586802-id-3235549.html
/*****************簡單的導入功能,涉及到數據類型判斷*****************/
string ls_path,ls_name,ls_filter
integer li_ret
long ll_rows,ll_columns
string a[1000,100] //導入數據行數最大1000行,列數最大100列
ls_filter = "Excel文件(*.xls),*.xls"
li_ret = GetFileOpenName("請選擇后綴名為XLS的文件",ls_path,ls_name,"xls",ls_filter)
if li_ret <> 1 then return
//建立OLE對象
OLEObject ExcelServer
ExcelServer = create OLEObject
integer li_excel
li_excel = ExcelServer.ConnectToNewObject("excel.application")
if li_excel < 0 then
MessageBox("錯誤提示","連接excel失敗,檢查你的系統是否安裝了office!")
return
end if
ExcelServer.Workbooks.Open(ls_path,0,False)
ExcelServer.Application.DisplayAlerts = False
ll_rows = ExcelServer.ActiveSheet.UsedRange.Rows.Count //取得總行數
ll_columns= ExcelServer.ActiveSheet.UsedRange.columns.Count //取得總行數
dw_grid.reset()
if ll_rows = 0 then
messagebox("提示!","沒有Excel數據可導,請檢查Excel表格后重來!")
ExcelServer.Workbooks.close()
ExcelServer.DisConnectObject()
Destroy ExcelServer
return
end if
//導入的excel文件行數是否超過1000 行
if ll_rows >1000 then
messagebox("錯誤提示!","導入的excel文件列數過大,無法導入!")
ExcelServer.Workbooks.close()
ExcelServer.DisConnectObject()
Destroy ExcelServer
return
end if
//導入的excel文件列數與所對應的數據窗口列數不一致,則提示
integer li_columncount //得到數據窗口的總列數,總共14列
li_columncount = integer(dw_grid.describe("datawindow.column.count"))
if ll_columns <> li_columncount - 2 then
messagebox("提示錯誤!","導入的excel文件列數與所對應的數據窗口類數不一致!")
ExcelServer.Workbooks.close()
ExcelServer.DisConnectObject()
Destroy ExcelServer
return
end if
//從excel文件導入到數據窗口,沒有空行,字段,計算列字段都為字符串型
//long m,n
//for m = 1 to ll_rows
// dw_grid.insertrow(0)
// for n = 1 to ll_columns
// a[m,n] = string(ExcelServer.ActiveSheet.Cells[m,n].value)
// dw_grid.Setitem(m,n,a[m,n])
// next
//next
//ll_null為空行數
long m,amount,ll_null
amount = 0
for m = 1 to ll_rows
a[m,1] = String(ExcelServer.ActiveSheet.Cells[m,1].value)
if len(a[m,1]) <> 0 then
amount = amount+1
end if
next
ll_null = ll_rows - amount
long i,li_count
string ls_text
for i=(ll_null+1+1) to ll_rows //+1+1即表中列名稱算一行,從+1行讀起
li_count = dw_grid.insertrow(0)
dw_grid.setrow(li_count)
//簡稱
dw_grid.object.clientcode_1[li_count] = ExcelServer.ActiveSheet.Cells[i,2].text
//客戶編碼
dw_grid.object.clientcode[li_count] = ExcelServer.ActiveSheet.Cells[i,1].text
//數期
ls_text = ExcelServer.ActiveSheet.Cells[i,3].text
if not isnumber( ls_text ) and ls_text <> '' then
messagebox("錯誤","第["+ string(i) + ',3]單元格必須是數字!')
return
else
dw_grid.object.yyprd_bas_client_sq[li_count] = Long(ls_text)
end if
//信用額
if not isnumber(ExcelServer.ActiveSheet.Cells[i,4].text) and ExcelServer.ActiveSheet.Cells[i,4].text <> '' then
messagebox("錯誤","第["+ string(i) + ',4]單元格必須是數字!')
return
else
dw_grid.object.yyprd_bas_client_creditamount[li_count] = Dec(ExcelServer.ActiveSheet.Cells[i,4].text)
end if
//所屬月份
if not isdate(ExcelServer.ActiveSheet.Cells[i,5].text) and ExcelServer.ActiveSheet.Cells[i,5].text <> '' then
messagebox("錯誤","第["+ string(i) + ',5]單元格必須是日期!')
return
else
dw_grid.object.months[li_count] = datetime(ExcelServer.ActiveSheet.Cells[i,5].text)
end if
//貨幣
dw_grid.object.currency[li_count] = String(ExcelServer.ActiveSheet.Cells[i,6].text)
//匯率
if not isnumber(ExcelServer.ActiveSheet.Cells[i,7].text) and ExcelServer.ActiveSheet.Cells[i,7].text <> '' then
messagebox("錯誤","第["+ string(i) + ',7]單元格必須是數字!')
return
else
dw_grid.object.currencyrate[li_count] = dec(ExcelServer.ActiveSheet.Cells[i,7].text)
end if
//銷售金額
if not isnumber(ExcelServer.ActiveSheet.Cells[i,8].text) and ExcelServer.ActiveSheet.Cells[i,8].text <> '' then
messagebox("錯誤","第["+ string(i) + ',8]單元格必須是數字!')
return
else
dw_grid.object.salesamount[li_count] = Dec(ExcelServer.ActiveSheet.Cells[i,8].text)
end if
//備注
dw_grid.object.yyprd_cdc_sales_remarks[li_count] = String(ExcelServer.ActiveSheet.Cells[i,9].text)
//建立日期
ls_text = ExcelServer.ActiveSheet.Cells[i,10].text
if isnumber(ls_text) and ls_text <> '' then
messagebox("錯誤","第["+ string(i) + ',10]單元格必須是日期!')
return
else
dw_grid.object.created_date[li_count] = datetime(ExcelServer.ActiveSheet.Cells[i,10].text)
end if
next
messagebox("提示","導入數據成功,請點擊保存按鈕提交到數據庫!")
ExcelServer.Workbooks.close()
ExcelServer.DisConnectObject()
Destroy ExcelServer