phpexcel可以讓開發(fā)者方便快捷的來操作xls文件了,我們下文來整理幾個關(guān)于phpexcel對數(shù)據(jù)導入與導出例子.
PHPExcel讀取excel文件,實例代碼如下:
- require_once('include/common.inc.php');
- require_once(ROOTPATH . 'include/phpExcel/PHPExcel/IOFactory.php');
- $filePath = './file/xls/110713.xls';
- $fileType = PHPExcel_IOFactory::identify($filePath); //文件名自動判斷文件類型
- $objReader = PHPExcel_IOFactory::createReader($fileType);
- $objPHPExcel = $objReader->load($filePath);
- $currentSheet = $objPHPExcel->getSheet(0); //第一個工作簿
- $allRow = $currentSheet->getHighestRow(); //行數(shù)
- $output = array();
- $preType = '';
- $qh = $currentSheet->getCell('A4')->getValue();
- //按照文件格式從第7行開始循環(huán)讀取數(shù)據(jù)
- for($currentRow = 7;$currentRow<=$allRow;$currentRow++){
- //判斷每一行的B列是否為有效的序號,如果為空或者小于之前的序號則結(jié)束
- $xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
- if(emptyempty($xh))break;
- $tmpType = (string)$currentSheet->getCell('C'.$currentRow)->getValue(); //賽事類型
- if(!emptyempty($tmpType))$preType = $tmpType;
- $output[$xh]['type'] = $preType;
- $output[$xh]['master'] = $currentSheet->getCell('F'.$currentRow)->getValue(); //主隊
- $output[$xh]['guest'] = $currentSheet->getCell('H'.$currentRow)->getValue(); //客隊
- }
- //從當前行開始往下循環(huán),取出第一個不為空的行
- for( ; ; $currentRow++){
- $xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
- if(!emptyempty($xh))break;
- }
- for( ; $currentRow <= $allRow; $currentRow++){
- $xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
- if(emptyempty($xh))break;
- $output[$xh]['rq'] = $currentSheet->getCell('I'.$currentRow)->getValue();
- }
- header("content-type:text/html; charset=utf-8");
- echo '期號:' . $qh . "\n\n";
- if(!emptyempty($output)){
- printf("%-5s\t%-15s\t%-40s\t%-40s\t%-5s\n", '序號', '賽事類型', '主隊', '客隊', '讓球值');
- foreach($output as $key => $row){
- $format = "%-5d\t%-15s\t%-40s\t%-40s\t%-5s\n";
- printf($format, $key, $row['type'], $row['master'], $row['guest'], $row['rq']);
- }
- }
phpexcel導出excel數(shù)據(jù)
在服務器端生成靜態(tài)文件,相比直接生成,這兩種方法的主要區(qū)別是生成格式的不同,模板文件完全相同,下邊是一個在上例基礎(chǔ)上更改后的樣子,注意與上例的區(qū)別,代碼如下:
- <?php
- // 包含class的基本頭文件
- include("./class/class.php");
- // 生成excel的基本類定義(注意文件名的大小寫)
- include("./class/phpexcel/PHPExcel.php");
- // 包含寫Excel5格式的文件,如果需要生成excel2007的文件,包含對應的Writer即可
- include("./class/phpexcel/PHPExcel/Writer/Excel5.php");
- // 包含寫PDF格式文件
- include("./class/phpexcel/PHPExcel/Writer/PDF.php");
- // 創(chuàng)建phpexcel對象,此對象包含輸出的內(nèi)容及格式
- $m_objPHPExcel = new PHPExcel();
- // 模板文件,為了實現(xiàn)格式與內(nèi)容分離,有關(guān)輸出文件具體內(nèi)容實現(xiàn)在模板文件中
- // 模板文件將對象$m_objPHPExcel進行操作
- include("./include/excel.php"); //開源軟件:Vevb.com
- // 輸出文件的類型,excel或pdf
- $m_exportType = "pdf";
- $m_strOutputExcelFileName = date('Y-m-j_H_i_s').".xls"; // 輸出EXCEL文件名
- $m_strOutputPdfFileName = date('Y-m-j_H_i_s').".pdf"; // 輸出PDF文件名
- // 輸出文件保存路徑,此路徑必須可寫
- $m_strOutputPath = "./output/";
- // 如果需要輸出EXCEL格式
- if($m_exportType=="excel"){
- $objWriter = new PHPExcel_Writer_Excel5($m_objPHPExcel);
- $objWriter->save($m_strOutputPath.$m_strOutputExcelFileName);
- }
- // 如果需要輸出PDF格式
- if($m_exportType=="pdf"){
- $objWriter = new PHPExcel_Writer_PDF($m_objPHPExcel);
- $objWriter->save($m_strOutputPath.$m_strOutputPdfFileName);
- }
- ?>
新聞熱點
疑難解答