纵有疾风起
人生不言弃

Excel导入PDM表

场景:

很多时候我们直接在pdm中设计一张表的时候,不如excel操作方便,所以就需要通过excel画table然后倒入pdm中变成table,废话不多

执行

pdm运行脚本(Ctrl+Shift+x),文件–>打开脚本–>RUN

代码:

excel2table.vbs

Option Explicit Dim mdl ' the current modelSet mdl = ActiveModelIf (mdl Is Nothing) Then   MsgBox "There is no Active Model"End If Dim HaveExcelDim RQRQ = vbYes 'MsgBox("Is Excel Installed on your machine ?", vbYesNo + vbInformation, "Confirmation")If RQ = vbYes Then   HaveExcel = True   ' Open & Create Excel Document   Dim x1  '   Set x1 = CreateObject("Excel.Application")   Dim currentpath '   currentPath = createobject("Scripting.FileSystemObject").GetFolder(".").Path   x1.Workbooks.Open currentPath&"\excel2pdm.xlsx"   '指定excel文档路径   x1.Workbooks(1).Worksheets("Sheet1").Activate   '指定要打开的sheet名称Else   HaveExcel = FalseEnd If a x1, mdlsub a(x1, mdl)dim rwIndex   dim tableNamedim colnamedim tabledim colon error Resume Nextset table = mdl.Tables.CreateNew     '创建表table.Name ="MyTableName" '指定表名,第一列的值table.Code = "MyTableCode" table.Comment ="TableComment" '指定表注释,第二列的值For rwIndex = 2 To 1000   '指定要遍历的Excel行标  由于第1行是表头,从第2行开始        With x1.Workbooks(1).Worksheets("Sheet1")            If .Cells(rwIndex, 1).Value = "" Then '如果遍历到第一列为空,则退出               Exit For            End If             set col = table.Columns.CreateNew   '创建一列/字段              col.Name = .Cells(rwIndex, 1).Value   '指定列名             col.Code = .Cells(rwIndex, 2).Value   '指定列名                            col.DataType = .Cells(rwIndex, 3).Value '指定列数据类型                   col.Comment = .Cells(rwIndex, 4).Value  '指定列说明                   If .Cells(rwIndex, 5).Value <>"" Then                col.DefaultValue=.Cells(rwIndex,5).Value '默认值             End If             col.Precision = .Cells(rwIndex, 6).Value  '精度                   If .Cells(rwIndex,7).Value="1" Then                        col.Primary  = true  '是否主键            End If              End WithNextMsgBox "生成数据表结构共计 1 " , vbOK + vbInformation, "表" Exit SubEnd sub

excel(注意文件名)放在和脚本同一个目录下的

Excel导入PDM表插图
excel2pdm.xlsx

脚本下载地址:
链接: https://pan.baidu.com/s/121Uw1fMz57w1EWIT0MFzqg 提取码: ys1e
如链接不可用,请私信我,并且附上您的邮箱。
网络上类似代码比较多,我针对修改了一个位置,增加了一些属性的读取,如有侵权,请联系我

文章转载于:https://www.jianshu.com/p/6ee499506d3f

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » Excel导入PDM表
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录