Вы находитесь на странице: 1из 9

PHPexcel es muy buena. http://phpexcel.codeplex.com/ la clase debes colocar en la carpeta libs de tu app directorio libs/PHPexcel.

php libs/PHPexcel =>todo el contenido de la carpeta la consulta en la base se hace igual como imprimes en un listar te dejo un ejemplo de como va en la vista <?php /** Error reporting */ error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />'); date_default_timezone_set('America/Bogota'); if (PHP_SAPI == 'cli') die('This example should only be run from a Web Browser'); /** Include PHPExcel */ Load::lib('PHPExcel'); $dir = dirname(APP_PATH) . "/public/"; $ruta = $dir . "img/default"; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setCreator("ASDIN-SHOP.COM") ->setLastModifiedBy("PIL") ->setTitle("LISTA DE PRODUCTOS") ->setSubject("Documento Generado por PHPExcel") ->setDescription("LISTA DE PRODUCTOS PIL.") ->setKeywords("LISTA DE PRODUCTOS PIL") ->setCategory("LISTA DE PRODUCTOS PIL"); // Se crea una primera hoja, que representa la fecha de venta // Combinar celdas titulo echo date('H:i:s'), " Merge cells", EOL; $objPHPExcel->getActiveSheet()->setShowGridlines(false); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(2); //ANCHO DE CELDAS //$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(13); $objPHPExcel->getActiveSheet()->mergeCells('B1:D6'); //CONVINAR CELDAS $objPHPExcel->getActiveSheet()->mergeCells('E3:G3'); //CONVINAR CELDAS $objPHPExcel->getActiveSheet()->setCellValue('E3', 'LISTA DE PRODUCTOS CON STOCK'); //CELDA Y CONTENIDO $objPHPExcel->getActiveSheet()->setCellValue('B7', 'CODIGO PIL'); //CELDA Y CONTENIDO

