[phpexcel导出与读取excel]phpexcel导出与读取excel的经典实例

更新时间:2019-08-17    来源:excel    手机版     字体:

【www.bbyears.com--excel】

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(empty($xh))break;
       
        $tmpType = (string)$currentSheet->getCell("C".$currentRow)->getValue(); //赛事类型
        if(!empty($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(!empty($xh))break;
    }
   
    for( ; $currentRow <= $allRow; $currentRow++){
        $xh = (int)$currentSheet->getCell("B".$currentRow)->getValue();
        if(empty($xh))break;
       
        $output[$xh]["rq"] = $currentSheet->getCell("I".$currentRow)->getValue();
    }
    header("content-type:text/html; charset=utf-8");
   
    echo "期号:" . $qh . "\n\n";
    if(!empty($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数据

在服务器端生成静态文件

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

 代码如下

// 包含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");

// 输出文件的类型,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); 
}
?>

本文来源:http://www.bbyears.com/bangongshuma/62682.html

热门标签

更多>>

本类排行