2017-08-25 83 views
0

是否有人會知道有人採用PHPExcel的「01simple-download-xls.php」並將其修改爲與MySQLi查詢?我一直在找一個好的工作例子遇到一些麻煩。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下載-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; 

回答

0

下載PHPExcel here並嘗試下面的代碼。

$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; 
+0

到目前爲止,我還沒有能夠獲得該代碼的工作,但我仍然在玩它。謝謝。 – Reno

0

您可以利用工作表的fromArray()方法來從一個數組作爲一個單一的呼叫建立單元行,不管有多少值在數組中,將工作:

$row=2; 
while ($result = mysqli_fetch_row($queryResultSet)) { 
    $objPHPExcel->getActiveSheet() 
     ->fromArray($result, null, 'A'.$row++); 
} 
+0

這幾乎完美。唯一的問題(我不明白)是它重複每一列。就像第一個數據庫列有「a」,第二個數據庫列有「b」一樣,在電子表格中,您將看到:a,a,b,b – Reno

+0

我的不好....'mysqli_fetch_array()'返回關聯___和____枚舉列;應該使用'mysqli_fetch_assoc()'或'mysqli_fetch_row()',或者使用'mysqli_fetch_array()''MYSQLI_NUM'或'MYSQLI_ASSOC'標誌 –