前提:要下載PHPEXCEL庫文件php
如:phpexcel官方下載 , 本人使用下載html
情形一:對於將數據寫入EXCEL表中的用法
header("content-type:text/html;charset=utf-8"); require "PHPEXCEL/Classes/PHPExcel.php"; require "PHPEXCEL/Classes/PHPExcel/IOFactory.php"; require "PHPEXCEL/Classes/PHPExcel/Reader/Excel5.php"; require "PHPEXCEL/Classes/PHPExcel/Reader/Excel2007.php"; require "PHPEXCEL/Classes/PHPExcel/Reader/Excel2003XML.php"; require "PHPEXCEL/Classes/PHPExcel/Writer/Excel2007.php"; require "PHPEXCEL/Classes/PHPExcel/Writer/Excel5.php";
$phpexcel = new PHPExcel();
###### 其中包含了 ‘讀’與‘寫’類########數組
$phpexcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true); //該語句用於換行,但必須與下面配套使用 $phpexcel->getActiveSheet() ->setCellValue('A1',"Tilel \n ID"); //請注意這裏的 '\n' ,這與上面的呼應使用的
$phpexcel->getActiveSheet()->setTitle($code_title); //設置比標題
$phpexcel->getActiveSheet()->getStyle('A1:L1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//設置垂直居中
$phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(2.8); //設置列的寬度
$phpexcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);////設置水平居中
$phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(8);//設置字體大小
$phpexcel->getActiveSheet() ->setCellValue('A'.$i,$row['id']);//寫入數據,其中$i,是變量.如$i = 1,2,3,4,5...
$phpexcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $phpexcel->getActiveSheet()->getStyle('B'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $phpexcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $phpexcel->getActiveSheet()->getStyle('D'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); //設置水平居中,居左/右
$obj_Writer = PHPExcel_IOFactory::createWriter($phpexcel,'Excel5'); $filename = $code_title.".xls";//文件名
//設置header 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');//輸出 die();//種植執行
情形二:對於將EXCEL表中數據讀取的用法
第一步:引用核心庫文件,上面已有描述。
$phpexcel = new PHPExcel(); //同上
$filePath = "/data/home/cn/newadmin/product/uploads/cusabio_common.xls"; //引用一個文件路徑,並作判斷 if(file_exists($filePath)){ 。。。。。。。。。。。。。。。 }
$PHPReader = new PHPExcel_Reader_Excel5(); ### 1 // $PHPReader = new PHPExcel_Reader_Excel2007(); ### 2 ### 其中 1和2 的區別,與文件的後綴名有關。如:xls或者xlsx等 // var_dump($PHPReader); if(!$PHPReader->canRead($filePath)){ $PHPReader = new PHPExcel_Reader_Excel5(); //若是不成功的時候用之前的版原本讀取 if(!$PHPReader->canRead($filePath)){ echo 'no Excel'; return ; } }
$PHPExcel=$PHPReader->load($filePath); //獲取表中的第一個工做表,若是要獲取第二個,把0改成1,依次類推 $currentSheet=$PHPExcel->getSheet(0); //獲取總列數 $allColumn=$currentSheet->getHighestColumn(); //獲取總行數 $allRow=$currentSheet->getHighestRow(); // var_dump($allColumn, $allRow); // exit; for($currentRow=1;$currentRow<=$allRow;$currentRow++){ //從哪列開始,A表示第一列 for($currentColumn='A';$currentColumn != 'AT';$currentColumn++){ //數據座標 $address=$currentColumn.$currentRow; //讀取到的數據,保存到數組$arr中 $data[$currentRow][$currentColumn]=$currentSheet->getCell($address)->getValue(); // var_dump($data[$currentRow][$currentColumn]); } if($data[1]['A'] != "product_name_cn" && $data[1]['B'] != "product_name_en" && $data[1]['C'] != "product_code"){ echo "<script>alert('sorry,該文件讀取信息有誤。請使用系統提供的模板文件!');</script>"; echo "<script>window.history.back();</script>"; exit; } //判斷數組原始是否爲空。若是爲空,跳出循環 if(empty($data[$currentRow]['A']) && empty($data[$currentRow]['B']) && empty($data[$currentRow]['C'])){ unset($data[$currentRow]); break; } }
最後獲得的二位數組:$data,就是excel中的數據,剩下的如何調用各類方法。