$objPHPExcel->getActiveSheet()->setCellValue('C7', 'MARCA'); $objPHPExcel->getActiveSheet()->setCellValue('D7', 'PRODUCTO'); $objPHPExcel->getActiveSheet()->setCellValue('E7', '# PARTE'); $objPHPExcel->getActiveSheet()->setCellValue('F7', 'DESCRIPCION'); $objPHPExcel->getActiveSheet()->setCellValue('G7', 'DIMENSION'); $objPHPExcel->getActiveSheet()->setCellValue('H7', 'UNIDAD'); $objPHPExcel->getActiveSheet()->setCellValue('I7', 'PROVEEDOR'); $objPHPExcel->getActiveSheet()->setCellValue('J7', 'NUM. OFERTA'); $objPHPExcel->getActiveSheet()->setCellValue('K7', 'RESPONSABLE'); $objPHPExcel->getActiveSheet()->setCellValue('L7', 'FECHA COTIZACION'); $objPHPExcel->getActiveSheet()->setCellValue('M7', 'CANTIDAD'); $objPHPExcel->getActiveSheet()->setCellValue('N7', 'TIEMPO ENTREGA'); $objPHPExcel->getActiveSheet()->setCellValue('O7', 'PARTIDA ARANCELARIA'); $objPHPExcel->getActiveSheet()->setCellValue('P7', 'PORCENTAJE IMPORTACION'); $objPHPExcel->getActiveSheet()->setCellValue('Q7', 'PRECIO1'); $objPHPExcel->getActiveSheet()->setCellValue('R7', 'FECHA1'); $objPHPExcel->getActiveSheet()->setCellValue('S7', 'PRECIO2'); $objPHPExcel->getActiveSheet()->setCellValue('T7', 'FECHA2'); $objPHPExcel->getActiveSheet()->setCellValue('U7', 'PRECIO3'); $objPHPExcel->getActiveSheet()->setCellValue('V7', 'FECHA3'); $a = 8; foreach ($productos as $key => $value): $objPHPExcel->getActiveSheet()->setCellValue('B' . $a, $value->codigo); $objPHPExcel->getActiveSheet()->setCellValue('C' . $a, $value->marca); $objPHPExcel->getActiveSheet()->setCellValue('D' . $a, $value->producto); $objPHPExcel->getActiveSheet()->setCellValue('E' . $a, $value->num_parte); $objPHPExcel->getActiveSheet()->setCellValue('F' . $a, $value->descripcion); $objPHPExcel->getActiveSheet()->setCellValue('G' . $a, $value->dimencion); $objPHPExcel->getActiveSheet()->setCellValue('H' . $a, $value->simbolo); $objPHPExcel->getActiveSheet()->setCellValue('I' . $a, $value->proveedor); $objPHPExcel->getActiveSheet()->setCellValue('J' . $a, $value->num_compra); $objPHPExcel->getActiveSheet()->setCellValue('K' . $a, $value->login); $objPHPExcel->getActiveSheet()->setCellValue('L' . $a, $value->fecha_compra); $objPHPExcel->getActiveSheet()->setCellValue('M' . $a, $value->cantidad); $objPHPExcel->getActiveSheet()->setCellValue('N' . $a, $value->tiempo_entrega); $objPHPExcel->getActiveSheet()->setCellValue('O' . $a, $value->partida_arancelaria); $objPHPExcel->getActiveSheet()->setCellValue('P' . $a, $value->porcentaje_importacion); $objPHPExcel->getActiveSheet()->setCellValue('Q' . $a, $value->getListadoPrecios($value->id)>precio); $objPHPExcel->getActiveSheet()->setCellValue('R' . $a, $value->getListadoPrecios($value->id)>fecha_compra); foreach ($value->getListadoPrecios($value->id,2) as $precio): $objPHPExcel->getActiveSheet()->setCellValue('S'.$a, $precio->precio); $objPHPExcel->getActiveSheet()->setCellValue('T'.$a, $precio->fecha_compra); endforeach; foreach ($value->getListadoPrecios($value->id, 3) as $precio): $objPHPExcel->getActiveSheet()->setCellValue('U'.$a, $precio->precio); $objPHPExcel->getActiveSheet()->setCellValue('V'.$a, $precio->fecha_compra); endforeach;

$a++; endforeach;

//$objPHPExcel->getActiveSheet()->mergeCells('A28:B28'); // Just to test... //$objPHPExcel->getActiveSheet()->unmergeCells('A28:B28'); // Just to test... // Establecer las alineaciones //echo date('H:i:s'), " Set alignments", EOL; //$objPHPExcel->getActiveSheet()->getStyle('B7,C7,D7')->getAlignment()>setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);//ALINEAR A LA DERECHA $objPHPExcel->getActiveSheet()->getStyle('B7:V7')->getAlignment()>setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //ALINEAR AL CENTRO $objPHPExcel->getActiveSheet()->getStyle('B7:V7')->getAlignment()>setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //ALINEAR AL CENTRO $objPHPExcel->getActiveSheet()->getStyle('B7:V7')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('B7:V7')->getFont()->getColor()>setARGB(PHPExcel_Style_Color::COLOR_WHITE); $objPHPExcel->getActiveSheet()->getStyle('B7:V7')->getFill()>setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet()->getStyle('B7:V7')->getFill()->getStartColor()>setARGB(PHPExcel_Style_Color::COLOR_DARKBLUE); //COLOR DE FONDO $objPHPExcel->getActiveSheet()->getStyle('E3')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getRowDimension('7')->setRowHeight(20); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(40); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10); $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(25); $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(25); $objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('R')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('S')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('T')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('U')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('V')->setWidth(20);

// Definir fuentes //echo date('H:i:s'), " Set fonts", EOL; //$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara'); // Agregar un dibujo a la hoja de clculo //echo date('H:i:s'), " Add a drawing to the worksheet", EOL; $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('Logo'); $objDrawing->setDescription('Logo'); $objDrawing->setPath("$ruta/logo.png"); $objDrawing->setHeight(115); $objDrawing->setCoordinates('B1');

