Blog

 Exportar a Excel desde php

Imprimir blog - 07-Julio-2011 - 17:53:24 

acvgalaicaUna tarea que seguro que a muchos de nosotros nos toca hacer en los back-end de aplicaciones ( tiendas online, gestion de empresas, ....)

La forma de hacerlo es bastante sencilla sin usar ninguna clase, basta con cambiar las cabeceras del fichero para que se interprete como un .xls.
Las cabeceras en cuestión son:

header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=fichero.xls");

Hasta aquí no tenemos ningún problema, ahora bien si queremos darle formato al contenido de las celdas ( moneda, texto, fecha, ... ) ahí tendremos que usar unas reglas que os dejo aquí:

mso-number-format:"0"NO Decimals
mso-number-format:"0.000"3 Decimals
mso-number-format:"#,##0.000"Comma with 3 dec
mso-number-format:"mm/dd/yy"Date7
mso-number-format:"mmmm d, yyyy"Date9
mso-number-format:"m/d/yy h:mm AM/PM"D -T AMPM
mso-number-format:"Short Date"01/03/1998
mso-number-format:"Medium Date"01-mar-98
mso-number-format:"d-mmm-yyyy"01-mar-1998
mso-number-format:"Short Time"5:16
mso-number-format:"Medium Time"5:16 am
mso-number-format:"Long Time"5:16:21:00
mso-number-format:"Percent"Percent - two decimals
mso-number-format:"0%"Percent - no decimals
mso-number-format:"0.E+00"Scientific Notation
mso-number-format:"@"Text
mso-number-format:"# ???/???"Fractions - up to 3 digits (312/943)
mso-number-format:"022£022#,##0.00"£12.76
mso-number-format:"#,##0.00_ ;[Red]-#,##0.00 "

2 decimals, negative numbers in red and signed



Estas reglas las añadimos a la propiedas style de cada celda para indicarle en caso necesario el formato a usar. Por defecto las celdas siempre van a ser texto.

Espero que os sirva.

     

In Internet Explorer versions up to 8, things inside the canvas are inaccessible!

Blog - acvgalaica
ACV Galaica