SpreadSheet-Coding.com

PhpSpreadsheet

Create Excel Files With Option To Hide The Gridlines In PHP Using PHPSpreadSheet

July 25, 2020

Gridlines on xlsx files help users easily distinguish cells from one another, acting as guides to tell apart between columns and rows. However, for any specific reason, or no reason at all, there is an option to hide gridlines. This spreadsheet tutorial will show how to do that in PhpSpreadsheet.

Requirements:

Step 1.

Setup dependencies.

composer.json
{
    "require": {
        "phpoffice/phpspreadsheet": "^1.3"
    }
}

Step 2.

Install phpspreadsheet.

command line
$ composer install

Step 3.

Create a new PHP file, and start coding.

create-xlsx-files-with-option-to-hide-the-gridlines.php
<?php

// Autoload dependencies
require 'vendor/autoload.php';

// Import the core class of PhpSpreadsheet
use PhpOffice\PhpSpreadsheet\Spreadsheet;

// Import the Xlsx writer class
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// Create a new Spreadsheet object
$spreadsheet = new Spreadsheet();

// Retrieve the current active worksheet
$sheet = $spreadsheet->getActiveSheet();

// Set cell A1 with the "Hello World !" string value
$sheet->setCellValue('A1', 'Hello World !');

// Hide gridlines
$sheet->setShowGridlines(false);

// Write a new .xlsx file
$writer = new Xlsx($spreadsheet);

// Save the new .xlsx file
$writer->save('create-xlsx-files-with-option-to-hide-the-gridlines.xlsx');

Test.

Run the following codes.

command line
$ php create-xlsx-files-with-option-to-hide-the-gridlines.php

Result.

Open the generated file create-xlsx-files-with-option-to-hide-the-gridlines.xlsx.

PHP
// Hide gridlines
$sheet->setShowGridlines(false);

Default.

References: