Good Morning, i need help with the line breaks in laravel excel, This is my code:
public function export()
{
Excel::create('Entrada de Articulos', function($excel) {
$excel->sheet('Productos', function($sheet) {
$products=DB::table('inventario_seriales')
->join('renglones','inventario_seriales.id_renglon','=','renglones.id_renglon')
->select('inventario_seriales.*','renglones.descrip_renglon')
->get();
foreach($products as $product)
{
$sheet->fromArray([$product->id_serial,
$product->id_detalle,
$product->id_transaccion,
$product->id_renglon,
$product->serial,
$product->estatus,
$product->descrip_renglon,
]);
}
});
})->export('xls');
}
The problem is with the line breaks, because all the records is on the same column, and i want to put a line break to order that and put the head of each one.
Hi, i believe that i resolve that with this piece of code
foreach($products as $product)
{
$sheet->appendRow(array($product->id_serial,
$product->id_detalle,
$product->id_transaccion,
$product->id_renglon,
$product->serial,
$product->estatus,
$product->descrip_renglon,
));
}
I replace the method fromArray for appendRow, and i resolve that, but now i want an answer to supports this solution.
Yeah appends row should do the trick in the way you have written it.
Alternatively you can do:
public function export()
{
Excel::create('Entrada de Articulos', function($excel) {
$excel->sheet('Productos', function($sheet) {
$products=DB::table('inventario_seriales')
->join('renglones','inventario_seriales.id_renglon','=','renglones.id_renglon')
->select('inventario_seriales.*','renglones.descrip_renglon')
->get();
$data = [];
foreach($products as $product) {
$data[] = array($product->id_serial,
$product->id_detalle,
$product->id_transaccion,
$product->id_renglon,
$product->serial,
$product->estatus,
$product->descrip_renglon,
)
$sheet->fromArray($data);
}
});
})->export('xls');
}
I think that an easy way to doit, is using the
$i = 0;
foreach($products as $product)
{
if($i > 0)
{
$html .= '<br>';
}
$html .= $product->id_serial . ' ' . $product->id_detalle . ' ' . $product->id_transaccion . ' ' . $product->id_renglon . ' ' . $product->serial . ' ' . $product->estatus . ' ' . $product->descrip_renglon;
$i = $i + 1;
}
$helper = new PHPExcel_Helper_HTML;
$richText = $helper->toRichTextObject($html);
$sheet->setCellValue('A1', $richText); //Put it on the right cell
This should work.
Sorry for my english, my mattern language is Spanish ( I saw that the table records are in spanish so i assume that Iavy speak spanish ). But maybe in english is also usefull to another participants.
Best regards.
append PHP_EOL for break a line its working fine
like : $html = "some string" . PHP_EOL . "this will be in new line";
Most helpful comment
append PHP_EOL for break a line its working fine
like : $html = "some string" . PHP_EOL . "this will be in new line";