Ver Mensaje Individual
  #5 (permalink)  
Antiguo 02/09/2013, 22:03
albocv
 
Fecha de Ingreso: agosto-2013
Ubicación: Santiago
Mensajes: 4
Antigüedad: 11 años, 7 meses
Puntos: 0
Respuesta: Problema excel con php

Código PHP:
Ver original
  1. switch ( $_REQUEST['reporte_seleccionado'] ) {
  2. case 1:
  3. $fecha = $_REQUEST['fecha_retiro'];
  4. $muestreos = Buscar_muestreos($fecha,$server,$user,$pass,$base) ;
  5. //print_a($muestreos);
  6. $matriz = array(
  7. 'No',
  8. 'Cliente',
  9. 'Obra',
  10. 'Constructora',
  11. 'Direccion',
  12. 'Comuna',
  13. 'Hormigon',
  14. 'Inicio',
  15. 'Fin',
  16. 'Fre',
  17. 'Vol',
  18. 'Tipo/Cant',
  19. 'Pri',
  20. 'N.P.',
  21. 'C.Obra',
  22. 'Observaciones'
  23. )
  24. );
  25.  
  26.  
  27. foreach( $muestreos as $id => $m ) {
  28. if( $m['MUE_MUE_ESTADO'] != 'Pendiente' ) {
  29. unset( $muestreos[$id] );
  30. continue;
  31. }
  32. $o = $m['MUE_OBR_ID'];
  33.  
  34. $codigo_obra = Buscar_codigo_obra( $m['MUE_CLI_ID'],$o,$server,$user,$pass,$base );
  35. $datos_obras_clientes = Buscar_registro_obras_clientes($m['MUE_CLI_ID'],$codigo_obra,$server,$user,$pass,$base);
  36. $obra = Buscar_datos_obra( $o,$server,$user,$pass,$base );
  37. $datos_cliente = Buscar_cliente_por_id($m['MUE_CLI_ID'],$server,$user,$pass,$base);
  38. $cliente = $datos_cliente['CLI_NOMBRE'];
  39. $contratista = $datos_obras_clientes['O_CLI_CONTRATISTA'];
  40. //print_a($obra);
  41. $matriz[] = array(
  42. $m['MUE_MUE_NUMERO_DIARIO'],
  43. $cliente,
  44. $obra['NOMBRE'],
  45. $contratista,
  46. $obra['DIRECCION'],
  47. $obra['COMUNA'],
  48. $m['MUE_HOR_TIPO']?$m['MUE_HOR_TIPO']:' ',
  49. $m['MUE_MUE_HORA_INI'],
  50. $m['MUE_MUE_HORA_FIN'],
  51. $m['MUE_MUE_FRECUENCIA'],
  52. $m['MUE_HOR_VOLUMEN'],
  53. $m['MUE_HOR_PROBETA_TIPO']." / ".$m['MUE_HOR_PROBETA_CANTIDAD'],
  54. $m['MUE_MUE_PRIORIDAD'],
  55. $m['MUE_NUM_PEDIDO'],
  56. $obras[$o]['codigo'],
  57. $m['MUE_MUE_OBSERVACIONES']?$m['MUE_MUE_OBSERVACIONES']:' '
  58. );
  59.  
  60. }
  61.  
  62.  
  63. streamTable( array( "Plantilla de Muestreos" => $matriz ), 'plantilla_de_muestreos_'.$fecha );

FUNCIONES:

