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

首頁 > 開發(fā) > PHP > 正文

phpexcel導出與讀取excel的經(jīng)典實例

2024-05-04 21:49:51
字體:
供稿:網(wǎng)友

phpexcel可以讓開發(fā)者方便快捷的來操作xls文件了,我們下文來整理幾個關(guān)于phpexcel對數(shù)據(jù)導入與導出例子.

PHPExcel讀取excel文件,實例代碼如下:

  1. require_once('include/common.inc.php'); 
  2.    require_once(ROOTPATH . 'include/phpExcel/PHPExcel/IOFactory.php'); 
  3.     
  4.    $filePath = './file/xls/110713.xls';  
  5.     
  6.    $fileType = PHPExcel_IOFactory::identify($filePath); //文件名自動判斷文件類型 
  7.    $objReader = PHPExcel_IOFactory::createReader($fileType); 
  8.    $objPHPExcel = $objReader->load($filePath); 
  9.     
  10.    $currentSheet = $objPHPExcel->getSheet(0); //第一個工作簿 
  11.    $allRow = $currentSheet->getHighestRow(); //行數(shù) 
  12.    $output = array(); 
  13.    $preType = ''
  14.     
  15.    $qh = $currentSheet->getCell('A4')->getValue(); 
  16.    //按照文件格式從第7行開始循環(huán)讀取數(shù)據(jù) 
  17.    for($currentRow = 7;$currentRow<=$allRow;$currentRow++){  
  18.        //判斷每一行的B列是否為有效的序號,如果為空或者小于之前的序號則結(jié)束 
  19.        $xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue(); 
  20.        if(emptyempty($xh))break
  21.         
  22.        $tmpType = (string)$currentSheet->getCell('C'.$currentRow)->getValue(); //賽事類型 
  23.        if(!emptyempty($tmpType))$preType = $tmpType
  24.        $output[$xh]['type'] = $preType
  25.        $output[$xh]['master'] = $currentSheet->getCell('F'.$currentRow)->getValue(); //主隊 
  26.        $output[$xh]['guest'] = $currentSheet->getCell('H'.$currentRow)->getValue(); //客隊     
  27.    } 
  28.     
  29.    //從當前行開始往下循環(huán),取出第一個不為空的行 
  30.    for( ; ; $currentRow++){ 
  31.        $xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue(); 
  32.        if(!emptyempty($xh))break
  33.    } 
  34.     
  35.    for( ; $currentRow <= $allRow$currentRow++){ 
  36.        $xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue(); 
  37.        if(emptyempty($xh))break
  38.         
  39.        $output[$xh]['rq'] = $currentSheet->getCell('I'.$currentRow)->getValue(); 
  40.    } 
  41.    header("content-type:text/html; charset=utf-8"); 
  42.     
  43.    echo '期號:' . $qh . "\n\n"
  44.    if(!emptyempty($output)){ 
  45.        printf("%-5s\t%-15s\t%-40s\t%-40s\t%-5s\n"'序號''賽事類型''主隊''客隊''讓球值'); 
  46.        foreach($output as $key => $row){ 
  47.            $format = "%-5d\t%-15s\t%-40s\t%-40s\t%-5s\n"
  48.            printf($format$key$row['type'], $row['master'], $row['guest'], $row['rq']); 
  49.        } 
  50.    } 

phpexcel導出excel數(shù)據(jù)

在服務器端生成靜態(tài)文件,相比直接生成,這兩種方法的主要區(qū)別是生成格式的不同,模板文件完全相同,下邊是一個在上例基礎(chǔ)上更改后的樣子,注意與上例的區(qū)別,代碼如下:

  1. <?php   
  2. // 包含class的基本頭文件 
  3. include("./class/class.php"); 
  4. // 生成excel的基本類定義(注意文件名的大小寫) 
  5. include("./class/phpexcel/PHPExcel.php"); 
  6. // 包含寫Excel5格式的文件,如果需要生成excel2007的文件,包含對應的Writer即可 
  7. include("./class/phpexcel/PHPExcel/Writer/Excel5.php"); 
  8. // 包含寫PDF格式文件 
  9. include("./class/phpexcel/PHPExcel/Writer/PDF.php"); 
  10. // 創(chuàng)建phpexcel對象,此對象包含輸出的內(nèi)容及格式 
  11. $m_objPHPExcel = new PHPExcel(); 
  12. // 模板文件,為了實現(xiàn)格式與內(nèi)容分離,有關(guān)輸出文件具體內(nèi)容實現(xiàn)在模板文件中 
  13. // 模板文件將對象$m_objPHPExcel進行操作 
  14. include("./include/excel.php"); //開源軟件:Vevb.com 
  15. // 輸出文件的類型,excel或pdf 
  16. $m_exportType = "pdf"
  17. $m_strOutputExcelFileName = date('Y-m-j_H_i_s').".xls"// 輸出EXCEL文件名 
  18. $m_strOutputPdfFileName = date('Y-m-j_H_i_s').".pdf"// 輸出PDF文件名 
  19. // 輸出文件保存路徑,此路徑必須可寫 
  20. $m_strOutputPath = "./output/"
  21. // 如果需要輸出EXCEL格式 
  22. if($m_exportType=="excel"){ 
  23. $objWriter = new PHPExcel_Writer_Excel5($m_objPHPExcel); 
  24. $objWriter->save($m_strOutputPath.$m_strOutputExcelFileName);   
  25. // 如果需要輸出PDF格式 
  26. if($m_exportType=="pdf"){ 
  27. $objWriter = new PHPExcel_Writer_PDF($m_objPHPExcel); 
  28. $objWriter->save($m_strOutputPath.$m_strOutputPdfFileName);   
  29. ?>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 莎车县| 濮阳县| 辉南县| 南丹县| 永吉县| 洪江市| 甘孜县| 五指山市| 尤溪县| 白朗县| 诏安县| 会同县| 永福县| 都匀市| 肇东市| 张家港市| 宁城县| 阜新市| 普兰店市| 麻栗坡县| 洞口县| 连南| 安顺市| 全南县| 仁布县| 灵武市| 漠河县| 邹平县| 洱源县| 丹巴县| 尤溪县| 禹州市| 长子县| 托里县| 东乡族自治县| 怀化市| 平利县| 庄河市| 合川市| 舒兰市| 怀来县|