SpreadSheet-Coding.com

PHPSpreadsheet · Google Sheets API · Excel

Spreadsheets, driven by code.

Hands-on PHP tutorials for working with Excel and Google Sheets — read and write .xlsx, convert files to JSON, stream downloads in the browser, and insert images, formulas, and styling. Every guide ships with code you can copy, run, and adapt.

rotate-text.php View article
<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->setTitle('Survey');

$worksheet->fromArray([
    ['Rep', 'Answered on time', 'Escalated twice', 'Left a comment', 'Notes'],
    ['Ivy', 'Yes', 'No', 'Yes', 'steady'],
    ['Marco', 'Yes', 'Yes', 'No', 'watch'],
    ['Dan', 'No', 'No', 'Yes', 'steady'],
], null, 'A1');

// Long headings over narrow columns: rotate them 45 degrees.
$worksheet->getStyle('B1:D1')->getAlignment()
    ->setTextRotation(45)
    ->setVertical(Alignment::VERTICAL_BOTTOM);

// The 255 special case: letters stacked one under the next, not rotated.
$worksheet->getStyle('E1')->getAlignment()->setTextRotation(255);

// Rotated text needs the room; Excel will not grow the row for you here.
$worksheet->getRowDimension(1)->setRowHeight(90);
foreach (range('B', 'D') as $column) {
    $worksheet->getColumnDimension($column)->setWidth(6);
}

(new Xlsx($spreadsheet))->save('survey.xlsx');
echo "Wrote survey.xlsx\n\n";

printf("%-6s %-14s %s\n", 'Cell', 'Set to', 'Stored as');
foreach (['B1' => 45, 'C1' => 45, 'D1' => 45, 'E1' => 255] as $cell => $set) {
    printf("%-6s %-14s %d\n", $cell, $set, $worksheet->getStyle($cell)->getAlignment()->getTextRotation());
}

echo "\nRow 1 height: " . $worksheet->getRowDimension(1)->getRowHeight() . "\n";

// Anything outside -90..90 (except 255) is rejected, not clamped.
try {
    $worksheet->getStyle('A1')->getAlignment()->setTextRotation(120);
} catch (SpreadsheetException $e) {
    echo "\nsetTextRotation(120): " . $e->getMessage() . "\n";
}

$reloaded = IOFactory::load('survey.xlsx')->getActiveSheet();
echo "\nAfter a save and reload:\n";
printf("  B1 %d\n  E1 %d\n",
    $reloaded->getStyle('B1')->getAlignment()->getTextRotation(),
    $reloaded->getStyle('E1')->getAlignment()->getTextRotation()
);

The full script from Rotate Text In A Cell In Excel Files In PHP Using PHPSpreadSheet — copy, run, adapt.

IOFactory::load() PhpSpreadsheet
Open any spreadsheet file
getActiveSheet() PhpSpreadsheet
Select the worksheet to fill
fromArray() PhpSpreadsheet
Write many rows at once
getCalculatedValue() PhpSpreadsheet
Read a formula result
save('php://output') PhpSpreadsheet
Stream the file as a download
spreadsheets_values->get() Google Sheets
Read a range of cells
spreadsheets_values->update() Google Sheets
Write a range of cells
spreadsheets->create() Google Sheets
Create a new spreadsheet
json_encode() PHP
Serialize rows to JSON
header() PHP
Send the download headers