$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

// llenar celdas //echo date('H:i:s'), " Set fills", EOL; //$objPHPExcel->getActiveSheet()->getStyle('A1:G1')->getFill()>setFillType(PHPExcel_Style_Fill::FILL_SOLID); // COLOR CELDAS RELLENO //$objPHPExcel->getActiveSheet()->getStyle('A1:G1')->getFill()->getStartColor()>setARGB('FF808080'); // Cambiar el nombre de primera hoja de clculo echo date('H:i:s'), " Rename first MATERIALES-PRODUCTOS", EOL; $objPHPExcel->getActiveSheet()->setTitle('MATERIALES-PRODUCTOS'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); /** Error reporting */ error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); date_default_timezone_set('Europe/London'); Load::lib('PHPExcel/IOFactory'); // Save Excel 2007 file echo date('H:i:s'), " Write to Excel2007 format", EOL; $callStartTime = microtime(true);

header("Last-Modified:" . gmdate . ( "D, d MYH: i: s" ) . "GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check = 0, pre-check = 0", false); header("Pragma: no-cache"); header('Content-Type: application / vnd.openxmlformats officedocument.spreadsheetml.sheet-'); header('Content-Disposition: attachment; filename = "LISTADO DE PRODUCTOS.xlsx"'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); ob_end_clean(); $objWriter->save('php://output'); //$objWriter->save(str_replace('.phtml', '.xlsx', __FILE__)); exit; // Save Excel 95 file header("Last-Modified:" . gmdate . ( "D, d MYH: i: s" ) . "GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check = 0, pre-check = 0", false); header("Pragma: no-cache"); header('Content-Type: application / vnd.openxmlformats officedocument.spreadsheetml.sheet-'); header('Content-Disposition: attachment; filename = "LISTADO DE PRODUCTOS.xlsx"'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); ob_end_clean(); //$objWriter->save(str_replace('.phtml', '.xls', __FILE__)); $objWriter->save('php://output');

exit; ?>

El domingo, 1 de diciembre de 2013 18:56:55 UTC-5, Yader Centeno escribi:


- mostrar texto citado -

manuel_j555

Re: [KumbiaPHP 9794] Re: Exportar datos a Excel con KumbiaPHP

Buenas, como recomendacin adicional siempre es mejor pasar a una variable el activeSheet para no estar haciendo: $objPHPExcel->getActiveSheet()->..... es mejor: $sheet = $objPHPExcel->getActiveSheet(); y luego si usar directamente $sheet->... Saludos!!!

manuel_j555

Re: [KumbiaPHP 9794] Re: Exportar datos a Excel con KumbiaPHP

ac tienes otro ejemplo de uso: https://github.com/manuelj555/PresupuestoBundle/blob/master/Resources/views/Presupuesto/pres upuesto.xls.php

El 3 de diciembre de 2013 07:29, Manuel Aguirre <programad...@gmail.com> escribi:


- mostrar texto citado -

Yader Centeno

