PHPSpreadSheet library with Scriptcase

PHPSpreadSheet library with Scriptcase

1- We download the external library PHPSpreadSheet without composer https://php-download.com/downloads/phpoffice/phpspreadsheet/1.8.2.0/phpoffice_phpspreadsheet_1.8.2.0_require.zip


2- Then we proceed to load the external library in the scriptcase: https://www.youtube.com/watch?v=nPiAVJZBbq8 we can be guided by this video if we still do not know how to use the external libraries in scriptcase


3- In our case the library that we are going to use is called PHP


4- If we enter it we can see our loaded folders

5- Then we proceed to use our library in a blank application

6- In the case of calls from the classes to be used from the library, the scriptcase uses as follows


- If the class is in \ PhpOffice \ PhpSpreadsheet \ src \ Spreadsheet.php then to use in the scriptcase you must perform \ PhpOffice \ PhpSpreadsheet \ Spreadsheet ();

Sample code to use the SpreadSheet

includesc_url_library("prj","PHP","vendor/autoload.php");


$documento= new \PhpOffice\PhpSpreadsheet\Spreadsheet();

$documento

   ->getProperties()

   ->setCreator("Prueba")

   ->setLastModifiedBy('JuanAquino') // última vez modificado por

    ->setTitle('Miprimer documento creado con PhpSpreadSheet')

    ->setSubject('Elasunto')

   ->setDescription('Estedocumento fue generado para prueba')

   ->setKeywords('etiquetas opalabras clave separadas por espacios')

   ->setCategory('Lacategoría');

 

$nombreDelDocumento= "Mi primer archivo.xlsx";

 

header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

header('Content-Disposition:attachment;filename="' . $nombreDelDocumento . '"');

header('Cache-Control:max-age=0');

 

#Como ya hay una hoja por defecto, la obtenemos, no la creamos

$hojaDeProductos= $documento->getActiveSheet();

$hojaDeProductos->setTitle("Productos");

#Escribir encabezado de los productos

//$encabezado=["id","nombre","apellido","edad","nacionalidad","profesion","sexo","nacimiento"];

$encabezado=["id","nombre","apellido","edad","sexo","fechaNacimiento0","nacionalidad"];

#El último argumento es por defecto A1 pero lo pongo para que seexplique mejor

$hojaDeProductos->fromArray($encabezado,null, "A1");


//$consulta= "SELECT id, nombre, apellido, edad, nacionalidad, profesion,sexo, nacimiento FROM dbo.tabla2";

$consulta= "SELECT id, nombre, apellido, edad, sexo, fechaNacimiento0,nacionalidad FROM dbo.prueba";


sc_select(ds,$consulta);

//var_dump({ds});


#Comenzamos en la 2 porque la 1 es del encabezado

$numeroDeFila= 2;


$producto= {ds};

foreach($productoas $key => $valor)

{

$id= $valor['id'];

$nombre = $valor['nombre'];

$apellido = $valor['apellido'];

$edad = $valor['edad'];

$nacionalidad= $valor['nacionalidad'];

$profesion= $valor['profesion'];

$sexo = $valor['sexo'];

$fechaNacimiento = $valor['nacimiento'];

#Escribirlos en el documento

$hojaDeProductos->setCellValueByColumnAndRow(1,$numeroDeFila, $id);

$hojaDeProductos->setCellValueByColumnAndRow(2,$numeroDeFila, $nombre);

$hojaDeProductos->setCellValueByColumnAndRow(3,$numeroDeFila, $apellido);

$hojaDeProductos->setCellValueByColumnAndRow(4,$numeroDeFila, $edad);

$hojaDeProductos->setCellValueByColumnAndRow(5,$numeroDeFila, $nacionalidad);

$hojaDeProductos->setCellValueByColumnAndRow(6,$numeroDeFila, $profesion);

$hojaDeProductos->setCellValueByColumnAndRow(7,$numeroDeFila, $sexo);

$hojaDeProductos->setCellValueByColumnAndRow(8,$numeroDeFila, $fechaNacimiento);

$numeroDeFila++;

}

$writer= PhpOffice\PhpSpreadsheet\IOFactory::createWriter($documento,'Xlsx');

$writer->save('php://output');

exit;

    • Related Articles

    • Creating and Using a library in Scriptcase

      We'll take a step-by-step look at how to create and make use of a library in Scriptcase. 1. To create an library go to: Tools > Internal Libraries 2. In this first window you should select what level you want to create/upload your library.   3. You ...
    • GoJS Library is no longer available

      GoJS library is no longer available for native use in Scriptcase.  We have purchased a GoJS O&M license for Scriptcase 9 and, by our understanding, we could distribute GoJS to any number of end users, however we were recently contacted by Northwoods ...
    • Request a new license

      Some situations require the reinstallation of Scriptcase, in these cases, the user must generate a new serial on our website to register the tool. Follow the steps below to generate a new serial:  1. Log in to the website The entire process to ...
    • Notifications in Scriptcase

      1- We proceed to download the external push notification library https://codeload.github.com/Nickersoft/push.js/zip/master 2- We load the external library in our scriptcase, we can follow this tutorial if necessary: ...
    • Example animation inside scriptcase

      This is an example of how to place animation on the form sheet, 1 - We go to the page of https://github.com/animate-css/animate.css to download the .zip of the animations. 2 - Downloading the .zip we proceed to load the downloaded file as an external ...