PHPExcel 导入、导出

HUA
HUA
2018-05-27 / 0 评论 / 1,493 阅读 / 正在检测是否收录...

PHPExcel类是php一个excel表格处理插件了,下面我来给大家介绍利用PHPExcel类来导入与导出excel表格 的应用方法,有需要了解的朋友不防参考参考(PHPExcel自己百度下载这里不介绍了)。

<?php
/**
 * @name PHP Excel
 */
require_once ('/lib/PHPExcel/PHPExcel.php');
error_reporting(0);
class Excel{

    //转为数组
       //$file excel 文件地址
    public function toArray($file,$sheet=null){
        $objPHPExcel = PHPExcel_IOFactory::load($file);
        if($sheet){
            return $objPHPExcel->getSheetByName($sheet)->toArray();
        }
        else{
            return $objPHPExcel->getActiveSheet()->toArray();
        }
    }

    //导出
       //$filename 文件名
       //$title 标签名
       // $data  需要导出的数据
    public function download($filename,$title,$data){
        // Create new PHPExcel object
        $objPHPExcel = new PHPExcel();

        // Set document properties
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
        ->setLastModifiedBy("Maarten Balliauw")
        ->setTitle("Office 2007 XLSX Test Document")
        ->setSubject("Office 2007 XLSX Test Document")
        ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
        ->setKeywords("office 2007 openxml php");

        // Add some data
        foreach($data as $k=>$v){
            $k2 = 0;
            foreach($v as $vv){
                $objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($k2,$k+1,$vv);
                $k2++;
            }
        }

        $objPHPExcel->getActiveSheet()->setTitle($title);
        
        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);
        
        ob_end_clean();
        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
        
        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
}

以上便是PHPexcel 应用实例,更多功能需进一步探索。

0

评论 (0)

取消