Código PHP:
Ver original
  1. function streamTable( $hojas, $filename = FALSE, $disposition = 'inline', $debug = FALSE ) {
  2.  
  3.  
  4. error_reporting( $debug ? E_ALL ^ E_NOTICE : 0 );
  5.  
  6. $excel = doExcel( $hojas );
  7.  
  8. if( ! $filename ) $filename = md5( uniqid( rand(), true ) );
  9.  
  10. if( $debug ) {
  11. print '<pre>'.htmlentities( implode( "\n", $excel ) ).'</pre>';
  12. } else {
  13. header( 'Content-Type: application/vnd.ms-excel; charset=UTF-8' );
  14. header( 'Content-Disposition: '.$disposition.'; filename="'.$filename.'.xls"' );
  15. print implode( "\n", $excel );
  16. }
  17. }
  18.  
  19.  
  20. function doExcel( $hojas, $filename = '' ){
  21. //require_once( 'RAIDManager.php' );
  22. reset( $hojas );
  23. list( , $p ) = each( $hojas ); list( , $p ) = each( $p );
  24. if( ! is_array( $p ) ) $hojas = array( '' => $hojas );
  25.  
  26. $buffer = array();
  27. $buffer[] = '<'.'?xml version="1.0" encoding="'.iconv_get_encoding( 'output_encoding' ).'"?'.'>';
  28. $buffer[] = '<'.'?mso-application progid="Excel.Sheet"?'.'>';
  29. $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">';
  30.  
  31. $buffer[] = '<Styles>';
  32. $buffer[] = '<Style ss:ID="Default1" ss:Name="Normal">';
  33. $buffer[] = '<Font ss:Size="8"/>';
  34. $buffer[] = '<ss:Borders><ss:Border ss:Position="Top" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  35. $buffer[] = '<ss:Border ss:Position="Left" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  36. $buffer[] = '<ss:Border ss:Position="Bottom" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  37. $buffer[] = '<ss:Border ss:Position="Right" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  38. $buffer[] = '</ss:Borders></Style>';
  39. $buffer[] = '<Style ss:ID="Result" ss:Name="Result" ss:Parent="Default1">';
  40. $buffer[] = '<Font ss:Bold="1" ss:Size="8" />';
  41. $buffer[] = '<ss:Borders><ss:Border ss:Position="Top" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  42. $buffer[] = '<ss:Border ss:Position="Left" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  43. $buffer[] = '<ss:Border ss:Position="Bottom" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  44. $buffer[] = '<ss:Border ss:Position="Right" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  45. $buffer[] = '</ss:Borders></Style>';
  46. $buffer[] = '<ss:Style ss:ID="Head" ss:Name="Head" ss:Parent="Default1">';
  47. $buffer[] = '<ss:Borders><ss:Border ss:Position="Top" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  48. $buffer[] = '<ss:Border ss:Position="Left" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  49. $buffer[] = '<ss:Border ss:Position="Bottom" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  50. $buffer[] = '<ss:Border ss:Position="Right" ss:Color="Black" ss:LineStyle="Continuous" ss:Weight="1" />';
  51. $buffer[] = '</ss:Borders>';
  52. $buffer[] = '<ss:Alignment ss:Horizontal="Center"/><ss:Font ss:Bold="1" /><ss:Interior ss:Color="#c0c0c0" ss:Pattern="Solid" /></ss:Style>';
  53. $buffer[] = '<ss:Style ss:ID="Titles" ss:Name="Title" ss:Parent="Default1">';
  54. $buffer[] = '<ss:Font ss:Bold="1" /></ss:Style>';
  55. $buffer[] = '<Style ss:ID="co1"/>';
  56. $buffer[] = '<Style ss:ID="ro1"/>';
  57. $buffer[] = '<Style ss:ID="ta1"/>';
  58. $buffer[] = '</Styles>';
  59.  
  60. foreach( $hojas as $nombre => $table ) {
  61.  
  62. if( empty($nombre) ) $nombre = '(vacio)';
  63.  
  64. $buffer[] = '<Worksheet ss:Name="'.$nombre.'">';
  65. $buffer[] = '<Table ss:StyleID="ta1">';
  66.  
  67. $cols = 0;
  68. foreach( $table as $row ) {
  69. $cols = max( $cols, sizeof( $row ) );
  70. }
  71.  
  72. $width = array();
  73. $head = 1.5;
  74. foreach( $table as $row ) {
  75. if( sizeof( $row ) < $cols ) continue;
  76. foreach( $row as $i => $cell ) {
  77. $width[$i] = max( $width[$i], strlen( $cell )*$head );
  78. }
  79. if( $head > 1 ) $head = 1;
  80. }
  81.  
  82. foreach( $width as $w ) $buffer[] = '<Column ss:Width="'.round( 5.21 * $w ).'"/>';
  83.  
  84. $i = 0;
  85.  
  86. // TITLES
  87. foreach( $table as $i => $row ) {
  88. if( sizeof( $row ) == $cols ) break;
  89. $buffer[] = '<ss:Row>';
  90. foreach( $row as $cell ) {
  91. $buffer[] = '<Cell ss:StyleID="Titles"><Data ss:Type="String">'.$cell.'</Data></Cell>';
  92. }
  93. $buffer[] = '</ss:Row>';
  94. }
  95.  
  96. // HEADERS
  97. $buffer[] = '<ss:Row>';
  98. $head = $table[$i++];
  99. foreach( $head as $cell ) {
  100. $buffer[] = '<Cell ss:StyleID="Head"><Data ss:Type="String">'.$cell.'</Data></Cell>';
  101. }
  102. $buffer[] = '</ss:Row>';
  103.  
  104. // BODY
  105. for(; $i < sizeof( $table ); $i++ ) {
  106. $row = $table[$i];
  107. $buffer[] = '<Row ss:Height="13">';
  108. foreach( $row as $cell ) {
  109. if( $cell === '' )
  110. $buffer[] = '<ss:Cell ss:StyleID="Result"></ss:Cell>';
  111. elseif( $cell[0] == '=' )
  112. $buffer[] = '<ss:Cell ss:StyleID="Result" ss:Formula="'.$cell.'"></ss:Cell>';
  113. elseif( is_numeric($cell) )
  114. $buffer[] = '<ss:Cell ss:StyleID="Default1"><ss:Data ss:Type="Number">'.$cell.'</ss:Data></ss:Cell>';
  115. else{
  116. $__cachedoExcel__ = array();
  117. $cell = preg_replace_callback( "/(<[^<]+>)/U", array( 'EXCEL', '__cachedoExcel' ), $cell );
  118. $cell = preg_replace_callback( "/__cachedoExcel_(\d+)__/U", array( 'EXCEL', '__cachedoExcelR' ), $cell );
  119. $buffer[] = '<ss:Cell ss:StyleID="Default1"><ss:Data ss:Type="String">'.$cell.'</ss:Data></ss:Cell>';
  120. }
  121. }
  122. $buffer[] = '</Row>';
  123. }
  124.  
  125. $buffer[] = '</Table>';
  126. $buffer[] = '<x:WorksheetOptions><x:PageSetup>';
  127. $buffer[] = '<x:Layout x:Orientation="Landscape" /><x:PageMargins x:Bottom="0.25" x:Left="0.25" x:Right="0.25" x:Top="0.25" />';
  128. $buffer[] = '</x:PageSetup></x:WorksheetOptions>';
  129. $buffer[] = '</Worksheet>';
  130. }
  131. $buffer[] = '</Workbook>';
  132.  
  133. if( $filename != '' ) return RAIDManager::file_put_contents( $filename, $buffer );
  134. return $buffer;
  135. }