Hola a todos: Gracias por sus recomendaciones. He intentado usar el PHPExcel de Codeplex.com, segn el cdigo de ejemplo de EDUARYAZ y de manuel_j555. Descargue el PHPExcel y lo copie completo en la carpeta "app/libs". Sin embargo, cuando doy click en un botn (en mi vista "listar.phtml" que tengo para ir a la funcin de exportar (en mi controlador), solo se queda la ruedita del AJAX como que estuviera ocupado, pero de ah no pasa. Qu estar haciendo mal? Porque no recibo ningn mensaje de error. Aunque sinceramente, no se si el cdigo que puse en el controlador (abajo detallado), es el correcto. Les dejo mi cdigo por si me pueden ayudar a ver donde estoy mal. Me podran ayudar, por favor? He creado la siguiente vista, que he llamado ": <?php /** Error reporting */ error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); Load::lib('PHPExcel'); Load::lib('PHPExcel/IOFactory'); //invoco la clase para generar el libro excel $libro = new PHPExcel(); //creo una hoja, es decir, puedo crear N hojas $hoja1 = $libro->getActiveSheet(); $hoja1->setTitle("presupuesto"); $hoja1->setShowGridlines(false); //Tamao de las columnas $hoja1->getColumnDimension('A')->setWidth("10"); $hoja1->getColumnDimension('B')->setWidth("30"); $hoja1->getColumnDimension('C')->setWidth("100"); $hoja1->getColumnDimension('D')->setWidth("50"); $hoja1->getColumnDimension('E')->setWidth("50"); $hoja1->getColumnDimension('F')->setWidth("30"); $hoja1->getColumnDimension('G')->setWidth("30"); $hoja1->getColumnDimension('H')->setWidth("30"); $hoja1->getColumnDimension('I')->setWidth("30"); $hoja1->getColumnDimension('J')->setWidth("30"); $hoja1->getColumnDimension('K')->setWidth("30"); $hoja1->getColumnDimension('L')->setWidth("30"); $hoja1->getColumnDimension('M')->setWidth("30"); $hoja1->getColumnDimension('N')->setWidth("30"); $hoja1->getColumnDimension('O')->setWidth("30");

$hoja1->getColumnDimension('P')->setWidth("30"); $hoja1->getColumnDimension('Q')->setWidth("30"); $hoja1->getColumnDimension('R')->setWidth("30"); //a partir de que fila se mostrarn las descripciones, la primera son las cabeceras $headerRow = 4; //seteamos los valores de titulo y cabeceras $hoja1->setCellValue("A1", "Presupuesto por Grupo de usuarios"); $hoja1->setCellValue("A{$headerRow}", "ID"); $hoja1->setCellValue("B{$headerRow}", "Periodo"); $hoja1->setCellValue("C{$headerRow}", "Cuenta"); $hoja1->setCellValue("D{$headerRow}", "Delegacion"); $hoja1->setCellValue("E{$headerRow}", "Grupo de usuarios"); $hoja1->setCellValue("F{$headerRow}", "Mes 1"); $hoja1->setCellValue("G{$headerRow}", "Mes 2"); $hoja1->setCellValue("H{$headerRow}", "Mes 3"); $hoja1->setCellValue("I{$headerRow}", "Mes 4"); $hoja1->setCellValue("J{$headerRow}", "Mes 5"); $hoja1->setCellValue("K{$headerRow}", "Mes 6"); $hoja1->setCellValue("L{$headerRow}", "Mes 7"); $hoja1->setCellValue("M{$headerRow}", "Mes 8"); $hoja1->setCellValue("N{$headerRow}", "Mes 9"); $hoja1->setCellValue("O{$headerRow}", "Mes 10"); $hoja1->setCellValue("P{$headerRow}", "Mes 11"); $hoja1->setCellValue("Q{$headerRow}", "Mes 12"); $hoja1->setCellValue("R{$headerRow}", "Notas"); //unimos las columnas A1 y E1 donde v el ttulo $hoja1->mergeCells("A1:R1"); //ac sacamos la fila a partir de donde van las descripciones, luego de las cabeceras $initialRow = $headerRow + 1; $fila = $initialRow; //recorremos las descripciones de cada presupuesto de grupo y seteamos los valores en la celdas foreach($apresupareapag->items as $apresuparea): $hoja1->setCellValue("A{$fila}", $apresuparea->id); $hoja1->setCellValue("B{$fila}", $apresuparea->nombre_periodo); $hoja1->setCellValue("C{$fila}", $apresuparea->codigo_cta); $hoja1->setCellValue("D{$fila}", $apresuparea->codigo_area); $hoja1->setCellValue("E{$fila}", $apresuparea->nombre_grupo); $hoja1->setCellValue("F{$fila}", $apresuparea->mes1); $hoja1->setCellValue("G{$fila}", $apresuparea->mes2); $hoja1->setCellValue("H{$fila}", $apresuparea->mes3); $hoja1->setCellValue("I{$fila}", $apresuparea->mes4); $hoja1->setCellValue("J{$fila}", $apresuparea->mes5); $hoja1->setCellValue("K{$fila}", $apresuparea->mes6); $hoja1->setCellValue("L{$fila}", $apresuparea->mes7); $hoja1->setCellValue("M{$fila}", $apresuparea->mes8); $hoja1->setCellValue("N{$fila}", $apresuparea->mes9); $hoja1->setCellValue("O{$fila}", $apresuparea->mes10); $hoja1->setCellValue("P{$fila}", $apresuparea->mes11); $hoja1->setCellValue("Q{$fila}", $apresuparea->mes12); $hoja1->setCellValue("R{$fila}", $apresuparea->observaciones); ++$fila;

endforeach; //anterior tendr el valor de la ultima fila con descripcin $ultima = $fila - 1; //Definimos los totales $hoja1->setCellValue("E{$fila}", "TOTAL"); $hoja1->setCellValue("F{$fila}", "=SUM(F{$initialRow}:F{$ultima})"); $hoja1->setCellValue("G{$fila}", "=SUM(G{$initialRow}:G{$ultima})"); $hoja1->setCellValue("H{$fila}", "=SUM(H{$initialRow}:H{$ultima})"); $hoja1->setCellValue("I{$fila}", "=SUM(I{$initialRow}:I{$ultima})"); $hoja1->setCellValue("J{$fila}", "=SUM{J{$initialRow}:J{$ultima})"); $hoja1->setCellValue("K{$fila}", "=SUM(K{$initialRow}:K{$ultima})"); $hoja1->setCellValue("L{$fila}", "=SUM(L{$initialRow}:L{$ultima})"); $hoja1->setCellValue("M{$fila}", "=SUM(M{$initialRow}:M{$ultima})"); $hoja1->setCellValue("N{$fila}", "=SUM(N{$initialRow}:N{$ultima})"); $hoja1->setCellValue("O{$fila}", "=SUM(O{$initialRow}:O{$ultima})"); $hoja1->setCellValue("P{$fila}", "=SUM(P{$initialRow}:P{$ultima})"); $hoja1->setCellValue("Q{$fila}", "=SUM(Q{$initialRow}:Q{$ultima})"); $libro->setActiveSheetIndex(0); // Guardar en Excel 2007 file header("Last-Modified:" . gmdate . ( "D, d MYH: i: s" ) . "GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check = 0, pre-check = 0", false); header("Pragma: no-cache"); header('Content-Type: application / vnd.openxmlformats officedocument.spreadsheetml.sheet-'); header('Content-Disposition: attachment; filename = "PRESUPUESTO.xlsx"'); $objWriter = PHPExcel_IOFactory::createWriter($libro, 'Excel2007'); ob_end_clean(); $objWriter->save('php://output'); exit; ?> Tambin he creado la siguiente funcin en el controlador: <?php public function exportar($order='order.id.asc', $page='pag.1', $grupo='0'){ View::template(NULL); //Agregado para que no envi todo el html(Beta2). En Beta1 $this>template=NULL View::response('xls'); $apresuparea = new Apresuparea(); $this->apresupareapag = $apresuparea->getListadoApresuparea($order, $page, $grupo); $this->page = $page; $this->grupo = $grupo; $this->order = $order; } ?> Y desde la vista principal "listar.phtml", he puesto un botn para hacer click y que me genere el archivo de excel: <?php

echo DwHtml::button('presupuesto/apresuparea/exportar/order.id.asc/1/'.$grupo.'/', 'Exportar a Excel', array('class'=>'btn-info', 'id'=>'botonexportar'), 'print', APP_AJAX); ?>

Вам также может понравиться