Styling with PHPExcel methods

It’s possible to style the sheets and specific cells with help of PHPExcel methods. This package includes a lot of shortcuts (see export documentation), but also always the use of the native methods.

// Set background color for a specific cell
$sheet->getStyle('A1')->applyFromArray(array(
    'fill' => array(
        'type'  => PHPExcel_Style_Fill::FILL_SOLID,
        'color' => array('rgb' => 'FF0000')
    )
));

Using HTML tags

Most of the HTML tags are supported.

<html>

    <!-- Headings -->
    <td><h1>Big title</h1></td>

    <!--  Bold -->
    <td><b>Bold cell</b></td>
    <td><strong>Bold cell</strong></td>

    <!-- Italic -->
    <td><i>Italic cell</i></td>

    <!-- Images -->
    <td><img src="img.jpg" /></td>

</html>

Inside the view.php config you can change how these tags will be interpreted by Excel by default.

Using HTML attributes

Some of the basic styling can be done with HTML attributes.

<html>

    <!-- Horizontal alignment -->
    <td align="right">Big title</td>

    <!--  Vertical alignment -->
    <td valign="middle">Bold cell</td>

    <!-- Rowspan -->
    <td rowspan="3">Bold cell</td>

    <!-- Colspan -->
    <td colspan="6">Italic cell</td>

    <!-- Width -->
    <td width="100">Cell with width of 100</td>

    <!-- Height -->
    <td height="100">Cell with height of 100</td>

</html>

Styling through inline-styles

It’s possible to use inline styles inside your view files. Most of the general styles are supported.

<html>

    <!-- Cell with black background -->
    <td style="background-color: #000000;">Cell</td>

</html>

Inside the reference guide you can find a list of supported styles.

Styling through external CSS file

Styling can be done through an external CSS file.

External css file:

#cell {
    background-color: #000000;
    color: #ffffff;
}

.cell {
    background-color: #000000;
    color: #ffffff;
}

tr td {
    background-color: #ffffff;
}

tr > td {
    border-bottom: 1px solid #000000;
}

Table:

<html>

    {{ HTML::style('css/table.css') }}

    <!-- Cell styled with class -->
    <td class="cell">Cell</td>

    <!-- Cell styled with ID -->
    <td id="cell">Cell</td>

</html>





Available file properties

Properties that can be set with $excel->set{$property}()

Property name
creator
lastModifiedBy
title
description
subject
keywords
category
manager
company

Available sheet properties

Properties that can be set with $sheet->set{$property}()

Property namePossible value
orientationstring
paperSizeinteger
scaleinteger
fitToPageboolean
fitToHeightboolean
fitToWidthboolean
columnsToRepeatAtLeftarray
rowsToRepeatAtToparray
horizontalCenteredboolean
verticalCenteredboolean
printArearange
firstPageNumberinteger

Available CSS styles

Styles that can be used inside an external CSS file or as inline CSS.

Style nameExample Value
background(-color)#000000
color#FFFFFF
font-weightbold
font-styleitalic
font-weightbold
font-size20px
font-familyOpen Sans
text-decorationunderline
text-aligncenter
vertical-alignmiddle
border(-*)1px dashed #CCC
width100(px)
height1100(px)

Available border styles

Style namePHPExcel class reference
nonePHPExcel_Style_Border::BORDER_NONE
dashDotPHPExcel_Style_Border::BORDER_DASHDOT
dashDotDotPHPExcel_Style_Border::BORDER_DASHDOTDOT
dashedPHPExcel_Style_Border::BORDER_DASHED
dottedPHPExcel_Style_Border::BORDER_DOTTED
doublePHPExcel_Style_Border::BORDER_DOUBLE
hairPHPExcel_Style_Border::BORDER_HAIR
mediumPHPExcel_Style_Border::BORDER_MEDIUM
mediumDashDotPHPExcel_Style_Border::BORDER_MEDIUMDASHDOT
mediumDashDotDotPHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT
mediumDashedPHPExcel_Style_Border::BORDER_MEDIUMDASHED
slantDashDotPHPExcel_Style_Border::BORDER_SLANTDASHDOT
thickPHPExcel_Style_Border::BORDER_THICK
thinPHPExcel_Style_Border::BORDER_THIN

Available column formatting

Format namePHPExcel class reference
GeneralPHPExcel_Style_NumberFormat::FORMAT_GENERAL
@PHPExcel_Style_NumberFormat::FORMAT_TEXT
0PHPExcel_Style_NumberFormat::FORMAT_NUMBER
0.00PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00
#,##0.00PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1
#,##0.00_-PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED2
0%PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE
0.00%PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00
yyyy-mm-ddPHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2
yy-mm-ddPHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD
dd/mm/yyPHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY
d/m/yPHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH
d-m-yPHPExcel_Style_NumberFormat::FORMAT_DATE_DMYMINUS
d-mPHPExcel_Style_NumberFormat::FORMAT_DATE_DMMINUS
m-yPHPExcel_Style_NumberFormat::FORMAT_DATE_MYMINUS
mm-dd-yyPHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX14
d-mmm-yyPHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15
d-mmmPHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX16
mmm-yyPHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX17
m/d/yy h:mmPHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX22
d/m/y h:mmPHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME
h:mm AM/PMPHPExcel_Style_NumberFormat::FORMAT_DATE_TIME1
h:mm:ss AM/PMPHPExcel_Style_NumberFormat::FORMAT_DATE_TIME2
h:mmPHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3
h:mm:ssPHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4
mm:ssPHPExcel_Style_NumberFormat::FORMAT_DATE_TIME5
h:mm:ssPHPExcel_Style_NumberFormat::FORMAT_DATE_TIME6
i:s.SPHPExcel_Style_NumberFormat::FORMAT_DATE_TIME7
h:mm:ss;@PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME8
yy/mm/dd;@PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH
“$”#,##0.00_-PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE
$#,##0_-PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD
[$EUR ]#,##0.00_-PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE

Closures

MethodClosure class
create()Maatwebsite\Excel\Writers\LaravelExcelWriter
load()Maatwebsite\Excel\Readers\LaravelExcelReader
batchMaatwebsite\Excel\Readers\Batch
sheet()Maatwebsite\Excel\Classes\LaravelExcelWorksheet
cells()Maatwebsite\Excel\Writers\CellWriter
row()Maatwebsite\Excel\Writers\CellWriter