麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > PHP > 正文

PHP使用PHPexcel導入導出數據的方法

2024-05-04 23:40:24
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP使用PHPexcel導入導出數據的方法,以實例形式較為詳細的分析了PHP使用PHPexcel實現數據的導入與導出操作相關技巧,需要的朋友可以參考下

本文實例講述了PHP使用PHPexcel導入導出數據的方法。分享給大家供大家參考,具體如下:

導入數據:

 

 
  1. <?php 
  2. error_reporting(E_ALL); //開啟錯誤 
  3. set_time_limit(0); //腳本不超時 
  4. date_default_timezone_set('Europe/London'); //設置時間 
  5. /** Include path **/ 
  6. set_include_path(get_include_path() . PATH_SEPARATOR . 'http://www.companysz.com/../Classes/');//設置環境變量 
  7. /** PHPExcel_IOFactory */ 
  8. include 'PHPExcel/IOFactory.php'
  9. //$inputFileType = 'Excel5'; //這個是讀 xls的 
  10. $inputFileType = 'Excel2007';//這個是計xlsx的 
  11. //$inputFileName = './sampleData/example2.xls'; 
  12. $inputFileName = './sampleData/book.xlsx'
  13. echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />'
  14. $objReader = PHPExcel_IOFactory::createReader($inputFileType); 
  15. $objPHPExcel = $objReader->load($inputFileName); 
  16. /* 
  17. $sheet = $objPHPExcel->getSheet(0); 
  18. $highestRow = $sheet->getHighestRow(); //取得總行數 
  19. $highestColumn = $sheet->getHighestColumn(); //取得總列 
  20. */ 
  21. $objWorksheet = $objPHPExcel->getActiveSheet();//取得總行數 
  22. $highestRow = $objWorksheet->getHighestRow();//取得總列數 
  23. echo 'highestRow='.$highestRow; 
  24. echo "<br>"
  25. $highestColumn = $objWorksheet->getHighestColumn(); 
  26. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//總列數 
  27. echo 'highestColumnIndex='.$highestColumnIndex; 
  28. echo "<br />"
  29. $headtitle=array(); 
  30. for ($row = 1;$row <= $highestRow;$row++) 
  31. $strs=array(); 
  32. //注意highestColumnIndex的列數索引從0開始 
  33. for ($col = 0;$col < $highestColumnIndex;$col++) 
  34. $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue(); 
  35. $info = array( 
  36. 'word1'=>"$strs[0]"
  37. 'word2'=>"$strs[1]"
  38. 'word3'=>"$strs[2]"
  39. 'word4'=>"$strs[3]"
  40. ); 
  41. //在這兒,你可以連接,你的數據庫,寫入數據庫了 
  42. print_r($info); 
  43. echo '<br />'
  44. ?> 

導出數據:

(如果有特殊的字符串 = 麻煩  str_replace(array('='),'',$val['roleName']);)

 

  1. private function _export_data($data = array()) 
  2. error_reporting(E_ALL); //開啟錯誤 
  3. set_time_limit(0); //腳本不超時 
  4. date_default_timezone_set('Europe/London'); //設置時間 
  5. /** Include path **/ 
  6. set_include_path(FCPATH.APPPATH.'/libraries/Classes/');//設置環境變量 
  7. // Create new PHPExcel object 
  8. Include 'PHPExcel.php'
  9. $objPHPExcel = new PHPExcel(); 
  10. // Set document properties 
  11. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw"
  12. ->setLastModifiedBy("Maarten Balliauw"
  13. ->setTitle("Office 2007 XLSX Test Document"
  14. ->setSubject("Office 2007 XLSX Test Document"
  15. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes."
  16. ->setKeywords("office 2007 openxml php"
  17. ->setCategory("Test result file"); 
  18. // Add some data 
  19. $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');  
  20. if($data){ 
  21. $i = 1; 
  22. foreach ($data as $key => $value) { 
  23. $newobj = $objPHPExcel->setActiveSheetIndex(0); 
  24. $j = 0;  
  25. foreach ($value as $k => $val) { 
  26. $index = $letter[$j]."$i"
  27. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($index, $val); 
  28. $j++; 
  29. $i++; 
  30. }  
  31. $date = date('Y-m-d',time());  
  32. // Rename worksheet 
  33. $objPHPExcel->getActiveSheet()->setTitle($date); 
  34. $objPHPExcel->setActiveSheetIndex(0); 
  35. // Redirect output to a client's web browser (Excel2007) 
  36. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
  37. header('Content-Disposition: attachment;filename="'.$date.'.xlsx"'); 
  38. header('Cache-Control: max-age=0'); 
  39. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
  40. $objWriter->save('php://output'); 
  41. exit; 

直接上代碼:

 

 
  1. public function export_data($data = array()) 
  2. # code... 
  3. include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/Writer/IWriter.php') ; 
  4. include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/Writer/Excel5.php') ; 
  5. include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel.php') ; 
  6. include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/IOFactory.php') ; 
  7. $obj_phpexcel = new PHPExcel(); 
  8. $obj_phpexcel->getActiveSheet()->setCellValue('a1','Key'); 
  9. $obj_phpexcel->getActiveSheet()->setCellValue('b1','Value');  
  10. if($data){ 
  11. $i =2; 
  12. foreach ($data as $key => $value) { 
  13. # code... 
  14. $obj_phpexcel->getActiveSheet()->setCellValue('a'.$i,$value); 
  15. $i++; 
  16. }  
  17. $obj_Writer = PHPExcel_IOFactory::createWriter($obj_phpexcel,'Excel5'); 
  18. $filename = "outexcel.xls"
  19. header("Content-Type: application/force-download");  
  20. header("Content-Type: application/octet-stream");  
  21. header("Content-Type: application/download");  
  22. header('Content-Disposition:inline;filename="'.$filename.'"');  
  23. header("Content-Transfer-Encoding: binary");  
  24. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  
  25. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");  
  26. header("Pragma: no-cache");  
  27. $obj_Writer->save('php://output');  

希望本文所述對大家php程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美成年人视频在线观看 | 久久免费激情视频 | 一级毛片免费高清 | 欧美精品成人一区二区三区四区 | 精品一区二区电影 | 青热久思思| 亚洲性一区 | www.91视频com | 欧美日韩国产一区二区三区在线观看 | 欧美成年人在线视频 | 成人午夜免费看 | 午夜精品福利影院 | 久久嗨| 久久精品小短片 | 羞羞视频在线免费 | caoporn国产一区二区 | 亚洲欧美在线视频免费 | 国产chinesehd精品91 | 亚洲国产成人久久成人52 | 高清国产在线 | 色网站免费观看 | 成年人福利视频 | 欧美成在线视频 | av电影免费观看 | 久久影院yy6080 | 国产日韩欧美 | 久久91亚洲精品久久91综合 | 91久久在线观看 | 成人福利在线视频 | 亚洲自拍第二页 | 1024亚洲天堂 | 国语自产免费精品视频在 | 日韩中文字幕一区二区三区 | 在线成人免费视频 | 黄色网址你懂的 | 一区二区三区国产视频 | 欧美 日韩 三区 | 国产小视频在线观看 | 精品黑人一区二区三区国语馆 | 九九热免费视频在线观看 | 越南一级黄色片 |