Respuesta: Problema excel con php Este es el case 1.....son 7 muy similares
switch ( $_REQUEST['reporte_seleccionado'] ) {
case 1:
$fecha = $_REQUEST['fecha_retiro'];
$muestreos = Buscar_muestreos($fecha,$server,$user,$pass,$base) ;
//print_a($muestreos);
$matriz = array(
array(
'No',
'Cliente',
'Obra',
'Constructora',
'Direccion',
'Comuna',
'Hormigon',
'Inicio',
'Fin',
'Fre',
'Vol',
'Tipo/Cant',
'Pri',
'N.P.',
'C.Obra',
'Observaciones'
)
);
foreach( $muestreos as $id => $m ) {
if( $m['MUE_MUE_ESTADO'] != 'Pendiente' ) {
unset( $muestreos[$id] );
continue;
}
$o = $m['MUE_OBR_ID'];
$codigo_obra = Buscar_codigo_obra( $m['MUE_CLI_ID'],$o,$server,$user,$pass,$base );
$datos_obras_clientes = Buscar_registro_obras_clientes($m['MUE_CLI_ID'],$codigo_obra,$server,$user,$pass,$base);
$obra = Buscar_datos_obra( $o,$server,$user,$pass,$base );
$datos_cliente = Buscar_cliente_por_id($m['MUE_CLI_ID'],$server,$user,$pass,$base);
$cliente = $datos_cliente['CLI_NOMBRE'];
$contratista = $datos_obras_clientes['O_CLI_CONTRATISTA'];
//print_a($obra);
$matriz[] = array(
$m['MUE_MUE_NUMERO_DIARIO'],
$cliente,
$obra['NOMBRE'],
$contratista,
$obra['DIRECCION'],
$obra['COMUNA'],
$m['MUE_HOR_TIPO']?$m['MUE_HOR_TIPO']:' ',
$m['MUE_MUE_HORA_INI'],
$m['MUE_MUE_HORA_FIN'],
$m['MUE_MUE_FRECUENCIA'],
$m['MUE_HOR_VOLUMEN'],
$m['MUE_HOR_PROBETA_TIPO']." / ".$m['MUE_HOR_PROBETA_CANTIDAD'],
$m['MUE_MUE_PRIORIDAD'],
$m['MUE_NUM_PEDIDO'],
$obras[$o]['codigo'],
$m['MUE_MUE_OBSERVACIONES']?$m['MUE_MUE_OBSERVACIONES']:' '
);
}
streamTable( array( "Plantilla de Muestreos" => $matriz ), 'plantilla_de_muestreos_'.$fecha );
FUNCIONES:
function streamTable( $hojas, $filename = FALSE, $disposition = 'inline', $debug = FALSE ) {
session_write_close();
error_reporting( $debug ? E_ALL ^ E_NOTICE : 0 );
$excel = doExcel( $hojas );
if( ! $filename ) $filename = md5( uniqid( rand(), true ) );
if( $debug ) {
print '<pre>'.htmlentities( implode( "\n", $excel ) ).'</pre>';
} else {
header( 'Content-Type: application/vnd.ms-excel; charset=UTF-8' );
header( 'Content-Disposition: '.$disposition.'; filename="'.$filename.'.xls"' );
print implode( "\n", $excel );
}
exit;
}
function doExcel( $hojas, $filename = '' ){
//require_once( 'RAIDManager.php' );
reset( $hojas );
list( , $p ) = each( $hojas ); list( , $p ) = each( $p );
if( ! is_array( $p ) ) $hojas = array( '' => $hojas );
$buffer = array();
$buffer[] = '<'.'?xml version="1.0" encoding="'.iconv_get_encoding( 'output_encoding' ).'"?'.'>';
$buffer[] = '<'.'?mso-application progid="Excel.Sheet"?'.'>';
$buffer[] = '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet">';
$buffer[] = '<Styles>';
$buffer[] = '<Style ss:ID="Default1" ss:Name="Normal">';
$buffer[] = '<Font ss:Size="8"/>';
$buffer[] = '<ss:Borders><ss:Border ss:Position="Top" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '<ss:Border ss:Position="Left" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '<ss:Border ss:Position="Bottom" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '<ss:Border ss:Position="Right" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '</ss:Borders></Style>';
$buffer[] = '<Style ss:ID="Result" ss:Name="Result" ss:Parent="Default1">';
$buffer[] = '<Font ss:Bold="1" ss:Size="8" />';
$buffer[] = '<ss:Borders><ss:Border ss:Position="Top" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '<ss:Border ss:Position="Left" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '<ss:Border ss:Position="Bottom" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '<ss:Border ss:Position="Right" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '</ss:Borders></Style>';
$buffer[] = '<ss:Style ss:ID="Head" ss:Name="Head" ss:Parent="Default1">';
$buffer[] = '<ss:Borders><ss:Border ss:Position="Top" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '<ss:Border ss:Position="Left" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '<ss:Border ss:Position="Bottom" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '<ss:Border ss:Position="Right" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
$buffer[] = '</ss:Borders>';
$buffer[] = '<ss:Alignment ss:Horizontal="Center"/><ss:Font ss:Bold="1" /><ss:Interior ss:Color="#c0c0c0" ss:Pattern="Solid" /></ss:Style>';
$buffer[] = '<ss:Style ss:ID="Titles" ss:Name="Title" ss:Parent="Default1">';
$buffer[] = '<ss:Font ss:Bold="1" /></ss:Style>';
$buffer[] = '<Style ss:ID="co1"/>';
$buffer[] = '<Style ss:ID="ro1"/>';
$buffer[] = '<Style ss:ID="ta1"/>';
$buffer[] = '</Styles>';
foreach( $hojas as $nombre => $table ) {
if( empty($nombre) ) $nombre = '(vacio)';
$buffer[] = '<Worksheet ss:Name="'.$nombre.'">';
$buffer[] = '<Table ss:StyleID="ta1">';
$cols = 0;
foreach( $table as $row ) {
$cols = max( $cols, sizeof( $row ) );
}
$width = array();
$head = 1.5;
foreach( $table as $row ) {
if( sizeof( $row ) < $cols ) continue;
foreach( $row as $i => $cell ) {
$width[$i] = max( $width[$i], strlen( $cell )*$head );
}
if( $head > 1 ) $head = 1;
}
foreach( $width as $w ) $buffer[] = '<Column ss:Width="'.round( 5.21 * $w ).'"/>';
$i = 0;
// TITLES
foreach( $table as $i => $row ) {
if( sizeof( $row ) == $cols ) break;
$buffer[] = '<ss:Row>';
foreach( $row as $cell ) {
$buffer[] = '<Cell ss:StyleID="Titles"><Data ss:Type="String">'.$cell.'</Data></Cell>';
}
$buffer[] = '</ss:Row>';
}
// HEADERS
$buffer[] = '<ss:Row>';
$head = $table[$i++];
foreach( $head as $cell ) {
$buffer[] = '<Cell ss:StyleID="Head"><Data ss:Type="String">'.$cell.'</Data></Cell>';
}
$buffer[] = '</ss:Row>';
// BODY
for(; $i < sizeof( $table ); $i++ ) {
$row = $table[$i];
$buffer[] = '<Row ss:Height="13">';
foreach( $row as $cell ) {
if( $cell === '' )
$buffer[] = '<ss:Cell ss:StyleID="Result"></ss:Cell>';
elseif( $cell[0] == '=' )
$buffer[] = '<ss:Cell ss:StyleID="Result" ss:Formula="'.$cell.'"></ss:Cell>';
elseif( is_numeric($cell) )
$buffer[] = '<ss:Cell ss:StyleID="Default1"><ss:Data ss:Type="Number">'.$cell.'</ss:Data></ss:Cell>';
else{
$__cachedoExcel__ = array();
$cell = preg_replace_callback( "/(<[^<]+>)/U", array( 'EXCEL', '__cachedoExcel' ), $cell );
$cell = htmlspecialchars( html_entity_decode( $cell ) );
$cell = preg_replace_callback( "/__cachedoExcel_(\d+)__/U", array( 'EXCEL', '__cachedoExcelR' ), $cell );
$buffer[] = '<ss:Cell ss:StyleID="Default1"><ss:Data ss:Type="String">'.$cell.'</ss:Data></ss:Cell>';
}
}
$buffer[] = '</Row>';
}
$buffer[] = '</Table>';
$buffer[] = '<x:WorksheetOptions><x:PageSetup>';
$buffer[] = '<x:Layout x:Orientation="Landscape" /><x:PageMargins x:Bottom="0.25" x:Left="0.25" x:Right="0.25" x:Top="0.25" />';
$buffer[] = '</x:PageSetup></x:WorksheetOptions>';
$buffer[] = '</Worksheet>';
}
$buffer[] = '</Workbook>';
if( $filename != '' ) return RAIDManager::file_put_contents( $filename, $buffer );
return $buffer;
} |