使用PHPExcel将表从SQL导出到Excel

时间:2015-03-27 17:40:58

标签: php sql phpexcel

我使用this example从我的SQL DB写入Excel。我的大部分数据都是cyrilllic,所以我使用utf8_unicode_ci(可以在代码中看到)编码。

问题如下:模块写入文件,但由于某些错误,其内容无法读取。或者它可能只是Excel 03(我知道它已经老了,是的)我正在使用?任何人都可以帮我这个吗?



<?php
//$host='localhost'; $user='me'; $pass='mypassword'; $DataBase='mydatabase';//define the correct values
require 'connection_data.php';
// open the connexion to the databases server
$Link = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE) or die('Can\'t connect !');
$Link->set_charset('utf8_unicode_ci');//if not by default
//your request
$request='SELECT * FROM Reports';
$result= $Link->query($request);//get the result (ressource)
/** Include PHPExcel */
require_once 'PHPExcel.php';//change if necessary

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$F = $objPHPExcel->getActiveSheet();
$Line=1;

while($row = $result->fetch_assoc()){//extract each record
    $F->setCellValue('A'.$Line, $row['Rep_ID']);
    $F->setCellValue('B'.$Line, $row['TownName']);
	$F->setCellValue('C'.$Line, $row['ShopAdress']);
	$F->setCellValue('D'.$Line, $row['ShopName']);
	$F->setCellValue('E'.$Line, $row['ProductName']);
	$F->setCellValue('F'.$Line, $row['ProductPrice']);
	$F->setCellValue('G'.$Line, $row['AddDate']);
	$F->setCellValue('C'.$Line, $row['UserName']);//write in the sheet
    ++$Line;
}
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="report.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>
&#13;
&#13;
&#13;

0 个答案:

没有答案