Wednesday, September 10, 2008

excel html descarga

Tuve el problema al descargar archivos Excel, desde php...

descubrí que es suficiente hacer una tabla, y descargalo como un archivo xls:

un .php con lo siguiente:

$strExcel = ob_get_clean();

$filename = uniqid('').".xls";
$path = "temp/";
file_put_contents($path.$filename,$strExcel);

// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache

// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=".$filename.";");

/*
The Content-transfer-encoding header should be binary, since the file will be read
directly from the disk and the raw bytes passed to the downloading computer.
The Content-length header is useful to set for downloads. The browser will be able to
show a progress meter as a file downloads. The content-lenght can be determines by
filesize function returns the size of a file.
*/
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($path.$filename));

@readfile($path.$filename);

además para el problema de descargar números como texto y demás formatos:

excel reconoce css, y se pueden utilizar formulas, comandos.

http://agoric.com/sources/software/htmltoExcel:
The solution? The mso-number-format style attribute, to be put on table cells (). Several number formats are available. These are some of the more common:
mso-number-format:\@
text
mso-number-format:"0\.000"
3 decimals
mso-number-format:\#\,\#\#0\.000
comma separators (and 3 decimals)
mso-number-format:"mm\/dd\/yy"
Date format
mso-number-format:"d\\-mmm\\-yyyy"
another date format
mso-number-format:Percent
percent

'mso' stands for Microsoft Office, so these formatting hints will hold if table data is imported into any Office product.

No comments:

Post a Comment