phpexcel 导出mysql6_用phpexcel导出mysql数据库表到exc…

header("Content-Type: text/html;charset=utf-8");

date_default_timezone_set('Asia/Shanghai');

$my_host = "127.0.0.1"; //主机

$my_user = "

xxxxxx"; //用户名

$my_pass = "

xxxxxx"; //密码

$my_data = "xxxxxx";  //MySQL库名

// Connect to MySQL

$mysqli = new mysqli($my_host, $my_user, $my_pass,$my_data);

if ($mysqli->connect_errno) {

printf("Connect failed: %s\n", $mysqli->connect_error);

exit();

}

//设置连接的字符集,也可以用$mysqli->query("SET NAMES UTF8");来设置

//$mysqli->query("SET NAMES UTF8");

$mysqli->set_charset("utf8");

require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '

');

// Create new PHPExcel object

echo date('H:i:s') , " Create new PHPExcel object" , EOL;

$objPHPExcel = new PHPExcel();

// Set document properties

echo date('H:i:s') , " Set document properties" , EOL;

$objPHPExcel->getProperties()->setCreator("wangxuwei")

->setLastModifiedBy("wangxuwei")

->setTitle("PHPExcel Test document")

->setSubject("PHPExcel Test document")

->setDescription("Test document for PHPExcel, generated using PHP classes.")

->setKeywords("office PHPExcel php")

->setCategory("Test result file");

// Add some data

echo date('H:i:s') , " Add title" , EOL;

$objPHPExcel->setActiveSheetIndex(0)

->setCellValue('A1', '学号')

->setCellValue('B1', '姓名')

->setCellValue('C1', '生日')

->setCellValue('D1', '性别')

->setCellValue('E1', '研究方向');

//执行一个简单查询,结果输出到excel文件

echo date('H:i:s') , " Add data from MySQL server " , EOL;

$result=$mysqli->query('select SNO,SNAME,BDATE,SEX,DIR from STUDENT');

$i=1;

while($row=$result->fetch_array(MYSQLI_BOTH))

{

$i++;

$objPHPExcel->setActiveSheetIndex(0)

->setCellValue('A'.$i,$row['SNO'])

->setCellValue('B'.$i,$row['SNAME'])

->setCellValue('C'.$i,$row['BDATE'])

->setCellValue('D'.$i,$row['SEX'])

->setCellValue('E'.$i,$row['DIR']);

}

// Rename worksheet

echo date('H:i:s') , " Rename worksheet" , EOL;

$objPHPExcel->getActiveSheet()->setTitle('学生信息表');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet

$objPHPExcel->setActiveSheetIndex(0);

// Save Excel 2007 file

echo date('H:i:s') , " Write to Excel2007 format" , EOL;

$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

$callEndTime = microtime(true);

$callTime = $callEndTime - $callStartTime;

echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;

echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;

// Save Excel 95 file

echo date('H:i:s') , " Write to Excel5 format" , EOL;

$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save(str_replace('.php', '.xls', __FILE__));

$callEndTime = microtime(true);

$callTime = $callEndTime - $callStartTime;

echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;

echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;

// Echo done

echo date('H:i:s') , " Done writing files" , EOL;

echo 'Files have been created in ' , getcwd() , EOL;

// Clean up

$result->close();

$mysqli->close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值