- 参数初始化 * : InitParm
- 确定excel的title 定位数据* : MatchTitleIndex
- 读取Excel : ReadExcelData
- 校验数据库数据 : ValidateInDB
- 表内数值型格式校验 ValidateintegerFormat
- 修改数据库 UpdateDB
/*--------------主程序-------------*/
/*1、参数初始化*/
Do 'InitParm'
/*2、读取Excel数据,校验表头格式,定位数据*/
Do 'MatchTitleIndex'
&CellRow += 1
do while &ExcelDocument.Cells(&CellRow, &Variable_Index).Text <> '' //这里是读一条就调一次数据库,需要优化
&Flag = 0
Do 'ReadExcelData'
/*3、校验Excel表内数据*/
/*3.1、数据库数据校验*/
Do 'ValidateInDB'
/*3.2、表内数值型数据格式校验*/
Do 'ValidateintegerFormat'
/*4、结束数据校验,如果正确,修改数据库,如果不正确,则提示有错误*/
if &Flag = 1
&ErrorOutMessage = "部分数据有误,详见数据录入错误日志"
else
Do 'UpdateDB'
endif
&CellRow += 1
enddo
&ExcelDocument.Close()/*导入修改完毕,关闭Excel*/
/*--------------Sub-------------*/
Sub 'InitParm'
&ExcelDocument.Open(&FilePathName)/*打开上传的Excel文件*/
&CellRow = 1
&ErrorOutMessage = ""
// &Pattern = "^[0-9]*$"/*数值型正则表达式*/
&Variabel1_index= 0
&Variabel2_Index = 0
&Variable3_Index = 0
&Variable4_Index = 0
&Variable5_Index = 0
EndSub
Sub 'MatchTitleIndex'
If (&ExcelDocument.ErrCode <> 0)
&ErrorMessage = &ExcelDocument.ErrDescription
&ExcelDocument.Close()
Return
Endif
for &iCellIndex = 1 to 100
&Title = &ExcelDocument.Cells(&CellRow, &iCellIndex).Text
if trim(&Title) = "title1"
&Variable1_index= &iCellIndex
endif
if trim(&Title) = "title2"
&Variable2_Index = &iCellIndex
endif
if trim(&Title) = "title3"
&Variable3_Index = &iCellIndex
endif
if trim(&Title) = "title4"
&Variabel4_Index = &iCellIndex
endif
if trim(&Title) = "title5"
&Variable5_Index = &iCellIndex
endif
endfor
&Index = &Variable1_Index* (&Variable2_index+ &Variable3_index+ &Variable4_index+ &Variable5_index) //&Variable1_index是不能缺少的主键
if &Index = 0
&ErrorMessage = '没有找到应对的列'
return
endif
EndSub
Sub 'ReadExcelData'
&Variabel1= &ExcelDocument.Cells(&CellRow,&Variable1_Index).Text
&Variable2= &ExcelDocument.Cells(&CellRow,&Variable2_Index).Text
&Variable3= &ExcelDocument.Cells(&CellRow,&Variable3_Index).Text
&Variable4= &ExcelDocument.Cells(&CellRow,&Variable4_Index).Text
&Variable5 = &ExcelDocument.Cells(&CellRow,&Variable5_Index).Number
EndSub
Sub 'ValidateInDB'
EndSub
Sub 'ValidateintegerFormat'
if &Variable1.IsEmpty() or &Variable=''
&DataErrorReason = '参数名称不能为空'
P_InsertErrorInfo.Call(&UserCode,ErrorType.ImportError,&OriginalFileName,
'第'+&CellRow.ToString()+'行'+','+'第'+&MakProductCode_Index.ToString()+'列',&DataErrorReason)
&Flag = 1
endif
EndSub
Sub 'UpdateDB'
For each
where Attribute1= &Variable1 //这两行可以不用也行
&Variable2= Attriable2 //不同的逻辑看自己的
Do 'New'
EndFor
&ErrorMessage = '导入完成'
commit
EndSub
Sub 'New'
for each //根据颜色名称获取颜色编码
where A1= &V1
&V2= A2
exit
endfor
New
A1= &V1
A2= &V2
A3= &V3
EndNew
EndSub
genexus excel导入