PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法
本文實(shí)例講述了PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
導(dǎo)入數(shù)據(jù):
<?php
error_reporting(E_ALL); //開啟錯(cuò)誤
set_time_limit(0); //腳本不超時(shí)
date_default_timezone_set('Europe/London'); //設(shè)置時(shí)間
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'http://www.dhdzp.com/../Classes/');//設(shè)置環(huán)境變量
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
//$inputFileType = 'Excel5'; //這個(gè)是讀 xls的
$inputFileType = 'Excel2007';//這個(gè)是計(jì)xlsx的
//$inputFileName = './sampleData/example2.xls';
$inputFileName = './sampleData/book.xlsx';
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
/*
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); //取得總行數(shù)
$highestColumn = $sheet->getHighestColumn(); //取得總列
*/
$objWorksheet = $objPHPExcel->getActiveSheet();//取得總行數(shù)
$highestRow = $objWorksheet->getHighestRow();//取得總列數(shù)
echo 'highestRow='.$highestRow;
echo "<br>";
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//總列數(shù)
echo 'highestColumnIndex='.$highestColumnIndex;
echo "<br />";
$headtitle=array();
for ($row = 1;$row <= $highestRow;$row++)
{
$strs=array();
//注意highestColumnIndex的列數(shù)索引從0開始
for ($col = 0;$col < $highestColumnIndex;$col++)
{
$strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
$info = array(
'word1'=>"$strs[0]",
'word2'=>"$strs[1]",
'word3'=>"$strs[2]",
'word4'=>"$strs[3]",
);
//在這兒,你可以連接,你的數(shù)據(jù)庫,寫入數(shù)據(jù)庫了
print_r($info);
echo '<br />';
}
?>
導(dǎo)出數(shù)據(jù):
(如果有特殊的字符串 = 麻煩 str_replace(array('='),'',$val['roleName']);)
private function _export_data($data = array())
{
error_reporting(E_ALL); //開啟錯(cuò)誤
set_time_limit(0); //腳本不超時(shí)
date_default_timezone_set('Europe/London'); //設(shè)置時(shí)間
/** Include path **/
set_include_path(FCPATH.APPPATH.'/libraries/Classes/');//設(shè)置環(huán)境變量
// Create new PHPExcel object
Include 'PHPExcel.php';
$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
$letter = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
if($data){
$i = 1;
foreach ($data as $key => $value) {
$newobj = $objPHPExcel->setActiveSheetIndex(0);
$j = 0;
foreach ($value as $k => $val) {
$index = $letter[$j]."$i";
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($index, $val);
$j++;
}
$i++;
}
}
$date = date('Y-m-d',time());
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle($date);
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client's web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$date.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
直接上代碼:
public function export_data($data = array())
{
# code...
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/Writer/IWriter.php') ;
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/Writer/Excel5.php') ;
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel.php') ;
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/IOFactory.php') ;
$obj_phpexcel = new PHPExcel();
$obj_phpexcel->getActiveSheet()->setCellValue('a1','Key');
$obj_phpexcel->getActiveSheet()->setCellValue('b1','Value');
if($data){
$i =2;
foreach ($data as $key => $value) {
# code...
$obj_phpexcel->getActiveSheet()->setCellValue('a'.$i,$value);
$i++;
}
}
$obj_Writer = PHPExcel_IOFactory::createWriter($obj_phpexcel,'Excel5');
$filename = "outexcel.xls";
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$obj_Writer->save('php://output');
}
希望本文所述對(duì)大家php程序設(shè)計(jì)有所幫助。
- 使用PHPExcel實(shí)現(xiàn)數(shù)據(jù)批量導(dǎo)出為excel表格的方法(必看)
- 利用phpExcel實(shí)現(xiàn)Excel數(shù)據(jù)的導(dǎo)入導(dǎo)出(全步驟詳細(xì)解析)
- ThinkPHP使用PHPExcel實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入導(dǎo)出完整實(shí)例
- php實(shí)現(xiàn)利用phpexcel導(dǎo)出數(shù)據(jù)
- Yii中使用PHPExcel導(dǎo)出Excel的方法
- PHPExcel導(dǎo)出2003和2007的excel文檔功能示例
- Codeigniter+PHPExcel實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)到Excel文件
- thinkphp3.2中實(shí)現(xiàn)phpexcel導(dǎo)出帶生成圖片示例
- 基于PHPExcel的常用方法總結(jié)
- PHPExcel實(shí)現(xiàn)表格導(dǎo)出功能示例【帶有多個(gè)工作sheet】
相關(guān)文章
PHP MemCached 高級(jí)緩存應(yīng)用代碼
PHP MemCached 高級(jí)緩存應(yīng)用,使用MemCached的學(xué)習(xí)的朋友可以參考下。2010-08-08
PHP?array_combine()?函數(shù)內(nèi)置函數(shù)
這篇文章主要介紹了PHP?array_combine()函數(shù)內(nèi)置函數(shù),array_combine()是PHP中的一個(gè)內(nèi)置函數(shù),用于組合兩個(gè)數(shù)組并通過使用一個(gè)數(shù)組作為鍵和另一個(gè)數(shù)組作為值來創(chuàng)建一個(gè)新數(shù)組2022-09-09
使用cookie實(shí)現(xiàn)統(tǒng)計(jì)訪問者登陸次數(shù)
本篇文章是對(duì)使用cookie實(shí)現(xiàn)統(tǒng)計(jì)訪問者登陸次數(shù)的代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
非常好用的兩個(gè)PHP函數(shù) serialize()和unserialize()
使用serialize()函數(shù)和unserialize()函數(shù),這兩個(gè)函數(shù)的用法真是絕配,一個(gè)是進(jìn)行序列化存儲(chǔ),另一個(gè)則是進(jìn)行序列化恢復(fù),方便極了2012-02-02
使用zend studio for eclipse不能激活代碼提示功能的解決辦法
相信有蠻多人用zend studio for eclipse寫代碼吧,但有時(shí)候好好的一個(gè)項(xiàng)目就突然沒得語法提示,很郁悶。2009-10-10
PHP整數(shù)取余返回負(fù)數(shù)的相關(guān)解決方法
PHP整數(shù)取余返回負(fù)數(shù)對(duì)于編程經(jīng)驗(yàn)不足的新手來說還是比較頭疼的一件事。希望通過本文介紹的內(nèi)容,大家能充分掌握這一解決辦法2014-05-05

