Documentos de Académico
Documentos de Profesional
Documentos de Cultura
PHP Excel
PHP Excel
Creo que el ttulo lo dice todo. Esta librera permite leer y manipular el contenido de una hoja de clculo hecha con Microsoft Excel. Vamos directo al grano y veamos un ejemplo. Usaremos Microsoft Office Excel 2007 para la hoja de clculo, sea el archivo tendr la extensin xlsx. Tambin usaremos la versin 5.2.x de PHP.
<?php /** Incluir la ruta **/ set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/'); /** Clases necesarias */ require_once('PHPExcel.php'); require_once('PHPExcel/Reader/Excel2007.php'); // Variables de la pgina $_VIEWDATA = array( 'v_precioTotal' => 0, 'v_descuento' => 0, 'v_precioFinal' => 0 ); // Peticin de clculo? if (isset($_REQUEST['boton_calcular'])) { // Cargando la hoja de clculo $objReader = new PHPExcel_Reader_Excel2007(); $objPHPExcel = $objReader->load("calculo.xlsx"); // Asignar hoja de calculo activa $objPHPExcel->setActiveSheetIndex(0); // Asignar data $objPHPExcel->getActiveSheet()->setCellValue('automatico', $_REQUEST['transmision_Automatica']); $objPHPExcel->getActiveSheet()->setCellValue('cuero', $_REQUEST['asientos_Cuero']); $objPHPExcel->getActiveSheet()->setCellValue('suspension', $_REQUEST['suspension']); // Calculos $_VIEWDATA['v_precioTotal'] = $objPHPExcel->getActiveSheet()>getCell('total')->getCalculatedValue(); $_VIEWDATA['v_descuento'] = $objPHPExcel->getActiveSheet()>getCell('descuento')->getCalculatedValue(); $_VIEWDATA['v_precioFinal'] = $objPHPExcel->getActiveSheet()>getCell('final')->getCalculatedValue(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-88591" /> <title>Ejemplo</title> </head> <body> <form id="formulario" method="post" name="formulario" action="index.php"> <table> <tr> <th>Transmisión automática :</th> <td> <select id="transmision_Automatica" name="transmision_Automatica"> <?php
if(isset($_REQUEST['transmision_Automatica'])) { ?> <option value="<?php echo $_REQUEST['transmision_Automatica']; ?>" selected="selected"><?php echo $_REQUEST['transmision_Automatica']; ?></option> <?php } ?> <option value="No">No</option> <option value="Si">Si</option> </select> </td> </tr> <tr> <th>Asientos de cuero:</th> <td> <select id="asientos_Cuero" name="asientos_Cuero"> <?php if(isset($_REQUEST['asientos_Cuero'])) { ?> <option value="<?php echo $_REQUEST['asientos_Cuero']; ?>" selected="selected"><?php echo $_REQUEST['asientos_Cuero']; ?></option> <?php } ?> <option value="No">No</option> <option value="Si">Si</option> </select> </td> </tr> <tr> <th>Suspensión:</th> <td> <select id="suspension" name="suspension"> <?php if(isset($_REQUEST['suspension'])) { ?> <option value="<?php echo $_REQUEST['suspension']; ?>" selected="selected"><?php echo $_REQUEST['suspension']; ?></option> <?php } ?> <option value="No">No</option> <option value="Si">Si</option> </select> </td> </tr> <tr> <th> </th> <td> <input id="boton_calcular" name="boton_calcular" type="submit" value="Calcular" /> </td> </tr> </table> </form> <?php if (isset($_REQUEST['boton_calcular'])) { ?> <h2>Detalles del Precio</h2> <p>Basado en tus preferencias, el precio de tu carro ser S/. <?php echo number_format($_VIEWDATA['v_precioFinal'], 2); ?> Nuevos Soles.</p> <table> <tr> <th>Precio Total:</th> <td><?php echo number_format($_VIEWDATA['v_precioTotal'], 2); ?> Nuevos Soles</td> </tr> <tr>
<th>Descuento:</th> <td><?php echo number_format($_VIEWDATA['v_descuento'] * 100, 2); ?>%</td> </tr> <tr> <td colspan="2"><hr noshade="noshade"></hr> </tr> <tr> <th>Total Final:</th> <td><?php echo number_format($_VIEWDATA['v_precioFinal'], 2); ?> Nuevos Soles</td> </tr> </table> <p><a href="index.php">Calcular nuevo precio</a></p> <?php } ?> </body> </html>
4. A probar ...!
Vamos al navegador web y escribirmos http://localhost/phpexcel_ejemplo/index.php y tendremos una interfaz con los datos que obtenemos de la hoja de clculo. Seguramente se nos ocurrir muchas cosas en la cual emplear est interesante librera. Y que tal si la combinamos con AJAX?. La librera PHPExcel contiene documentacin detallada para usarla correctamente, adems de muchos otros ejemplos. c