版权声明:本文为博主原创文章,转载请注明出处;
网上我也看到了很多的Powerdesigner 导出方法,因为Powerdesigner 提供了部分VBA功能,所以让我用代码导出Excel格式文件得以实现;
先看下效果图:
1.首先这个是PowerDesign待导出的文件
2.执行脚本后导出的Excel截图
3.后期规划导出效果图(因为支持了VBA,所以都是可以实现的):
一切以代码为主,处理思路是,先读取所有的Tables 循环遍历,得到单表对象,然后就可以拿到相关属性了,字段名,code,字段类型等,当然PowerDesign请按照官方格式填满哦
看代码哈
'****************************************************************************** '* 我的淘宝店: 52sunan.taobao.com '* 我的网站: www.52sunan.com '* Created: '* Version: 1.0 '****************************************************************************** Option Explicit Dim rowsNum rowsNum = 2 Dim Model Set Model = ActiveModel If (Model Is Nothing) Or (Not Model.IsKindOf(PdPDM.cls_Model)) Then Debug.print "null" Else ' Get the tables collection '创建EXCEL APP dim beginrow DIM EXCEL, SHEET set EXCEL = CREATEOBJECT("Excel.Application") EXCEL.workbooks.add '添加工作表 SET sheet = EXCEL.workbooks(1).sheets(1) sheet.name ="数据字典" sheet.Range(sheet.cells(1, 1),sheet.cells(1, 9)).Merge sheet.cells(1, 1) ="淘宝服装店地址:http://52sunan.taobao.com" sheet.Range(sheet.cells(1, 1),sheet.cells(1, 9)).Interior.Color=rgb(146,208,80) rowsNum=2 beginrow = rowsNum+1 Dim tab For Each tab In Model.tables TableLoop tab,SHEET Next EXCEL.visible = true '设置列宽和自动换行 sheet.Columns(1).ColumnWidth =10 sheet.Columns(2).ColumnWidth =15 sheet.Columns(4).ColumnWidth =20 sheet.Columns(5).ColumnWidth =15 sheet.Columns(6).ColumnWidth =15 sheet.Columns("C:C").EntireColumn.AutoFit sheet.Columns("i:i").EntireColumn.AutoFit End If Sub TableLoop(tab, sheet) If IsObject(tab) Then Dim rangFlag rowsNum = rowsNum + 1 sheet.cells(rowsNum, 1) = "表名" sheet.Range(sheet.cells(rowsNum, 2),sheet.cells(rowsNum, 9)).Merge sheet.cells(rowsNum, 2)=tab.code sheet.Range(sheet.cells(rowsNum, 1),sheet.cells(rowsNum, 9)).Borders.LineStyle = "1" sheet.Range(sheet.cells(rowsNum, 1),sheet.cells(rowsNum, 9)).Interior.Color=rgb(141,180,226) sheet.Range(sheet.cells(rowsNum, 1),sheet.cells(rowsNum, 9)).Borders.Weight ="3" rowsNum = rowsNum + 2 sheet.cells(rowsNum, 1) = "中文名" sheet.cells(rowsNum, 2) = "字段名" sheet.cells(rowsNum, 3) = "类型" sheet.cells(rowsNum, 4) = "长度" sheet.cells(rowsNum, 5) = "主键" sheet.cells(rowsNum, 6) = "索引" sheet.cells(rowsNum, 7) = "不可空" sheet.cells(rowsNum, 8) = "默认值" sheet.cells(rowsNum, 9) = "说明" sheet.Range(sheet.cells(rowsNum,1),sheet.cells(rowsNum,9)).Interior.Color=rgb(166,166,166) Dim col ' running column Dim colsNum colsNum = 0 for each col in tab.columns rowsNum = rowsNum + 1 colsNum = colsNum + 1 sheet.cells(rowsNum, 1) = col.name sheet.cells(rowsNum, 2) = col.code sheet.cells(rowsNum, 3) = col.datatype sheet.cells(rowsNum, 4) = IIF(col.Length<>0,col.Length,"") sheet.cells(rowsNum, 5) = IIF(col.Primary,"√","") sheet.cells(rowsNum, 6) = IIF(col.Primary,"√","") sheet.cells(rowsNum, 7) = IIF(col.Mandatory,"√","") sheet.cells(rowsNum, 8) = "无" sheet.cells(rowsNum, 9) = col.comment next '设置边框 DIM RanagBorder SET RanagBorder =sheet.Range(sheet.cells(rowsNum-colsNum,1),sheet.cells(rowsNum,9)) RanagBorder.Borders.LineStyle = "1" 'RaneBorderFun RanagBorder rowsNum = rowsNum + 1 End If End Sub function IIF(flg,tstr,fstr) if flg then IIF= tstr else IIF= fstr end if End function
http://www.52sunan.com 我平时搞的一个小网页
执行方法:Tools -> Execute COmmands -> Edit/Run Script 或者用快捷键也可以:ctr+shift+X
里面有一个小问题,我还没能获取到字段的默认值属性,用Default结果是关键字 使用不了,我正在研究中,向着最终文档方向发展,希望大家多多支持与交流~
这里的技术主要是VBA,只要可以拿到VBA的地方就可以很容易操作Excel了,而Excel作为存档文件的一个重要文件格式,平时偶尔会遇到整理数据字典,和其他公司联协等,所以构建一些良好的文档还是很必要的。
学好VBA,发现Excel 又变得继续强大强大了。。。
以上涉及到的资料请见附件:附件PowerDesignToExcelFile