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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

PHP variables store values during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.
