douxing8855 2017-04-05 09:10
浏览 40
已采纳

从PHP导入/导出文件

I think I wasn't clean in my explanation when I asked my question (the problem). I have a project PHP, I don't have a local environnement, I'm working on-line: I change my file via Netbeans and I deploy it without tested it locally. My server is of production.My Database (MySQL - phpMyadmin ) in the sever also. I import the data from my disk into the project. Then PHP exploit it via HighChart. The user can see these data per many choices (Date, city, Customer,..) and finally he can download this final result into file csv. Here the problem I can't download all the data, for example: in The Turnover display in the Chart 14Million but in the file csv it display a smaller number of turnover.

this following my code:

class Export{
    private $_dataIN=[];
    private $_author;
    private $_filepath;

    public function __construct(array $dataIN) {
        $this->_dataIN = $dataIN;
        $this->setFilepath();
        $this->prepareFile();
    }
    public function setFilepath() {
        $date = new \DateTime();
        $this->_filepath='../DATA/ExportFiles/ExportData'.$date->getTimestamp();
    }
    public function prepareFile(){
        $file = fopen($this->_filepath, 'w');
        fputcsv($file, array('Piece', 'Client', 'Libelle Client', 'Facturation','OTP','Typcde','Cdecli','Cdesap','Article','Designation','Dev','Montant_fac_dev_ht','Montant_fac_eur','Avion','Nature','Repsite','Zone','LRU','Contract'),';');        
        foreach ($this->_dataIN as $key => $statement) {
            fputcsv($file, array($statement->getId(), $statement->getCustomerId(), $statement->getCustomerName(), $statement->getDate(), $statement->getOTP(), $statement->getCdetype(), $statement->getCdecustomer(), $statement->getSAPcode(), $statement->getArticle(), $statement->getDesigniation(), $statement->getCurrency(), $statement->getAmount(), $statement->getAmountEur(), $statement->getAircraft(), $statement->getNature(), $statement->getRepsite(), $statement->getZone(), $statement->getLru(),$statement->getContract()),';');
        }
    }

    public  function download() {        

        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//octet-stream' 
        header('Content-Disposition: attachment; filename='.basename($this->_filepath).'.csv'); 
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($this->_filepath) . 'Giga'); //Kilo
        readfile($this->_filepath);
        exit();

I hope that this time my explanation is clean and you can help?

  • 写回答

1条回答 默认 最新

  • dream_wu2015 2017-04-05 09:16
    关注

    Better to use like this without storing:

    public function __construct($dataIN = array()) {
        $this->_dataIN = $dataIN;
        $this->prepareFile();
    }
    
    public function prepareFile(){
    
        $header[] = array('Piece', 'Client', 'Libelle Client', 'Facturation','OTP','Typcde','Cdecli','Cdesap','Article','Designation','Dev','Montant_fac_dev_ht','Montant_fac_eur','Avion','Nature','Repsite','Zone','LRU','Contract');        
    
        $data = array();
    
        foreach ($this->_dataIN as $key => $statement) {
            $data[] = array($statement->getId(), $statement->getCustomerId(), $statement->getCustomerName(), $statement->getDate(), $statement->getOTP(), $statement->getCdetype(), $statement->getCdecustomer(), $statement->getSAPcode(), $statement->getArticle(), $statement->getDesigniation(), $statement->getCurrency(), $statement->getAmount(), $statement->getAmountEur(), $statement->getAircraft(), $statement->getNature(), $statement->getRepsite(), $statement->getZone(), $statement->getLru(),$statement->getContract());
        }
    
        $final_data = array_merge($header, $data);
    
        $this->array_to_csv($final_data, 'report.csv');
    }
    
    
    function array_to_csv($array, $download = "")
    {   
        if ($download != "")
        {    
            header('Content-Description: File Transfer');
            header("Content-type: application/vnd.ms-excel");
            header('Content-Disposition: attachement; filename="' . $download . '"');
            header('Content-Transfer-Encoding: binary');
        }        
    
        ob_start();
        $f = fopen('php://output', 'w') or show_error("Can't open php://output");
        $n = 0;        
        foreach ($array as $line)
        {
            $n++;
            if ( ! fputcsv($f, $line))
            {
                show_error("Can't write line $n: $line");
            }
        }
        fclose($f) or show_error("Can't close php://output");
        $str = ob_get_contents();
        ob_end_clean();
    
        if ($download == "")
        {
            return $str;    
        }
        else
        {    
            print "\xEF\xBB\xBF"; // UTF-8 BOM
            print $str;
        }        
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 配置FPT报错,该如何处理
  • ¥15 请大家看一下这个代码咋写,一点思路都没有,最好能做一下,不要伪代码,有偿
  • ¥15 有偿请人帮写个安卓系统下禁止装软件及禁止拷入文件的程序
  • ¥100 用 H.265 对音视频硬编码 (CUDA)
  • ¥20 mpich安装完成后出问题
  • ¥15 stm32循迹小车代码问题
  • ¥15 输入一堆单词,使其去重输出
  • ¥15 qc代码,修改和添加东西
  • ¥50 Unity的粒子系统使用shadergraph(内置管线)制作的一个顶点偏移shader,但是粒子模型移动时,顶点也会偏移
  • ¥15 如何用python处理excel的数据(极值标准化)