PHP匯出excel檔案,第二步先實現自寫二維數組加入模板excel檔案後匯出

來源:互聯網
上載者:User

標籤:8 8   off   php   openxml   下一步   error   .sh   遍曆   listorder   

        今天主要研究資料加入EXCEL並匯出的問題,先不從資料庫提取資料匯出,自己先寫一個二維數組,然後遍曆二維數組寫入excel模板中匯出,首先根據模板excel的內容書寫對應的二維數組

$arr=array(array("111-3004394-8497032","UMN207-05MM","UMN207-05MM","2","Eric S Herbert / Entergy","600 Rockyhill Rd","PNPS"," ","plymouth","ma","02360","US","508 830-8823","","","","","","","1",""),
array("112-3297805-3545827","UMN207-05MM","UMN207-05MM","1","Yibai Liao","11 BANNISTER DR"," "," ","HOPEWELL JUNCTION"," ","12533-8204","US","9175926724","","","","","","","1",""));

然後用foreach遍曆二維數組並寫入模板excle檔案具體代碼如下 

$row=2;

foreach ($arr as $order)
{
$col=0;
foreach($order as $val)

$objPHPExcel->setActiveSheetIndex()->setCellValueByColumnAndRow($col, $row, $val);
$col++;
}

$row++;
}

$row為行,指定從第二行插入資料,並且迴圈換行, $val為二維數組資料,$col為列,基本實現資料寫入excel表格並匯出,整體代碼如下:

<?php
if($_POST[‘eub‘]==1)
{
header("location:ListOrders.php?t=OrderStatus&fc=all&orderstatus=Unshipped&range=14&submit=Go");
}
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__) . ‘/../plugins/PHPExcel-1.8/Classes/PHPExcel.php‘;


// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Read from Excel2007 (.xlsx) template
//echo date(‘H:i:s‘) , " Load Excel5 template file" , EOL;
$objReader = PHPExcel_IOFactory::createReader(‘Excel5‘);
$objPHPExcel = $objReader->load("../template/eub.xls");
//print_r($objPHPExcel);
//die();

$filename = "eub-".date("YmdHis").".xls";

$row = 2;

// Add some data
$arr=array(array("111-3004394-8497032","UMN207-05MM","UMN207-05MM","2","Eric S Herbert / Entergy","600 Rockyhill Rd","PNPS"," ","plymouth","ma","02360","US","508 830-8823","","","","","","","1",""),
array("112-3297805-3545827","UMN207-05MM","UMN207-05MM","1","Yibai Liao","11 BANNISTER DR"," "," ","HOPEWELL JUNCTION"," ","12533-8204","US","9175926724","","","","","","","1",""));

foreach ($OrderList as $order)
{
$col=0;
foreach($order as $val)

$objPHPExcel->setActiveSheetIndex()->setCellValueByColumnAndRow($col, $row, $val);
$col++;
}

$row++;
}

// Redirect output to a client’s web browser (Excel2007)
header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset="GB2312"‘);
header(‘Content-Disposition: attachment;filename="‘.$filename.‘"‘);
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, ‘Excel2007‘);
$objWriter->save(‘php://output‘);
exit;
?>

 

下一步就是從資料庫讀取資料寫入excel模板表並匯出的實現過程。

PHP匯出excel檔案,第二步先實現自寫二維數組加入模板excel檔案後匯出

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.