【问题标题】:Sending Variables to Word Document Laravel将变量发送到 Word 文档 Laravel
【发布时间】:2017-03-10 01:06:28
【问题描述】:

有任何方法可以将变量发送到 word 文档,重命名并使其可从 laravel 中的应用程序下载。

【问题讨论】:

    标签: laravel laravel-5 phpdoc


    【解决方案1】:

    很好地使用 PHPOffice/PHPWord

    你可以在 laravel 5.2 中做类似的事情:

      public function descargardoc($id) 
    {
      // New Word document
         setlocale(LC_TIME, 'es'); 
         //return $id;
        $data = $this->getData($id);
        $status = $this->getStatus($id);
        $date = date('Y-m-d');
        $fecha_documento = $this->getFecha($data);
        $fecha_ingreso = $this->getFechaIngreso($data); 
        $fecha_inicio_reduc  = $this->getFechaReducJor($data); 
    
        echo date('H:i:s') , " Create new PhpWord object" , PHP_EOL;
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
    
        $document = $phpWord->loadTemplate('doc/reduccion_de_jornada.docx');
    
        //Mapeo de Variables 
    
        $document->setValue('ciudad', ($data['ciudad']) );   
        $document->setValue('fecha_documento', $fecha_documento );
        $document->setValue('nombre_persona', ($data['nombre_persona']) );
        $document->setValue('apellido1', ($data['apellido1']) ); 
        $document->setValue('apellido2', ($data['apellido2']) );
        $document->setValue('dni', ($data['dni']) );
        $document->setValue('domicilio', ($data['domicilio']) );
        $document->setValue('calle', ($data['calle']) ); 
        $document->setValue('nrocalle', ($data['nrocalle']) );
        $document->setValue('piso', ($data['piso']) );
        $document->setValue('puerta', ($data['puerta']) );
        $document->setValue('codpostal', ($data['codpostal']) );
        $document->setValue('ciudad', ($data['ciudad']) );
        $document->setValue('nombre_empresa', ($data['nombre_empresa']) );
        $document->setValue('fecha_ingreso',  $fecha_ingreso );
        $document->setValue('puesto_trabajo', ($data['puesto_trabajo']) );
        $document->setValue('categoria_profesional', ($data['categoria_profesional']) );
        $document->setValue('porcentaje_reduccion', ($data['porcentaje_reduccion']) );
        $document->setValue('razon_reduccion', ($data['razon_reduccion']) ); 
        $document->setValue('ini_horario_reduc', ($data['ini_horario_reduc']) ); 
        $document->setValue('fin_horario_reduc', ($data['fin_horario_reduc']) ); 
        $document->setValue('fecha_inicio_reduc', $fecha_inicio_reduc  ); 
        $document->setValue('fin_dia_reduc', ($data['fin_dia_reduc']) );
        $document->setValue('preaviso', ($data['preaviso']) );
    
    
    
        $name = 'Doc'."$id".'-'.'ReduJornada'."$date".'.docx';
        echo date('H:i:s'), " Write to Word2007 format", PHP_EOL;
        $document->saveAs($name);
        rename($name, storage_path()."/word/{$name}");
        $file= storage_path(). "/word/{$name}";
    
        //$file= storage_path(). "/word/{$name}";
    
        $headers = array(
              //'Content-Type: application/msword',
              'Content-Type: vnd.openxmlformats-officedocument.wordprocessingml.document'
            );
    
        $response = Response::download($file, $name, $headers);
        ob_end_clean();
    
        return $response;
    
    
    
    }
    

    然后在你的模板文件中你必须以这种方式声明你的变量:

    En ${ciudad}。 ${fecha_documento}

    【讨论】:

      【解决方案2】:

      是的。 PHPOffice/PHPWord

      一个用于读写文字处理文档的纯PHP库

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-04
        相关资源
        最近更新 更多