Table of Contents
PHPExcel导出mysql数据库数据
Home php教程 php手册 PHPExcel导出mysql数据库数据

PHPExcel导出mysql数据库数据

Jun 13, 2016 am 08:56 AM

PHPExcel导出mysql数据库数据

使用PHPExcel导出文件

 

首先看看上面的文章,写入方法一样,只是中间多了数据库的操作,后面还有excel浏览器输出。

数据库代码(数据库配置文件自行完成)

 

<!--?php
/*db.php*/
	require dirname(__FILE__)./dbconfig.php;

	class db{
		public $conn = null;

		public function __construct($config){
			$this--->conn=mysql_connect($config[&#39;host&#39;],$config[&#39;username&#39;],$config[&#39;password&#39;]) or die(mysql_error());
			mysql_select_db($config[&#39;database&#39;],$this->conn) or die(mysql_error());
			mysql_query(set names .$config[&#39;charset&#39;]) or die(mysql_error());
		}

		public function getResult($sql){
			$resource = mysql_query($sql,$this->conn) or die(mysql_error());
			$res = array();
			while(($row=mysql_fetch_assoc($resource))!=false){
				$res[] = $row;
			}
			return $res;
		}

		public function getUserinfo(){
			$sql = 。。。;
			$res = self::getResult($sql);
			return $res;
		}
	}
Copy after login

数据库导出代码:

<!--?php
	$dir = dirname(__FILE__);
	require $dir./db.php;
	require $dir./PHPExcel.php;

	$db = new db($phpexcel);
	$objPHPExcel = new PHPExcel();

	for($i=0; $i<3; $i++){
		if($i-->0){
			$objPHPExcel->createSheet();
		}
		$objPHPExcel->setActiveSheetIndex($i);
		$objSheet = $objPHPExcel->getActiveSheet();
		$data = $db->getUserinfo();

		$objSheet->setCellValue(A1,编号)->setCellValue(B1,登陆名)
				->setCellValue(C1,昵称)->setCellValue(D1,电子邮箱)
				->setCellValue(E1,学校)->setCellValue(F1,最后登陆时间);

		$j = 2;
		foreach ($data as $key => $value) {
			# code...
			$objSheet->setCellValue(A.$j,$value[&#39;id&#39;])->setCellValue(B.$j,$value[&#39;user_login&#39;])
					->setCellValue(C.$j,$value[&#39;user_nicename&#39;])->setCellValue(D.$j,$value[&#39;user_email&#39;])
					->setCellValue(E.$j,$value[&#39;sch_name&#39;])->setCellValue(F.$j,$value[&#39;last_login_time&#39;]);
			$j++;
		}
	}

	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,Excel5);
	// $objWriter->save($dir.&#39;/export.xls&#39;); //生成excel文件
	browser_export(Excel5,browser_excel03.xls); //浏览器输出
	$objWriter->save(php://output);

	function browser_export($type, $filename){
		if($type == Excel5){
			header(&#39;Content-Type: application/vnd.ms-excel&#39;); //excel2003
		}else{
			header(&#39;Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&#39;); //excel2007
		}
		header(&#39;Content-Disposition: attachment;filename=&#39;.$filename.&#39;&#39;);
		header(&#39;Cache-Control: max-age=0&#39;);
	}
	

	
Copy after login

运行截图:

 


 

 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)