1. Go to this page and download the library: Download spatie/simple-excel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
spatie / simple-excel example snippets
use Spatie\SimpleExcel\SimpleExcelReader;
SimpleExcelReader::create($pathToFile)->getRows()
->each(function(array $rowProperties) {
// process the row
});
use Spatie\SimpleExcel\SimpleExcelReader;
// $rows is an instance of Illuminate\Support\LazyCollection
$rows = SimpleExcelReader::create($pathToCsv)->getRows();
$rows->each(function(array $rowProperties) {
// in the first pass $rowProperties will contain
// ['email' => '[email protected]', 'first_name' => 'john']
});
// $rows is an instance of Illuminate\Support\LazyCollection
$rows = SimpleExcelReader::create($pathToCsv)
->noHeaderRow()
->getRows()
->each(function(array $rowProperties) {
// in the first pass $rowProperties will contain
// [0 => 'john@example', 1 => 'john']
});
// $rows is an instance of Illuminate\Support\LazyCollection
$rows = SimpleExcelReader::create($pathToCsv)
->useHeaders(['email_address', 'given_name'])
->getRows()
->each(function(array $rowProperties) {
// in the first pass $rowProperties will contain
// ['email_address' => 'john@example', 'given_name' => 'john']
});
// $rows is an instance of Illuminate\Support\LazyCollection
$rows = SimpleExcelReader::create($pathToCsv)
->trimHeaderRow()
->headerOnRow(3)
->getRows()
->each(function(array $rowProperties) {
// in the first pass $rowProperties will contain
// ['email' => 'john@example', 'first_name' => 'john']
});
// $rows is an instance of Illuminate\Support\LazyCollection
$rows = SimpleExcelReader::create($pathToCsv)
->trimHeaderRow()
->getRows()
->each(function(array $rowProperties) {
// in the first pass $rowProperties will contain
// ['email' => 'john@example', 'first_name' => 'john']
});
use OpenSpout\Common\Entity\Style\Color;
use OpenSpout\Common\Entity\Style\CellAlignment;
use OpenSpout\Common\Entity\Style\Style;
use OpenSpout\Common\Entity\Style\Border;
use OpenSpout\Common\Entity\Style\BorderPart;
/* Create a border around a cell */
$border = new Border(
new BorderPart(Border::BOTTOM, Color::LIGHT_BLUE, Border::WIDTH_THIN, Border::STYLE_SOLID),
new BorderPart(Border::LEFT, Color::LIGHT_BLUE, Border::WIDTH_THIN, Border::STYLE_SOLID),
new BorderPart(Border::RIGHT, Color::LIGHT_BLUE, Border::WIDTH_THIN, Border::STYLE_SOLID),
new BorderPart(Border::TOP, Color::LIGHT_BLUE, Border::WIDTH_THIN, Border::STYLE_SOLID)
);
$style = (new Style())
->setFontBold()
->setFontSize(15)
->setFontColor(Color::BLUE)
->setShouldWrapText()
->setBackgroundColor(Color::YELLOW)
->setBorder($border);
$writer->addRow(['values', 'of', 'the', 'row'], $style);
$writer->setHeaderStyle($style);
SimpleExcelWriter::create(
file: 'document.xlsx',
configureWriter: function ($writer) {
$options = $writer->getOptions();
$options->DEFAULT_COLUMN_WIDTH=25; // set default width
$options->DEFAULT_ROW_HEIGHT=15; // set default height
// set columns 1, 3 and 8 to width 40
$options->setColumnWidth(40, 1, 3, 8);
// set columns 9 through 12 to width 10
$options->setColumnWidthForRange(10, 9, 12);
}
)