[英]PHPExcel: Using a MySQLi query on '01simple-download-xls.php' example
有人會知道有人采用PHPExcel的“ 01simple-download-xls.php”並對其進行修改以與MySQLi查詢一起使用的一個好例子嗎? 我一直在尋找一個好的例子方面遇到一些麻煩。
我想完整地保留示例中的所有內容,而只用MySQLi查詢替換這部分代碼(如下)。
看來我可以遍歷查詢並轉儲setCellValue(我認為)。 我一直在嘗試找到一種更優雅的解決方案,其中(不知道所涉及的列數)該代碼可以在不做任何修改的情況下適用於任何查詢。
因此,我只想替換此示例的一部分:
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
這是PHPExcel示例(01simple-download-xls.php)中包含的完整示例文件:
<?php
/**
* PHPExcel
*
* Copyright (c) 2006 - 2015 PHPExcel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPExcel
* @package PHPExcel
* @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE##
*/
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
$sql = mysqli_query($conn, "SELECT id FROM invoice_product WHERE model_name = '$mname' && model_no = '$mno' ORDER BY color");
$a=0;
while ($result = mysqli_fetch_array($sql))
{
$id_array[$a] = $result[id];
$a++;
}
$sql2 = mysqli_query($conn, "SELECT * FROM invoice_imei WHERE invoice_product_id IN (".implode(',',$id_array).")");
$setExcelName = "IMEI";
require_once '../PHPExcel/Classes/PHPExcel/IOFactory.php';
require_once '../PHPExcel/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
//setting column heading
$objPHPExcel->getActiveSheet()->setCellValue('A1',"Model Name");
$objPHPExcel->getActiveSheet()->setCellValue('B1',"Model No.");
$objPHPExcel->getActiveSheet()->setCellValue('C1',"Color");
$objPHPExcel->getActiveSheet()->setCellValue('D1',"IMEI");
$objPHPExcel->getActiveSheet()->setCellValue('E1',"Status");
//setting column body
$i=2; //starting from row 2 bcz row 1 set to header
while ($result2 = mysqli_fetch_array($sql2)) {
$sql3 = mysqli_query($conn, "SELECT color FROM invoice_product WHERE id = '$result2[invoice_product_id]'");
$result3 = mysqli_fetch_assoc($sql3);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$mname);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$i,$mno);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$i,$result3[color]);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$i,$result2[imei]);
if ($result2[status] == 0) {
$objPHPExcel->getActiveSheet()->setCellValue('E'.$i,'Not Used');
} else {
$objPHPExcel->getActiveSheet()->setCellValue('E'.$i,'Used');
}
$i++;
}
// Redirect output to a client’s web browser (Excel5)
header('Content-type: application/xlsx');
header('Content-Disposition: attachment;filename="IMEI.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
您可以利用工作表的fromArray()
方法將數組中的一行單元格設置為單個調用,無論數組中有多少值,該行都將起作用:
$row=2;
while ($result = mysqli_fetch_row($queryResultSet)) {
$objPHPExcel->getActiveSheet()
->fromArray($result, null, 'A'.$row++);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.