I'm trying to import xlsx with styled text (bold, font-size, hyperlinks, etc) but all I get when trying to import it is plain text.
Is there anyway to import a file with that info? I want to convert it to html tags after that.
I don't think that it is possible. How you will get info is it bold or no?
If Excel can get that information, I think there must be a way to get it with PHP, but I don't know how. For what I've seen, this plugin only gets plain text...
@Lydnasty I ran into this as well, and I had trouble digging up information on how to use getStyle().
I was able to grab this information through a sample code below:
Note that this is just a sample of how I was able to get the Cell formatting. One thing I did notice is that the Default fill color is 000000, so it may be a bit confusing that a background fill color is empty (which should be ffffff instead of 000000).
Hope this helps, not sure if there's a better way. Examples on the web point to getStyle through Laravel-Excel (Maatwebsite) but those examples return "undefined function getStyle". I had to do this below instead:
Excel::load('/path/to/excel/file.xlsx', function($reader)
{
$excel = $reader->getExcel();
foreach ( $excel->getAllSheets() as $sheet )
{
foreach ( $sheet->getRowIterator() as $index => $row )
{
foreach ( $row->getCellIterator() as $cell )
{
$value = $cell->getValue();
$styles = getCellStyles($cell);
dd([$value => $styles]);
}
}
}
});
function getCellStyles($cell)
{
if ( ! method_exists($cell, 'getStyle') )
{
return false;
}
$getStyle = $cell->getStyle();
$fill = $getStyle->getFill();
$font = $getStyle->getFont();
$borders = $getStyle->getBorders();
$alignment = $getStyle->getAlignment();
return [
'fill' => [
'color' => $fill->getFillType() == 'none' ? '' : $fill->getStartColor()->getRGB(),
'filled' => $fill->getFillType(),
],
'font' => [
'name' => $font->getName(),
'size' => $font->getSize(),
'color' => $font->getColor()->getRGB(),
],
'borders' => [
'left' => [
'color' => $borders->getLeft()->getColor()->getRGB(),
'thickness' => $borders->getLeft()->getBorderStyle(),
],
'right' => [
'color' => $borders->getRight()->getColor()->getRGB(),
'thickness' => $borders->getRight()->getBorderStyle(),
],
'top' => [
'color' => $borders->getTop()->getColor()->getRGB(),
'thickness' => $borders->getTop()->getBorderStyle(),
],
'bottom' => [
'color' => $borders->getBottom()->getColor()->getRGB(),
'thickness' => $borders->getBottom()->getBorderStyle(),
],
],
'alignment' => [
'horizontal' => $alignment->getHorizontal(),
'vertical' => $alignment->getVertical(),
'wrap' => $alignment->getWrapText(),
'shrink' => $alignment->getShrinkToFit(),
'indent' => $alignment->getIndent(),
],
];
}
thx @nathanh0 (Y) worked fine for me (Y)
This seems to work fine if the cell itself is styled, but if you have nested styles witin the text itself, it won't work, as I found out :(
Most helpful comment
@Lydnasty I ran into this as well, and I had trouble digging up information on how to use getStyle().
I was able to grab this information through a sample code below:
Note that this is just a sample of how I was able to get the Cell formatting. One thing I did notice is that the Default fill color is 000000, so it may be a bit confusing that a background fill color is empty (which should be ffffff instead of 000000).
Hope this helps, not sure if there's a better way. Examples on the web point to getStyle through Laravel-Excel (Maatwebsite) but those examples return "undefined function getStyle". I had to do this below instead: