• PHP导出Excel的功能实现
  • 发布于 2个月前
  • 505 热度
    0 评论
  • 远行客
  • 0 粉丝 45 篇博客
  •   

最近在帮用户做一个二手房租赁管理的财务系统,里面需要涉及到大量的财务统计报表需要导出Excel。对于PHP初学者的我来说,PHP导出Excel的功能还是第一次接触。网上找PHP导出Excel的代码研究了半天,终于捣鼓出来了。o(╯□╰)o,我的PHP导出Excel的功能代码如下:

PHP导出Excel代码:

<?php
/**
 * Created by PhpStorm.
 * 堆代码 duidaima.com
 * Time: 11:32
 */
//include_once "./Classes/PHPExcel.php";
 
class CsvReader {
    private $csv_file;
    private $spl_object = null;
    private $error;
 
    public function __construct($csv_file = '') {
        /*判断变量是否存在  &&  文件是否存在*/
        if($csv_file && file_exists($csv_file)) {
            $this->csv_file = $csv_file;
        }
    }
 
    public function set_csv_file($csv_file) {
        if(!$csv_file || !file_exists($csv_file)) {
            $this->error = 'File invalid';
            return false;
        }
        $this->csv_file = $csv_file;
        $this->spl_object = null;
    }
 
    public function get_csv_file() {
        return $this->csv_file;
    }
    /*判断文件是否存在  是否可读*/
    private function _file_valid($file = '') {
        $file = $file ? $file : $this->csv_file;
        if(!$file || !file_exists($file)) {
            return false;
        }
        if(!is_readable($file)) {
            return false;
        }
        return true;
    }
 
    private function _open_file() {
        if(!$this->_file_valid()) {
            $this->error = 'File invalid';
            return false;
        }
        if($this->spl_object == null) {
            $this->spl_object = new SplFileObject($this->csv_file, 'rb');
        }
        return true;
    }
 
    public function get_data($length = 0, $start = 0) {
        if(!$this->_open_file()) {
            return false;
        }
        $length = $length ? $length : $this->get_lines();
        $start = $start - 1;
        $start = ($start < 0) ? 0 : $start;
        $data = array();
        $this->spl_object->seek($start);
        while ($length-- && !$this->spl_object->eof()) {
            $data[] = $this->spl_object->fgetcsv();
            $this->spl_object->next();
        }
        return $data;
    }
 
    public function get_lines() {
        if(!$this->_open_file()) {
            return false;
        }
        $this->spl_object->seek(filesize($this->csv_file));
        return $this->spl_object->key();
    }
 
    public function get_error() {
        return $this->error;
    }
}//获取文件
$csv_file="./demo.csv";
 
$csvreader = new CsvReader($csv_file);
//获得csv文件有多少行
$line_number = $csvreader->get_lines();
//获得csv文件的数据
$data = $csvreader->get_data($line_number);//链接数据库
$link=mysqli_connect('127.0.0.1','root','','husky');
$facedata=[];
foreach ($data as $k=>$da){
        if($k>0){
 
            //产品地址
            $facedata['ids']=$da[0];
            $facedata['title']=$da[6];
            $facedata['description']=$da[7];
            $facedata['ava']="in stock";
            $facedata['condition']="new";
            $facedata['shipping_weight']=$da[9];
            $facedata['price']=$da[14]." USD";
            var_dump($da);
            $string=$da[46];
            $faceda=explode(',',$string);
 
            foreach ($faceda as $k1=>$v1){
                $faceda[$k1]=explode('=',$v1);
 
            }
            $facedata['color']=$faceda[2][1];
            $facedata['google_product_category']=str_replace("/",">",$da[4]);
            $facedata['material']=$faceda[1][1];
            var_dump($faceda);
            $facedata['link']="https://www.duidaima.com/".$da[17].".html";
            $facedata['image_link']="https://www.duidaima.com/".$da[21];
            $facedata['brand']=$faceda[4][1];
 
            $str='';
            foreach ($facedata as $fk=>$face){
                $str .= '`'.$fk.'`' .'='. '"'.$face.'"' .',';
            }
 
            $str=substr($str, 0, -1);
 
//            $des="http://www.duidaima.com/pub/media/catalog/product".$da[21];
            $sql='insert  into faceboolproduct set '.$str;
 
            $res=mysqli_query($link,$sql);
            var_dump($res);
            echo "<br />";
 
        }}
总结:

以上就是我基于PHP语言的导出Excel功能代码,对于很多老手来说导出Excel这种功能应该是手到擒来,但是对于我这种PHP菜鸟来说,第一次接触到这种需求,还是需要研究一下才能做出来。以后还是要多多学习啊。

用户评论