加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

phpexcel导出与读取excel的经典例子

发布时间:2023-09-13 14:02:02 所属栏目:PHP教程 来源:
导读:phpexcel可以让开发者方便快捷的来操作xls文件了,我们下文来整理几个关于phpexcel对数据导入与导出例子.

PHPExcel读取excel文件,实例代码如下:

require_once('include/common.inc.php');
require
phpexcel可以让开发者方便快捷的来操作xls文件了,我们下文来整理几个关于phpexcel对数据导入与导出例子.

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(); //行数 
   $output = array(); 
   $preType = ''; 
    
   $qh = $currentSheet->getCell('A4')->getValue(); 
   //按照文件格式从第7行开始循环读取数据 
   for($currentRow = 7;$currentRow<=$allRow;$currentRow++){  
       //判断每一行的B列是否为有效的序号,如果为空或者小于之前的序号则结束 
       $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(); //客队     
   } 
    
   //从当前行开始往下循环,取出第一个不为空的行 
   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数据

在服务器端生成静态文件,相比直接生成,这两种方法的主要区别是生成格式的不同,模板文件完全相同,下边是一个在上例基础上更改后的样子,注意与上例的区别,代码如下:

<?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"); 
// 创建phpexcel对象,此对象包含输出的内容及格式 
$m_objPHPExcel = new PHPExcel(); 
// 模板文件,为了实现格式与内容分离,有关输出文件具体内容实现在模板文件中 
// 模板文件将对象$m_objPHPExcel进行操作 
include("./include/excel.php"); //开源软件:Cuoxin.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);   

?>

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章