Home > php教程 > php手册 > thinkphp3.2.3结合PHPExcel导出数据库里所有表的结构

thinkphp3.2.3结合PHPExcel导出数据库里所有表的结构

WBOY
Release: 2016-06-07 11:39:57
Original
1017 people have browsed it

前面已经做过了一个关于这方面的demo,但没有对excel进行格式设置,用户体验不友好,这次做了改正,希望在项目开发的时候,大家能用得上.
按惯例,先上图
thinkphp3.2.3结合PHPExcel导出数据库里所有表的结构

代码如下:  public function out(){    <br>         //读取库里所有的表            <br>         $sql="show tables";   <br>         $result=M()->query($sql);  <br>         foreach ($result as $k=>$v) {             <br>             $k++;                     <br>             $_sql="SHOW FULL COLUMNS FROM ".$v['tables_in_'.C('DB_NAME')]; <br>             $data[][0]=array("表 {$k}.".$v['tables_in_'.C('DB_NAME')]."表",'','','','','','');<br>             $data[][1]=array("字段","类型","校对","NULL","键","默认","额外","权限","注释");<br>             $data[]=M()->query($_sql);  <br> <br>             $data[][]=array();                      <br>         }          <br>         //导入PHPExcel类库         <br>         import("Common.Org.PHPExcel");        <br>         import("Common.Org.PHPExcel.Writer.Excel5");         <br>         import("Common.Org.PHPExcel.IOFactory.php");         <br>         $filename="test_excel";              <br>         $this->getExcel($filename,$data);    <br>     }  <br> <br>     private function getExcel($fileName,$data){             <br>     //对数据进行检验            <br>          if(empty($data)||!is_array($data)){                 <br>              die("data must be a array");             <br>          }             <br>         $date=date("Y_m_d",time()); <br>         $fileName.="_{$date}.xls";              <br>         //创建PHPExcel对象,注意,不能少了\             <br>         $objPHPExcel=new \PHPExcel();             <br>         $objProps=$objPHPExcel->getProperties();  <br> <br>         $column=2;             <br>         $objActSheet=$objPHPExcel->getActiveSheet();   <br>         $objPHPExcel->getActiveSheet()->getStyle()->getFont()->setName('微软雅黑');//设置字体<br>         $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(25);//设置默认高度<br> <br>         $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('5');//设置列宽<br>         $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('22');//设置列宽<br>         $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('22');//设置列宽<br>         $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth('40');//设置列宽<br> <br>         //设置边框<br>         $sharedStyle1=new \PHPExcel_Style();<br>         $sharedStyle1->applyFromArray(array('borders'=>array('allborders'=>array('style'=>\PHPExcel_Style_Border::BORDER_THIN))));<br>         <br>         foreach ($data as $ke=>$row){      <br> <br>             foreach($row as $key=>$rows){<br> <br>                 if(count($row)==1&&empty($row[0][1])&&empty($rows[1])&&!empty($rows)){<br> <br>                     $objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "A{$column}:J{$column}");//设置边框<br>                     array_unshift($rows,$rows['0']);<br>                     $objPHPExcel->getActiveSheet()->mergeCells("A{$column}:J{$column}");//合并单元格<br>                     $objPHPExcel->getActiveSheet()->getStyle("A{$column}:J{$column}")->getFont()->setSize(12);//字体<br>                     $objPHPExcel->getActiveSheet()->getStyle("A{$column}:J{$column}")->getFont()->setBold(true);//粗体<br> <br>                     //背景色填充<br>                     $objPHPExcel->getActiveSheet()->getStyle("A{$column}:J{$column}")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);<br>                     $objPHPExcel->getActiveSheet()->getStyle("A{$column}:J{$column}")->getFill()->getStartColor()->setARGB('FFB8CCE4');<br> <br>                 }else{<br>                     if(!empty($rows)){<br>                         array_unshift($rows,$key+1);<br>                         $objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1,"A{$column}:J{$column}");//设置边框<br>                     } <br>                 }<br> <br>                 if($rows['1']=='字段'){<br>                     $rows[0]='ID';<br>                     //背景色填充<br>                     $objPHPExcel->getActiveSheet()->getStyle("A{$column}:J{$column}")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);<br>                     $objPHPExcel->getActiveSheet()->getStyle("A{$column}:J{$column}")->getFill()->getStartColor()->setARGB('FF4F81BD');<br>                 }<br> <br>                 $objPHPExcel->getActiveSheet()->getStyle("A{$column}:J{$column}")->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中<br>                 $objPHPExcel->getActiveSheet()->getStyle("A{$column}:J{$column}")->getAlignment()->setWrapText(true);//换行<br>                  //行写入                     <br>                 $span = ord("A");                       <br>                 foreach($rows as $keyName=>$value){                    <br>                     // 列写入                       <br>                     $j=chr($span);                         <br>                     $objActSheet->setCellValue($j.$column, $value);                        <br>                     $span++;                     <br>                 }                     <br>                 $column++;                 <br>             }             <br>         } <br>         $fileName = iconv("utf-8", "gb2312", $fileName);             <br>         //设置活动单指数到第一个表,所以Excel打开这是第一个表             <br>         $objPHPExcel->setActiveSheetIndex(0);             <br>         header('Content-Type: application/vnd.ms-excel');             <br>         header("Content-Disposition: attachment;filename=\"$fileName\"");             <br>         header('Cache-Control: max-age=0');                <br>         $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');             <br>         $objWriter->save('php://output'); //文件通过浏览器下载             <br>         exit;     <br>     }PHPExcel插件放在应用目录下的Common模块下的Org文件夹里,附带附件,大家可以下载,大家也可以改进,希望能把改进后的分享出来!

附件 tp323-PHPExcel.zip ( 1.91 MB 下载:711 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template