php动态生成Mysql数据库字典,并一键导出excel代码
2017-02-11 23:16
686 查看
背景:
公司系统里有二三十张表,老板叫我整理一份数据字典,工作量真让人蛋疼.网络上找了一些工具导出数据字典,但是格式不是很理想,于是就”偷懒”地写了一个导出数据字典的代码,刚好也可以用来生成毕业设计的数据字典.
代码如下.(复制即可使用,导出excel的js文件,想要的话,自行百度)
效果图:
![](https://img-blog.csdn.net/20170211231313979?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1YW5nY2hlbmd3YW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
公司系统里有二三十张表,老板叫我整理一份数据字典,工作量真让人蛋疼.网络上找了一些工具导出数据字典,但是格式不是很理想,于是就”偷懒”地写了一个导出数据字典的代码,刚好也可以用来生成毕业设计的数据字典.
代码如下.(复制即可使用,导出excel的js文件,想要的话,自行百度)
效果图:
<?php /** * 生成mysql数据字典 */ header("Content-type: text/html; charset=utf-8"); error_reporting(E_ALL && ~E_NOTICE); //配置数据库 $dbserver = ""; $dbusername = ""; $dbpassword = ""; $database = ""; //其他配置 $mysql_conn = @mysql_connect("$dbserver", "$dbusername", "$dbpassword") or die("Mysql connect is error."); mysql_select_db($database, $mysql_conn); mysql_query('SET NAMES utf8', $mysql_conn); $table_result = mysql_query('show tables', $mysql_conn); $no_show_table = array(); //不需要显示的表 $no_show_field = array(); //不需要显示的字段 //取得所有的表名 while ($row = mysql_fetch_array($table_result)) { if (!in_array($row[0], $no_show_table)) { $tables[]['TABLE_NAME'] = $row[0]; } } //替换所以表的表前缀 /* if($_GET['prefix']){ $prefix = 'czzj'; foreach($tables as $key => $val){ $tableName = $val['TABLE_NAME']; $string = explode('_',$tableName); if($string[0] != $prefix){ $string[0] = $prefix; $newTableName = implode('_', $string); mysql_query('rename table '.$tableName.' TO '.$newTableName); } } echo "替换成功!";exit(); } */ //循环取得所有表的备注及表中列消息 foreach ($tables as $k => $v) { $sql = 'SELECT * FROM '; $sql .= 'INFORMATION_SCHEMA.TABLES '; $sql .= 'WHERE '; $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'"; $table_result = mysql_query($sql, $mysql_conn); while ($t = mysql_fetch_array($table_result)) { $tables[$k]['TABLE_COMMENT'] = $t['TABLE_COMMENT']; } $sql = 'SELECT * FROM '; $sql .= 'INFORMATION_SCHEMA.COLUMNS '; $sql .= 'WHERE '; $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'"; /* $sql = 'SELECT a.COLUMN_NAME,a.COLUMN_TYPE, a.COLUMN_DEFAULT ,a.IS_NULLABLE ,a.EXTRA,a.COLUMN_COMMENT ,'; $sql .= ' b.REFERENCED_TABLE_NAME, b.REFERENCED_COLUMN_NAME FROM '; $sql .= 'INFORMATION_SCHEMA.COLUMNS a LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE b '; $sql .= ' on '; $sql .=" a.table_schema = b.table_schema AND a.table_schema = '{$database}' "; $sql .= " AND a.table_name = b.table_name AND a.table_name = '{$v['TABLE_NAME']}' "; $sql .=" AND a.column_name = b.column_name "; */ $fields = array(); $field_result = mysql_query($sql, $mysql_conn); while ($t = mysql_fetch_array($field_result)) { $t['REFERENCED_TABLE_NAME']=""; $t['REFERENCED_COLUMN_NAME']=""; $t['IS_FOREIGNKEY']= '<span >否</span>'; $sql = 'SELECT b.REFERENCED_TABLE_NAME, b.REFERENCED_COLUMN_NAME FROM '; $sql .= 'INFORMATION_SCHEMA.COLUMNS a , INFORMATION_SCHEMA.KEY_COLUMN_USAGE b '; $sql .= ' where'; $sql .=" a.table_schema = b.table_schema AND a.table_schema = '{$database}' "; $sql .= " AND a.table_name = b.table_name AND a.table_name = '{$v['TABLE_NAME']}' "; $sql .=" AND a.column_name = b.column_name AND a.column_name ='{$t['COLUMN_NAME']}' "; $sql .=" AND b.REFERENCED_TABLE_NAME IS NOT NULL "; $fk = mysql_query($sql, $mysql_conn); if($fk){ while ($k1 = mysql_fetch_array($fk)) { $t['REFERENCED_TABLE_NAME']= $k1['REFERENCED_TABLE_NAME']; $t['REFERENCED_COLUMN_NAME']= $k1['REFERENCED_COLUMN_NAME']; $t['IS_FOREIGNKEY']= '<span style="color :red;">是</span>'; } } /* if($sql&& d231 ;$k ){ echo '<pre>'; print_r($k ); echo '</pre>'; exit; } if($sql&&$t['COLUMN_NAME']=='belongto'){ echo '<pre>'; print_r($t); echo '</pre>'; exit; } if($t['REFERENCED_TABLE_NAME']){ echo '<pre>'; print_r($t); echo '</pre>'; exit; } */ $fields[] = $t; } $tables[$k]['COLUMN'] = $fields; } mysql_close($mysql_conn); $html = ''; //循环所有表 foreach ($tables as $k => $v) { //$html .= ' <h3>' . ($k + 1) . '、' . $v['TABLE_COMMENT'] .' ('. $v['TABLE_NAME']. ')</h3>'."\n"; /* <table> <tr> <th>Month</th> </tr> </table> */ $html .= ' <table border="0" cellspacing="0" cellpadding="0" width="100%">' . "\n"; $html .= ' <tr>' . "\n"; $html .= ' <th>' . ($k + 1) . '、' . $v['TABLE_COMMENT'] . ' (' . $v['TABLE_NAME'] . ')' . '</th>' . "\n"; $html .= ' </tr>' . "\n"; $html .= ' </table>' . "\n"; $html .= ' <table border="1" cellspacing="0" cellpadding="0" width="100%" style="margin-bottom:20px">' . "\n"; $html .= ' <tbody>' . "\n"; $html .= ' <tr>' . "\n"; $html .= ' <th>字段名</th>' . "\n"; $html .= ' <th>数据类型</th>' . "\n"; $html .= ' <th>默认值</th>' . "\n"; $html .= ' <th>是否外键</th>' . "\n"; $html .= ' <th>父表名称</th>' . "\n"; $html .= ' <th>父表字段</th>' . "\n"; $html .= ' <th>允许非空</th>' . "\n"; $html .= ' <th>自动递增</th>' . "\n"; $html .= ' <th>备注</th>' . "\n"; $html .= ' </tr>' . "\n"; foreach ($v['COLUMN'] as $f) { if (!is_array($no_show_field[$v['TABLE_NAME']])) { $no_show_field[$v['TABLE_NAME']] = array(); } if (!in_array($f['COLUMN_NAME'], $no_show_field[$v['TABLE_NAME']])) { $html .= ' <tr>' . "\n"; $html .= ' <td class="c1">' . $f['COLUMN_NAME'] . '</td>' . "\n"; $html .= ' <td class="c2">' . $f['COLUMN_TYPE'] . '</td>' . "\n"; $html .= ' <td class="c3">' . $f['COLUMN_DEFAULT'] . '</td>' . "\n"; $html .= ' <td class="c3">' . $f['IS_FOREIGNKEY'] . '</td>' . "\n"; $html .= ' <td class="c4">' . $f['REFERENCED_TABLE_NAME'] . '</td>' . "\n"; $html .= ' <td class="c4">' . $f['REFERENCED_COLUMN_NAME'] . '</td>' . "\n"; $html .= ' <td class="c4">' . $f['IS_NULLABLE'] . '</td>' . "\n"; $html .= ' <td class="c5">' . ($f['EXTRA'] == 'auto_increment' ? '是' : ' ') . '</td>' . "\n"; $html .= ' <td class="c6">' . $f['COLUMN_COMMENT'] . '</td>' . "\n"; $html .= ' </tr>' . "\n"; } } //多一个空行 ,这样到处Excel表的话,就会有一个空行,比较美观 $html .= ' <tr>' . "\n"; $html .= ' <td class="c1"></td>' . "\n"; $html .= ' <td class="c2"></td>' . "\n"; $html .= ' <td class="c3"></td>' . "\n"; $html .= ' <td class="c4"></td>' . "\n"; $html .= ' <td class="c5"></td>' . "\n"; $html .= ' <td class="c6"></td>' . "\n"; $html .= ' <td class="c7"></td>' . "\n"; $html .= ' <td class="c8"></td>' . "\n"; $html .= ' <td class="c8"></td>' . "\n"; $html .= ' </tr>' . "\n"; $html .= ' </tbody>' . "\n"; $html .= ' </table>' . "\n"; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>数据库数据字典生成代码</title> <style> body, td, th { font-family: "微软雅黑"; font-size: 14px; } .warp{margin:auto; width:1200px;} .warp h3{margin:0px; padding:0px; line-height:30px; margin-top:10px;} table { border-collapse: collapse; border: 1px solid #CCC; background: #efefef; } table th { text-align: left; font-weight: bold; height: 26px; line-height: 26px; font-size: 14px; text-align:center; border: 1px solid #CCC; padding:5px;} table td { height: 20px; font-size: 14px; border: 1px solid #CCC; background-color: #fff; padding:5px;} .c1 { width: 120px; } .c2 { width: 120px; } .c3 { width: 150px; } .c4 { width: 80px; text-align:center;} .c5 { width: 80px; text-align:center;} .c6 { width: 270px; } </style> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script src="jquery.table2excel.min.js"></script> <script> $(function () { $("#btn").click(function () { $(".table2excel").table2excel({ exclude: ".noExl", name: "Excel Document Name", filename: "数据字典", exclude_img: true, exclude_links: true, exclude_inputs: true }); }); }); </script> </head> <body> <h1 style="text-align:center;">数据库数据字典生成代码</h1> <div style = "width:100%;text-align:center;"> <button id="btn" style="padding: 10px;margin: 20px;"> 点击这里将表格内容导出到excel中 </button> </div> <div class="warp table2excel"> <?php echo $html; ?> </div> </body> </html>
相关文章推荐
- Php代码实现将mysql数据库导出为excel表格形式
- php将mysql数据库整库导出生成sql文件[php代码]
- PHP 动态导出生成 excel,csv文件
- 使用PHP一键生成MySQL数据库字典
- PHP导出Excel动态文件名
- 通用PHP动态生成静态HTML网页的代码
- php 导出excel实现方法及代码
- 通用PHP动态生成静态HTML网页的代码
- PHP 动态随机生成验证码类代码
- 动态生成适用于像sql的where条件拼接的面板js代码 与 php处理代码 (一)
- OA (ssh) 基本实现(poi 生成 Excel , struts2动态下载 mysql数据库文件) 学习笔记(含源代码)
- 通用PHP动态生成静态HTML网页的代码
- 通用PHP动态生成静态HTML网页的代码
- php动态生成JavaScript代码
- 通用PHP动态生成静态HTML网页的代码
- 动态生成适用于像sql的where条件拼接的面板js代码 与 php处理代码 (二)
- 分享一个自己开发的数据字典与动态代码生成工具
- 基于JXL的通用Excel动态导出过程/可以在Servlet/JSP生成Excel并下载
- ASP.NET Excel导出(动态生成)解决方案
- 一种最好用的php生成excel的代码 适用于windows和linux