Skip to main content
Relanzada por el usuario Comunidad
Relanzada por el usuario Comunidad

public function excel(Request $request){

public function excel(Request $request){

        $validator=Validator::make($request->all(),[
            'title'=>'required',
            'items'=>'required'

        ]);
            if($validator->fails()){
                 return response()->json(['error'=>$validator->errors()->all()],404);
            }

         $data=$this->getControl($request->items);
         $keys=$data['keys'];
         $values=$data['values'];
             
       /*  $spreadsheet = new Spreadsheet();
         $sheet = $spreadsheet->getActiveSheet();
         $greenNotBold = array("font" => array("bold" => true,"size"=>12,),);*/
               //crear lector de office 
         $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx'); 
         $reader->setReadDataOnly(FALSE);
         $spreadsheet = $reader->load("../storage/test.xlsx");
         $spreadsheet->setActiveSheetIndex(0);
         $worksheet = $spreadsheet->getActiveSheet();  //obtiene la hoja de calculo

          $theme=$this->find('$Subject',$worksheet);
          $titleCoordinate=$this->find('$column',$worksheet);
          $valueCoordinate=$this->find('$values',$worksheet);
            
            
           if($theme){
             $greenNotBold = array("font" => array("bold" => true,"size" => 20,),);
             $column=$worksheet->getCell($theme)->getColumn();
             $worksheet->getColumnDimension($column)
             ->setAutoSize(false);
             //$worksheet->mergeCells($theme:$theme[0])->getAlignment()->setWrapText(true); 
             $worksheet->getStyle($theme)
                                  ->applyFromArray($greenNotBold);
                                  
                                 
             $worksheet->setCellValue($theme,$request->title); 
          
           }
             
          
           
        if($titleCoordinate!==null && $valueCoordinate!==null ){
            $column=$worksheet->getCell($titleCoordinate)->getColumn();
            $valuecol=$worksheet->getCell($valueCoordinate)->getColumn();
           ///  return response('sdf',200);
            $greenNotBold = array("font" => array("bold" => true,"size" => 12,),);
           $worksheet->fromArray($keys,NULL,$titleCoordinate);
           $worksheet->getStyle($titleCoordinate.':'.chr(ord($titleCoordinate)+sizeof($keys)).$worksheet->getCell($titleCoordinate)->getRow())
                          ->applyFromArray($greenNotBold);
           
          $worksheet->fromArray($values,NULL,$valueCoordinate);
          $writer=new Xlsx($spreadsheet);
          $writer->save('../storage/'.$request->title.'.xlsx');

        }
         
                 
            
         return response()->download('../storage/'.$request->title.'.xlsx','nameoffile.pdf')->deleteFileAfterSend(true);
    }

public function excel(Request $request){

    $validator=Validator::make($request->all(),[
        'title'=>'required',
        'items'=>'required'

    ]);
        if($validator->fails()){
             return response()->json(['error'=>$validator->errors()->all()],404);
        }

     $data=$this->getControl($request->items);
     $keys=$data['keys'];
     $values=$data['values'];
         
   /*  $spreadsheet = new Spreadsheet();
     $sheet = $spreadsheet->getActiveSheet();
     $greenNotBold = array("font" => array("bold" => true,"size"=>12,),);*/
           //crear lector de office 
     $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx'); 
     $reader->setReadDataOnly(FALSE);
     $spreadsheet = $reader->load("../storage/test.xlsx");
     $spreadsheet->setActiveSheetIndex(0);
     $worksheet = $spreadsheet->getActiveSheet();  //obtiene la hoja de calculo

      $theme=$this->find('$Subject',$worksheet);
      $titleCoordinate=$this->find('$column',$worksheet);
      $valueCoordinate=$this->find('$values',$worksheet);
        
        
       if($theme){
         $greenNotBold = array("font" => array("bold" => true,"size" => 20,),);
         $column=$worksheet->getCell($theme)->getColumn();
         $worksheet->getColumnDimension($column)
         ->setAutoSize(false);
         //$worksheet->mergeCells($theme:$theme[0])->getAlignment()->setWrapText(true); 
         $worksheet->getStyle($theme)
                              ->applyFromArray($greenNotBold);
                              
                             
         $worksheet->setCellValue($theme,$request->title); 
      
       }
         
      
       
    if($titleCoordinate!==null && $valueCoordinate!==null ){
        $column=$worksheet->getCell($titleCoordinate)->getColumn();
        $valuecol=$worksheet->getCell($valueCoordinate)->getColumn();
       ///  return response('sdf',200);
        $greenNotBold = array("font" => array("bold" => true,"size" => 12,),);
       $worksheet->fromArray($keys,NULL,$titleCoordinate);
       $worksheet->getStyle($titleCoordinate.':'.chr(ord($titleCoordinate)+sizeof($keys)).$worksheet->getCell($titleCoordinate)->getRow())
                      ->applyFromArray($greenNotBold);
       
      $worksheet->fromArray($values,NULL,$valueCoordinate);
      $writer=new Xlsx($spreadsheet);
      $writer->save('../storage/'.$request->title.'.xlsx');

    }
     
             
        
     return response()->download('../storage/'.$request->title.'.xlsx','nameoffile.pdf')->deleteFileAfterSend(true);
}
public function excel(Request $request){

        $validator=Validator::make($request->all(),[
            'title'=>'required',
            'items'=>'required'

        ]);
            if($validator->fails()){
                 return response()->json(['error'=>$validator->errors()->all()],404);
            }

         $data=$this->getControl($request->items);
         $keys=$data['keys'];
         $values=$data['values'];
             
       /*  $spreadsheet = new Spreadsheet();
         $sheet = $spreadsheet->getActiveSheet();
         $greenNotBold = array("font" => array("bold" => true,"size"=>12,),);*/
               //crear lector de office 
         $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx'); 
         $reader->setReadDataOnly(FALSE);
         $spreadsheet = $reader->load("../storage/test.xlsx");
         $spreadsheet->setActiveSheetIndex(0);
         $worksheet = $spreadsheet->getActiveSheet();  //obtiene la hoja de calculo

          $theme=$this->find('$Subject',$worksheet);
          $titleCoordinate=$this->find('$column',$worksheet);
          $valueCoordinate=$this->find('$values',$worksheet);
            
            
           if($theme){
             $greenNotBold = array("font" => array("bold" => true,"size" => 20,),);
             $column=$worksheet->getCell($theme)->getColumn();
             $worksheet->getColumnDimension($column)
             ->setAutoSize(false);
             //$worksheet->mergeCells($theme:$theme[0])->getAlignment()->setWrapText(true); 
             $worksheet->getStyle($theme)
                                  ->applyFromArray($greenNotBold);
                                  
                                 
             $worksheet->setCellValue($theme,$request->title); 
          
           }
             
          
           
        if($titleCoordinate!==null && $valueCoordinate!==null ){
            $column=$worksheet->getCell($titleCoordinate)->getColumn();
            $valuecol=$worksheet->getCell($valueCoordinate)->getColumn();
           ///  return response('sdf',200);
            $greenNotBold = array("font" => array("bold" => true,"size" => 12,),);
           $worksheet->fromArray($keys,NULL,$titleCoordinate);
           $worksheet->getStyle($titleCoordinate.':'.chr(ord($titleCoordinate)+sizeof($keys)).$worksheet->getCell($titleCoordinate)->getRow())
                          ->applyFromArray($greenNotBold);
           
          $worksheet->fromArray($values,NULL,$valueCoordinate);
          $writer=new Xlsx($spreadsheet);
          $writer->save('../storage/'.$request->title.'.xlsx');

        }
         
                 
            
         return response()->download('../storage/'.$request->title.'.xlsx','nameoffile.pdf')->deleteFileAfterSend(true);
    }
Origen Enlace

Descargar archivos usando rest api laravel en Postman

estoy diseñando un pequeño servicio de exportación de archivos en laravel usando phpoffice y dompdf. Mi implementación genera los archivos, pero cuando lo pruebo en postman para ver como funciona el mecanismo de descarga me sale esto:

Exportar archivos en postman

En vez de ocurrir un evento de descarga como si estuviera navegando solo me sale la vista previa del archivo. No entiendo por que?

Mi código:

public function excel(Request $request){

    $validator=Validator::make($request->all(),[
        'title'=>'required',
        'items'=>'required'

    ]);
        if($validator->fails()){
             return response()->json(['error'=>$validator->errors()->all()],404);
        }

     $data=$this->getControl($request->items);
     $keys=$data['keys'];
     $values=$data['values'];
         
   /*  $spreadsheet = new Spreadsheet();
     $sheet = $spreadsheet->getActiveSheet();
     $greenNotBold = array("font" => array("bold" => true,"size"=>12,),);*/
           //crear lector de office 
     $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx'); 
     $reader->setReadDataOnly(FALSE);
     $spreadsheet = $reader->load("../storage/test.xlsx");
     $spreadsheet->setActiveSheetIndex(0);
     $worksheet = $spreadsheet->getActiveSheet();  //obtiene la hoja de calculo

      $theme=$this->find('$Subject',$worksheet);
      $titleCoordinate=$this->find('$column',$worksheet);
      $valueCoordinate=$this->find('$values',$worksheet);
        
        
       if($theme){
         $greenNotBold = array("font" => array("bold" => true,"size" => 20,),);
         $column=$worksheet->getCell($theme)->getColumn();
         $worksheet->getColumnDimension($column)
         ->setAutoSize(false);
         //$worksheet->mergeCells($theme:$theme[0])->getAlignment()->setWrapText(true); 
         $worksheet->getStyle($theme)
                              ->applyFromArray($greenNotBold);
                              
                             
         $worksheet->setCellValue($theme,$request->title); 
      
       }
         
      
       
    if($titleCoordinate!==null && $valueCoordinate!==null ){
        $column=$worksheet->getCell($titleCoordinate)->getColumn();
        $valuecol=$worksheet->getCell($valueCoordinate)->getColumn();
       ///  return response('sdf',200);
        $greenNotBold = array("font" => array("bold" => true,"size" => 12,),);
       $worksheet->fromArray($keys,NULL,$titleCoordinate);
       $worksheet->getStyle($titleCoordinate.':'.chr(ord($titleCoordinate)+sizeof($keys)).$worksheet->getCell($titleCoordinate)->getRow())
                      ->applyFromArray($greenNotBold);
       
      $worksheet->fromArray($values,NULL,$valueCoordinate);
      $writer=new Xlsx($spreadsheet);
      $writer->save('../storage/'.$request->title.'.xlsx');

    }
     
             
        
     return response()->download('../storage/'.$request->title.'.xlsx','nameoffile.pdf')->deleteFileAfterSend(true);
}