国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法

2024-05-04 23:40:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法,以實(shí)例形式較為詳細(xì)的分析了PHP使用PHPexcel實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入與導(dǎo)出操作相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:

導(dǎo)入數(shù)據(jù):

 

 
  1. <?php 
  2. error_reporting(E_ALL); //開(kāi)啟錯(cuò)誤 
  3. set_time_limit(0); //腳本不超時(shí) 
  4. date_default_timezone_set('Europe/London'); //設(shè)置時(shí)間 
  5. /** Include path **/ 
  6. set_include_path(get_include_path() . PATH_SEPARATOR . 'http://m.survivalescaperooms.com/../Classes/');//設(shè)置環(huán)境變量 
  7. /** PHPExcel_IOFactory */ 
  8. include 'PHPExcel/IOFactory.php'
  9. //$inputFileType = 'Excel5'; //這個(gè)是讀 xls的 
  10. $inputFileType = 'Excel2007';//這個(gè)是計(jì)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(); //取得總行數(shù) 
  19. $highestColumn = $sheet->getHighestColumn(); //取得總列 
  20. */ 
  21. $objWorksheet = $objPHPExcel->getActiveSheet();//取得總行數(shù) 
  22. $highestRow = $objWorksheet->getHighestRow();//取得總列數(shù) 
  23. echo 'highestRow='.$highestRow; 
  24. echo "<br>"
  25. $highestColumn = $objWorksheet->getHighestColumn(); 
  26. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//總列數(shù) 
  27. echo 'highestColumnIndex='.$highestColumnIndex; 
  28. echo "<br />"
  29. $headtitle=array(); 
  30. for ($row = 1;$row <= $highestRow;$row++) 
  31. $strs=array(); 
  32. //注意highestColumnIndex的列數(shù)索引從0開(kāi)始 
  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. //在這兒,你可以連接,你的數(shù)據(jù)庫(kù),寫(xiě)入數(shù)據(jù)庫(kù)了 
  42. print_r($info); 
  43. echo '<br />'
  44. ?> 

導(dǎo)出數(shù)據(jù):

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

 

  1. private function _export_data($data = array()) 
  2. error_reporting(E_ALL); //開(kāi)啟錯(cuò)誤 
  3. set_time_limit(0); //腳本不超時(shí) 
  4. date_default_timezone_set('Europe/London'); //設(shè)置時(shí)間 
  5. /** Include path **/ 
  6. set_include_path(FCPATH.APPPATH.'/libraries/Classes/');//設(shè)置環(huán)境變量 
  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');  

希望本文所述對(duì)大家php程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚东县| 登封市| 长春市| 红河县| 信宜市| 买车| 宜兴市| 伽师县| 吴桥县| 湟中县| 资源县| 同德县| 满洲里市| 金沙县| 丹阳市| 禄劝| 太和县| 肃宁县| 淮北市| 呈贡县| 马尔康县| 桦甸市| 洪洞县| 灌南县| 新乡市| 松原市| 宜城市| 华亭县| 托克托县| 孝昌县| 巴彦淖尔市| 萝北县| 宣威市| 宿迁市| 开江县| 新巴尔虎右旗| 新疆| 儋州市| 舟山市| 曲阜市| 襄垣县|